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.

28.9 KB
Raw Blame
"""
Tests for ``web/dependencies/threadpool.py``.

The helper ``run_db_sync`` exists to wrap ``asyncio.to_thread`` calls
that open ``get_user_db_session`` blocks. Each call must clean up the
worker thread's thread-local DB session on exit, so the next task on
the same worker doesn't inherit the previous user's session.
"""

from __future__ import annotations

import asyncio
import ast
import inspect
import threading
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from unittest.mock import patch

import pytest
from fastapi import APIRouter, FastAPI
from fastapi.routing import APIRoute
from fastapi.testclient import TestClient


def test_run_db_sync_calls_cleanup_on_success():
    """Successful path triggers ``cleanup_current_thread`` exactly once."""
    from local_deep_research.web.dependencies.threadpool import run_db_sync

    with patch(
        "local_deep_research.database.thread_local_session.cleanup_current_thread"
    ) as mock_cleanup:

        def _work(x):
            return x * 2

        result = asyncio.run(run_db_sync(_work, 21))
        assert result == 42
        assert mock_cleanup.call_count == 1


def test_run_db_sync_calls_cleanup_on_exception():
    """Exception path still triggers cleanup — that's the whole point;
    the previous user's session must not stick to the worker."""
    from local_deep_research.web.dependencies.threadpool import run_db_sync

    with patch(
        "local_deep_research.database.thread_local_session.cleanup_current_thread"
    ) as mock_cleanup:

        def _boom():
            raise ValueError("kaboom")

        with pytest.raises(ValueError, match="kaboom"):
            asyncio.run(run_db_sync(_boom))
        assert mock_cleanup.call_count == 1


def test_run_db_sync_runs_on_worker_thread_not_event_loop():
    """The wrapped fn must run off the event-loop thread — that's the
    whole reason for the threadpool offload. If a future refactor accidentally
    inlined the call, this test would fail."""
    from local_deep_research.web.dependencies.threadpool import run_db_sync

    captured: list[int] = []

    def _capture():
        captured.append(threading.get_ident())

    async def _runner():
        await run_db_sync(_capture)
        return threading.get_ident()

    loop_thread_id = asyncio.run(_runner())
    assert captured, "fn was never invoked"
    assert captured[0] != loop_thread_id, (
        "run_db_sync must execute on a worker thread, not the event loop"
    )


def test_run_db_sync_cleanup_failure_does_not_mask_result():
    """If cleanup itself raises, the original return value still propagates
    (the cleanup error is logged at debug, not raised)."""
    from local_deep_research.web.dependencies.threadpool import run_db_sync

    with patch(
        "local_deep_research.database.thread_local_session.cleanup_current_thread",
        side_effect=Exception("cleanup boom"),
    ):
        result = asyncio.run(run_db_sync(lambda: "ok"))
        assert result == "ok"


def test_run_db_sync_attempts_every_cleanup_after_earlier_failures():
    """Each ambient-state cleanup is an independent best-effort step.

    A broken DB-session cleanup must not leave the settings or egress context
    attached to the pooled worker, and a broken settings cleanup must not stop
    the egress cleanup either.  Cleanup failures also must not replace a
    successful task's return value.
    """
    from local_deep_research.web.dependencies.threadpool import run_db_sync

    attempted: list[tuple[str, int]] = []
    work_thread: list[int] = []

    def _failing_cleanup(name):
        def _cleanup():
            attempted.append((name, threading.get_ident()))
            raise RuntimeError(f"{name} cleanup failed")

        return _cleanup

    def _work():
        work_thread.append(threading.get_ident())
        return "original result"

    with (
        patch(
            "local_deep_research.database.thread_local_session.cleanup_current_thread",
            side_effect=_failing_cleanup("db"),
        ),
        patch(
            "local_deep_research.config.thread_settings.clear_settings_context",
            side_effect=_failing_cleanup("settings"),
        ),
        patch(
            "local_deep_research.security.egress.audit_hook.clear_active_context",
            side_effect=_failing_cleanup("egress"),
        ),
    ):
        result = asyncio.run(run_db_sync(_work))

    assert result == "original result"
    assert [name for name, _thread_id in attempted] == [
        "db",
        "settings",
        "egress",
    ]
    assert work_thread
    assert {thread_id for _name, thread_id in attempted} == {work_thread[0]}


def test_run_db_sync_cleanup_failures_do_not_mask_task_exception():
    """The task's exception wins even if every cleanup hook also raises."""
    from local_deep_research.web.dependencies.threadpool import run_db_sync

    attempted: list[str] = []
    original = ValueError("original task failure")

    def _failing_cleanup(name):
        def _cleanup():
            attempted.append(name)
            raise RuntimeError(f"{name} cleanup failed")

        return _cleanup

    def _work():
        raise original

    with (
        patch(
            "local_deep_research.database.thread_local_session.cleanup_current_thread",
            side_effect=_failing_cleanup("db"),
        ),
        patch(
            "local_deep_research.config.thread_settings.clear_settings_context",
            side_effect=_failing_cleanup("settings"),
        ),
        patch(
            "local_deep_research.security.egress.audit_hook.clear_active_context",
            side_effect=_failing_cleanup("egress"),
        ),
        pytest.raises(ValueError) as exc_info,
    ):
        asyncio.run(run_db_sync(_work))

    assert exc_info.value is original
    assert attempted == ["db", "settings", "egress"]


def test_run_db_sync_clears_egress_context_before_worker_reuse():
    """A later task on the same pooled worker sees no prior egress policy.

    The one-worker executor makes reuse deterministic.  The second task uses
    bare ``asyncio.to_thread`` so it observes the worker immediately, before
    any cleanup belonging to that second task could hide a leak.
    """
    from local_deep_research.security.egress.audit_hook import (
        get_active_context,
        set_active_context,
    )
    from local_deep_research.web.dependencies.threadpool import run_db_sync

    marker = object()

    def _arm_context():
        set_active_context(marker)
        return threading.get_ident(), get_active_context()

    def _observe_context():
        return threading.get_ident(), get_active_context()

    async def _scenario():
        loop = asyncio.get_running_loop()
        with ThreadPoolExecutor(max_workers=1) as executor:
            loop.set_default_executor(executor)
            armed = await run_db_sync(_arm_context)
            observed = await asyncio.to_thread(_observe_context)
        return armed, observed

    # Keep the two unrelated cleanup hooks inert so this test isolates the
    # real egress cleanup rather than touching database bootstrap state.
    with (
        patch(
            "local_deep_research.database.thread_local_session.cleanup_current_thread"
        ),
        patch(
            "local_deep_research.config.thread_settings.clear_settings_context"
        ),
    ):
        (
            (armed_thread, armed_context),
            (
                observed_thread,
                observed_context,
            ),
        ) = asyncio.run(_scenario())

    assert armed_context is marker
    assert observed_thread == armed_thread
    assert observed_context is None


def test_run_db_sync_passes_kwargs_through():
    from local_deep_research.web.dependencies.threadpool import run_db_sync

    def _work(a, *, b):
        return a + b

    with patch(
        "local_deep_research.database.thread_local_session.cleanup_current_thread"
    ):
        assert asyncio.run(run_db_sync(_work, 10, b=32)) == 42


def test_worker_cleanup_route_runs_cleanup_on_success_and_error():
    """A synchronous endpoint always cleans on the serving worker."""
    from fastapi.dependencies.models import Dependant

    from local_deep_research.web.dependencies.threadpool import (
        WorkerCleanupAPIRoute,
        wrap_sync_route_with_cleanup,
    )

    worker_threads: list[int] = []
    cleanup_threads: list[int] = []

    def ok():
        worker_threads.append(threading.get_ident())
        return {"ok": True}

    def boom():
        worker_threads.append(threading.get_ident())
        raise RuntimeError("route failed")

    ok_route = WorkerCleanupAPIRoute("/ok", ok, methods=["GET"])
    assert ok_route.endpoint is ok
    assert getattr(
        ok_route.dependant.call, "__ldr_worker_thread_cleanup__", False
    )
    assert ok_route.dependant.call.__wrapped__ is ok

    # wrap_sync_route_with_cleanup takes a Dependant and mutates ``.call`` in
    # place (rather than returning a wrapped callable) so it can gate on
    # FastAPI's own Dependant.is_coroutine_callable/is_gen_callable/
    # is_async_gen_callable instead of inspect.iscoroutinefunction et al.
    # (see the function's docstring / issue (5) of the #6095 follow-up).
    boom_dependant = Dependant(call=boom)
    wrap_sync_route_with_cleanup(boom_dependant)
    assert boom_dependant.call is not boom
    assert getattr(boom_dependant.call, "__ldr_worker_thread_cleanup__", False)

    with patch(
        "local_deep_research.database.thread_local_session.cleanup_current_thread",
        side_effect=lambda: cleanup_threads.append(threading.get_ident()),
    ):
        with ThreadPoolExecutor(max_workers=1) as executor:
            assert executor.submit(ok_route.dependant.call).result() == {
                "ok": True
            }
            with pytest.raises(RuntimeError, match="route failed"):
                executor.submit(boom_dependant.call).result()

    assert cleanup_threads == worker_threads


def test_worker_cleanup_encodes_plain_return_value_before_releasing_session():
    """A non-``Response`` return value must be serialized before cleanup.

    Regression test for the release-before-serialize ordering bug:
    FastAPI's own routing.py only calls ``serialize_response``/
    ``jsonable_encoder`` on a plain (non-``Response``) return value AFTER
    ``dependant.call`` has already returned. An ordinary
    ``with thread_cleanup(): return fn(...)`` releases the session the
    instant ``fn`` returns -- before that encoding step -- so a lazily
    materialized value would be read back with the session already gone.
    ``wrap_sync_route_with_cleanup`` must therefore encode the value
    itself, inside the cleanup block, before cleanup runs.
    """
    from local_deep_research.web.dependencies.threadpool import (
        WorkerCleanupAPIRoute,
    )

    events: list[str] = []

    class LazyValue:
        """Stands in for data whose materialization needs a live session.

        ``jsonable_encoder`` falls back to ``dict(obj)`` for an object
        that isn't a primitive, Pydantic model, dataclass, dict, or
        list -- which calls ``__iter__``. Recording that call lets the
        test observe whether it happened before or after cleanup.
        """

        def __iter__(self):
            events.append("encoded")
            return iter({"id": 1}.items())

    def endpoint():
        return {"value": LazyValue()}

    route = WorkerCleanupAPIRoute("/lazy", endpoint, methods=["GET"])

    with patch(
        "local_deep_research.database.thread_local_session.cleanup_current_thread",
        side_effect=lambda: events.append("cleaned"),
    ):
        with ThreadPoolExecutor(max_workers=1) as executor:
            result = executor.submit(route.dependant.call).result()

    assert events == ["encoded", "cleaned"], (
        f"value must be serialized before the session is released, got {events}"
    )
    assert result == {"value": {"id": 1}}


def test_worker_cleanup_pins_serialization_shape_for_untyped_any_routes():
    """Regression test for issue (2): the docstring's premise was false.

    This wrapper's docstring used to claim no route in the app declares a
    ``response_model``, so every sync route takes FastAPI's bare
    ``jsonable_encoder`` fallback in ``serialize_response``. That premise is
    false: a route annotated ``-> Any`` (21 of them in
    ``news_flask_api.py``, 14 synchronous) gets a ``response_model``/
    ``response_field`` derived from that annotation by
    ``APIRoute.__init__`` (``fastapi/routing.py``), so
    ``serialize_response`` takes the ``field.validate`` +
    ``field.serialize_json`` branch instead -- just on a value this wrapper
    already ran through ``jsonable_encoder``.

    Because ``jsonable_encoder`` already reduces datetimes/Decimals/
    timedeltas to plain JSON primitives before pydantic's serializer ever
    sees them, pydantic's own JSON-mode formatting for those types never
    applies. This pins the resulting shape (Python's ``datetime.isoformat()``
    with a ``+00:00`` offset, ``Decimal`` narrowed to ``float``, ``timedelta``
    narrowed to a ``float`` of total seconds) so a future ``-> Any`` sync
    route returning a raw, non-``isoformat()``'d value is caught by a shape
    change here rather than silently shipping a different wire format.
    """
    from datetime import datetime, timedelta, timezone
    from decimal import Decimal
    from typing import Any

    from fastapi import FastAPI
    from fastapi.testclient import TestClient

    from local_deep_research.web.dependencies.threadpool import (
        WorkerCleanupAPIRoute,
    )

    def untyped_any_endpoint() -> Any:
        return {
            "created_at": datetime(2026, 1, 1, 12, 0, 0, tzinfo=timezone.utc),
            "amount": Decimal("1.50"),
            "duration": timedelta(seconds=90),
        }

    app = FastAPI()
    app.router.route_class = WorkerCleanupAPIRoute
    app.add_api_route("/thing", untyped_any_endpoint, methods=["GET"])

    with patch(
        "local_deep_research.database.thread_local_session.cleanup_current_thread"
    ):
        with TestClient(app) as client:
            response = client.get("/thing")

    assert response.status_code == 200
    assert response.json() == {
        "created_at": "2026-01-01T12:00:00+00:00",
        "amount": 1.5,
        "duration": 90.0,
    }


def test_worker_cleanup_leaves_async_and_generator_endpoints_unchanged():
    """Deferred endpoint bodies need a different lifetime model."""
    from fastapi.dependencies.models import Dependant

    from local_deep_research.web.dependencies.threadpool import (
        wrap_sync_route_with_cleanup,
    )

    async def async_endpoint():
        return {"kind": "async"}

    def sync_generator_endpoint():
        yield {"kind": "sync-generator"}

    async def async_generator_endpoint():
        yield {"kind": "async-generator"}

    for endpoint in (
        async_endpoint,
        sync_generator_endpoint,
        async_generator_endpoint,
    ):
        dependant = Dependant(call=endpoint)
        wrap_sync_route_with_cleanup(dependant)
        assert dependant.call is endpoint, (
            f"{endpoint} should be left untouched by wrap_sync_route_with_cleanup"
        )


def test_worker_cleanup_gates_on_fastapi_callable_classification_not_inspect():
    """Regression test for issue (5): gate on Dependant.is_*_callable.

    ``inspect.iscoroutinefunction``/``isgeneratorfunction``/
    ``isasyncgenfunction`` do NOT follow a ``functools.wraps`` __wrapped__
    chain, but FastAPI's own ``Dependant.is_coroutine_callable`` /
    ``is_gen_callable`` / ``is_async_gen_callable`` do (they call
    ``inspect.unwrap`` internally) -- and FastAPI dispatches based on its
    own predicates, not ``inspect.*``. A sync ``functools.wraps`` passthrough
    over an ``async def`` endpoint must therefore be left untouched here too,
    even though ``inspect.iscoroutinefunction`` on the passthrough itself
    would say False.
    """
    import functools

    from fastapi.dependencies.models import Dependant

    from local_deep_research.web.dependencies.threadpool import (
        wrap_sync_route_with_cleanup,
    )

    async def real_async_endpoint():
        return {"kind": "async"}

    @functools.wraps(real_async_endpoint)
    def sync_passthrough_over_async(*args, **kwargs):
        return real_async_endpoint(*args, **kwargs)

    assert inspect.iscoroutinefunction(sync_passthrough_over_async) is False, (
        "test setup: this must be the exact case inspect.* misclassifies"
    )

    dependant = Dependant(call=sync_passthrough_over_async)
    assert dependant.is_coroutine_callable is True, (
        "test setup: FastAPI's own predicate must classify this as a "
        "coroutine callable via __wrapped__"
    )

    wrap_sync_route_with_cleanup(dependant)

    assert dependant.call is sync_passthrough_over_async, (
        "a callable FastAPI's own dispatch model treats as a coroutine "
        "must not be wrapped in the sync worker-cleanup boundary"
    )


def test_sync_iterator_cleanup_runs_on_chunk_completion_and_error():
    """Every ``next()`` outcome cleans on the worker that produced it."""
    from local_deep_research.web.dependencies.threadpool import (
        iterate_sync_with_cleanup,
    )

    cleanup_threads: list[int] = []
    iteration_threads: list[int] = []

    def complete_body():
        iteration_threads.append(threading.get_ident())
        yield b"complete"

    def failing_body():
        iteration_threads.append(threading.get_ident())
        yield b"first"
        iteration_threads.append(threading.get_ident())
        raise ValueError("stream failed")

    with patch(
        "local_deep_research.database.thread_local_session.cleanup_current_thread",
        side_effect=lambda: cleanup_threads.append(threading.get_ident()),
    ):
        with ThreadPoolExecutor(max_workers=1) as executor:
            complete = iterate_sync_with_cleanup(complete_body())
            assert executor.submit(next, complete).result() == b"complete"
            with pytest.raises(StopIteration):
                executor.submit(next, complete).result()

            failing = iterate_sync_with_cleanup(failing_body())
            assert executor.submit(next, failing).result() == b"first"
            with pytest.raises(ValueError, match="stream failed"):
                executor.submit(next, failing).result()

    assert len(cleanup_threads) == 4
    assert set(cleanup_threads) == set(iteration_threads)


def test_sync_iterator_cleanup_runs_on_explicit_close():
    """Closing a suspended wrapper runs the body's finally inside cleanup.

    This tests the explicit-close contract, not whether Starlette closes
    the response iterator immediately when a client disconnects.
    """
    from local_deep_research.web.dependencies.threadpool import (
        iterate_sync_with_cleanup,
    )

    events: list[str] = []

    def body():
        try:
            events.append("start")
            yield b"chunk1"
            yield b"chunk2"
        finally:
            events.append("inner-finally")

    cleanup_calls: list[None] = []
    with patch(
        "local_deep_research.database.thread_local_session.cleanup_current_thread",
        side_effect=lambda: cleanup_calls.append(None),
    ):
        wrapped = iterate_sync_with_cleanup(body())
        assert next(wrapped) == b"chunk1"
        assert events == ["start"]
        assert len(cleanup_calls) == 1

        # Close directly; a real response may instead rely on finalization.
        wrapped.close()

    # PRIMARY assertion. This is the only one that fails with the fix
    # reverted: without the GeneratorExit handler's explicit close, CPython
    # still runs the body's ``finally`` when the sole reference to it is
    # dropped (refcount finalization), so ``events`` below reaches its final
    # state either way. Only the SECOND cleanup boundary is genuinely new.
    assert len(cleanup_calls) == 2, (
        "close() must get its own thread_cleanup() boundary, not run bare"
    )
    # Ordering only -- deliberately NOT the regression signal, for the
    # refcount reason above.
    assert events == ["start", "inner-finally"], (
        "wrapped generator's finally must run during close(), in order"
    )


def test_sync_iterator_close_is_a_noop_for_non_generator_iterables():
    """A plain iterable (no ``.close()``) must not break on close()."""
    from local_deep_research.web.dependencies.threadpool import (
        iterate_sync_with_cleanup,
    )

    with patch(
        "local_deep_research.database.thread_local_session.cleanup_current_thread"
    ):
        wrapped = iterate_sync_with_cleanup([b"a", b"b", b"c"])
        assert next(wrapped) == b"a"
        wrapped.close()  # must not raise AttributeError


def test_streaming_response_leaves_async_body_on_the_event_loop():
    """Async response bodies must not be handed to the sync worker wrapper."""
    from local_deep_research.web.dependencies.threadpool import (
        WorkerCleanupStreamingResponse,
    )

    async def body():
        yield b"async"

    content = body()
    response = WorkerCleanupStreamingResponse(content)
    assert response.body_iterator is content


def test_production_routers_do_not_use_bare_streaming_response():
    """New synchronous streams cannot silently bypass worker cleanup."""
    router_root = (
        Path(__file__).resolve().parents[3]
        / "src"
        / "local_deep_research"
        / "web"
        / "routers"
    )
    bare_imports: list[str] = []
    bare_calls: list[str] = []
    protected_calls: list[str] = []

    for path in sorted(router_root.glob("*.py")):
        tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path))
        for node in ast.walk(tree):
            if isinstance(node, ast.ImportFrom):
                for name in node.names:
                    if name.name == "StreamingResponse":
                        bare_imports.append(f"{path.name}:{node.lineno}")
            if not isinstance(node, ast.Call):
                continue
            if isinstance(node.func, ast.Name):
                call_name = node.func.id
            elif isinstance(node.func, ast.Attribute):
                call_name = node.func.attr
            else:
                call_name = None
            if call_name == "StreamingResponse":
                bare_calls.append(f"{path.name}:{node.lineno}")
            elif call_name == "WorkerCleanupStreamingResponse":
                protected_calls.append(f"{path.name}:{node.lineno}")

    assert bare_imports == [], (
        "router modules imported bare StreamingResponse: "
        f"{bare_imports}; use WorkerCleanupStreamingResponse"
    )
    assert bare_calls == [], (
        "router modules constructed bare StreamingResponse objects: "
        f"{bare_calls}; use WorkerCleanupStreamingResponse"
    )
    assert len(protected_calls) >= 5, (
        f"the production stream census unexpectedly shrank: {protected_calls}"
    )


def test_prepare_router_wraps_sync_routes_only_and_is_idempotent():
    """Mounted routes wrap dispatch without replacing endpoint identity."""
    from local_deep_research.web.dependencies.threadpool import (
        prepare_router_for_worker_cleanup,
    )

    router = APIRouter()

    @router.get("/sync")
    def sync_endpoint():
        return {"kind": "sync"}

    @router.get("/async")
    async def async_endpoint():
        return {"kind": "async"}

    app = FastAPI()
    app.include_router(router)
    original = {
        route.path: (route.endpoint, route.dependant.call)
        for route in app.routes
        if isinstance(route, APIRoute)
    }
    prepare_router_for_worker_cleanup(app.router)
    once = {
        route.path: (route.endpoint, route.dependant.call)
        for route in app.routes
        if isinstance(route, APIRoute)
    }
    prepare_router_for_worker_cleanup(app.router)
    twice = {
        route.path: (route.endpoint, route.dependant.call)
        for route in app.routes
        if isinstance(route, APIRoute)
    }

    assert once["/sync"][0] is original["/sync"][0]
    assert once["/sync"][1] is not original["/sync"][1]
    assert once["/sync"][1].__wrapped__ is once["/sync"][0]
    assert once["/async"] == original["/async"]
    assert twice == once


def test_prepared_router_dispatches_through_cleanup_on_the_worker():
    """Post-mount dispatch mutation is observed by FastAPI's route handler."""
    from local_deep_research.web.dependencies.threadpool import (
        prepare_router_for_worker_cleanup,
    )

    endpoint_threads: list[int] = []
    cleanup_threads: list[int] = []
    router = APIRouter()

    @router.get("/sync")
    def sync_endpoint():
        endpoint_threads.append(threading.get_ident())
        return {"ok": True}

    app = FastAPI()
    app.include_router(router)
    prepare_router_for_worker_cleanup(app.router)

    mounted = next(
        route
        for route in app.routes
        if isinstance(route, APIRoute) and route.path == "/sync"
    )
    assert mounted.endpoint is sync_endpoint

    with patch(
        "local_deep_research.database.thread_local_session.cleanup_current_thread",
        side_effect=lambda: cleanup_threads.append(threading.get_ident()),
    ):
        with TestClient(app) as client:
            response = client.get("/sync")

    assert response.status_code == 200
    assert response.json() == {"ok": True}
    assert endpoint_threads
    assert cleanup_threads == endpoint_threads


def test_production_app_wraps_every_sync_api_route():
    """No module router or app-local endpoint can bypass worker cleanup.

    Classifies "synchronous" the same way ``wrap_sync_route_with_cleanup``
    itself does -- via ``route.dependant.is_coroutine_callable`` /
    ``is_gen_callable`` / ``is_async_gen_callable`` -- rather than
    ``inspect.iscoroutinefunction(route.endpoint)``. The latter does not
    follow a ``functools.wraps`` ``__wrapped__`` chain and shares the exact
    blind spot fixed in the production code (issue (5) of the #6095
    follow-up): a hypothetical sync passthrough wrapping an async endpoint
    would be miscounted as "needs cleanup" by ``inspect.*`` here even though
    FastAPI itself, and this module's own wrapper, correctly treat it as a
    coroutine callable and leave it alone.

    That swap alone would DISARM this census for generator endpoints, so it
    comes with ``generator_routes`` below.  ``inspect.iscoroutinefunction``
    is ``False`` for BOTH ``def f(): yield`` and ``async def f(): yield``
    (verified against fastapi 0.136.3), so the old predicate swept generator
    endpoints into ``sync_routes``, where they failed ``missing == []``
    because ``wrap_sync_route_with_cleanup`` deliberately leaves them
    unmarked.  The new predicate excludes them -- and neither
    ``test_production_routers_do_not_use_bare_streaming_response`` (it
    AST-scans for ``StreamingResponse`` construction, which a generator
    endpoint never performs) nor anything else would notice.  FastAPI would
    stream such a route through ``_sync_stream_jsonl()`` -> a bare
    ``StreamingResponse`` -> ``iterate_in_threadpool`` with no per-chunk
    cleanup: the #6095 leak class, silently reintroduced.  The separate
    ``generator_routes`` assertion is what keeps that visible.
    """
    from local_deep_research.web.fastapi_app import app

    api_routes = [route for route in app.routes if isinstance(route, APIRoute)]
    sync_routes = [
        route
        for route in api_routes
        if not route.dependant.is_coroutine_callable
        and not route.dependant.is_gen_callable
        and not route.dependant.is_async_gen_callable
    ]
    generator_routes = [
        route.path
        for route in api_routes
        if route.dependant.is_gen_callable
        or route.dependant.is_async_gen_callable
    ]
    missing = [
        route.path
        for route in sync_routes
        if not getattr(
            route.dependant.call, "__ldr_worker_thread_cleanup__", False
        )
    ]
    identity_mismatches = [
        route.path
        for route in sync_routes
        if getattr(route.dependant.call, "__wrapped__", None)
        is not route.endpoint
    ]

    assert sync_routes, "production app unexpectedly has no synchronous routes"
    assert missing == [], (
        f"synchronous routes without owner-worker cleanup: {sorted(missing)}"
    )
    assert generator_routes == [], (
        "the production app grew a generator endpoint: "
        f"{sorted(generator_routes)}. ``wrap_sync_route_with_cleanup`` "
        "deliberately leaves these untouched (see its skip branch and the "
        "comment above it, "
        "src/local_deep_research/web/dependencies/threadpool.py:143-162) "
        "because an endpoint-level ``finally`` would clean up before "
        "FastAPI even starts streaming the body. FastAPI streams such a "
        "route through a bare ``StreamingResponse`` and "
        "``iterate_in_threadpool``, with no per-chunk cleanup -- the #6095 "
        "leak class. Give the route an explicit "
        "``WorkerCleanupStreamingResponse(iterate_sync_with_cleanup(...))`` "
        "body instead of a generator endpoint, then drop it from here."
    )
    assert identity_mismatches == [], (
        "cleanup dispatch wrappers no longer point at the registered endpoint: "
        f"{sorted(identity_mismatches)}"
    )