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.

30.5 KB
Raw Blame
"""Contract tests for the SSE / streaming endpoints on the Flask -> FastAPI
migration branch.

Flask's ``Response(stream_with_context(generator()))`` became Starlette's
``StreamingResponse`` over a sync generator, driven through anyio's
threadpool. That change in execution model has already produced three real,
shipped bugs on this branch:

  * a DB session held open across a ``yield`` (the session's ``__enter__``
    and ``__exit__`` can land on different pooled OS threads, corrupting the
    thread-local scope-depth counter -- see the comments above every
    ``get_user_db_session`` call in the generators below);
  * SSE responses missing the anti-buffering headers nginx needs to not
    buffer the whole stream until completion;
  * an unbounded ``Thread.join()`` on client disconnect, blocking the
    ASGI event-loop-adjacent teardown path for as long as an in-flight
    embedding batch takes.

STEP 1 SURVEY -- every ``StreamingResponse`` construction under
``src/local_deep_research/web/routers/`` (grepped for ``StreamingResponse``
and ``text/event-stream``; confirmed no other package under ``src/``
constructs one):

  1. ``research.py::export_research_logs`` (~L1909-1988) -- NDJSON log
     export, ``media_type="application/x-ndjson"``. Its generator snapshots
     an ordered id list in one short-lived ``get_user_db_session`` scope,
     closes it, then hydrates+serializes each 500-row batch in its own
     short-lived session BEFORE yielding the batch -- no session is ever
     open across a yield.
  2. ``library.py::download_all_text`` (~L626-748) -- SSE,
     ``media_type="text/event-stream"``. Same pattern: one short-lived
     session to snapshot resource rows, closed before the per-resource
     download/yield loop.
  3. ``library.py::download_bulk`` (~L846-1145) -- SSE,
     ``media_type="text/event-stream"``. Every ``get_user_db_session`` call
     in its generator (queueing, claim, resource lookup, finalize/release)
     opens and closes within a single loop iteration before that
     iteration's ``yield``.
  4. ``rag.py::index_all`` (~L937-1139) -- SSE,
     ``media_type="text/event-stream"``. Setup DB work (embedding-config
     write + doc-id snapshot) happens in one short-lived session closed
     before the first yield; the indexing loop itself yields with no
     session held.
  5. ``rag.py::index_collection`` (~L2527-2853) -- SSE,
     ``media_type="text/event-stream"``. Same short-lived-session pattern
     as ``index_all``, PLUS the disconnect-drain machinery covered by
     contract 4 below: it fans indexing out to a background
     ``index-collection-parallel`` thread so the generator's main thread
     can keep yielding SSE heartbeats, and on generator ``finally`` (client
     disconnect or normal completion) it joins that worker with a bounded
     5s grace period, registering cleanup on worker completion if it is
     still alive after the grace period.

Coverage map for the four requested contracts:

  1. DB session held across a yield -- NOT covered elsewhere as a
     standalone, generic AST invariant. Implemented below
     (``find_session_held_across_yield`` + self-tests).
  2. SSE anti-buffering headers -- ALREADY FULLY COVERED by
     ``tests/web/routers/test_sse_response_headers.py``: per-endpoint HTTP
     tests for all 4 SSE routes above PLUS a generic source-level AST audit
     (``test_every_sse_streaming_response_sets_anti_buffering_headers``)
     that fails on any *new* ``text/event-stream`` StreamingResponse
     lacking the headers. Re-implementing the same AST-window scan here
     would be a duplicate, not a complementary check, so it is
     intentionally NOT repeated in this file.
  3. Correct media types -- implemented below as a generic closed-set
     invariant over every ``StreamingResponse`` call site (must be exactly
     ``text/event-stream`` or ``application/x-ndjson``, matching the SSE
     and NDJSON contracts respectively), plus a check that the two binary
     export routes (PDF, report export) use plain ``Response`` with a real,
     non-streaming mimetype. The "must not be StreamingResponse-over-bytes"
     half of that requirement is the sibling guard's job
     (``tests/web/routers/test_migration_antipattern_guards.py::
     test_no_streamingresponse_wraps_bytesio`` /
     ``find_streamingresponse_bytesio``) -- imported and reused here rather
     than reimplemented.
  4. Bounded disconnect drain -- ``tests/research_library/routes/
     test_rag_routes_cancel_and_worker_wiring.py::
     TestIndexCollectionDisconnectWorkerDrain`` already exercises this
     endpoint's actual runtime behaviour in detail (mocked ``threading
     .Thread``, real timing assertions, all three branches: fast exit,
     finishes-within-grace, deferred-to-daemon-drain). That file is not
     duplicated here. Instead, this file adds a complementary, differently
     shaped check: a static AST invariant (``find_unbounded_thread_join_in_
     generator_scope``) that any streaming generator's OWN scope (i.e.
     excluding nested ``def`` closures, which is exactly the sanctioned
     "defer the unbounded join to a background daemon thread" shape) may
     never contain a bare, timeout-less ``Thread.join()`` -- so a *future*
     streaming endpoint reintroducing this bug fails here even before any
     behavioral test is written for it.
"""

import ast
from pathlib import Path

import local_deep_research.web.routers as routers_pkg

ROUTERS_DIR = Path(routers_pkg.__file__).resolve().parent
SCANNED_FILES = sorted(ROUTERS_DIR.glob("*.py"))


def _rel(path: Path) -> str:
    return str(path.relative_to(ROUTERS_DIR.parents[3]))


def _parse(path: Path) -> ast.Module:
    return ast.parse(path.read_text(encoding="utf-8"), filename=str(path))


# ===========================================================================
# Shared AST helpers
# ===========================================================================


def _call_target_name(node: ast.AST):
    """The bare/attribute name a Call invokes, e.g. 'Thread' for both
    ``Thread(...)`` and ``threading.Thread(...)``; ``None`` if not a Call."""
    if not isinstance(node, ast.Call):
        return None
    func = node.func
    if isinstance(func, ast.Name):
        return func.id
    if isinstance(func, ast.Attribute):
        return func.attr
    return None


def _is_db_session_call(node: ast.AST) -> bool:
    return _call_target_name(node) == "get_user_db_session"


def _direct_yield(node: ast.AST) -> bool:
    """True if ``node`` contains a ``yield``/``yield from`` reachable
    without crossing into a nested function/lambda/class scope -- i.e. a
    yield that suspends the SAME generator ``node`` sits inside, not some
    unrelated nested helper's own generator."""
    for child in ast.iter_child_nodes(node):
        if isinstance(child, (ast.Yield, ast.YieldFrom)):
            return True
        if isinstance(
            child,
            (ast.FunctionDef, ast.AsyncFunctionDef, ast.Lambda, ast.ClassDef),
        ):
            continue  # separate scope
        if _direct_yield(child):
            return True
    return False


def _find_generator_functions(tree: ast.AST):
    """Every FunctionDef/AsyncFunctionDef in ``tree`` whose OWN body (not a
    nested def's) contains a yield -- i.e. is itself a generator function,
    such as the ``generate()`` closures the routes above build their
    ``StreamingResponse``/SSE bodies from."""
    for node in ast.walk(tree):
        if isinstance(
            node, (ast.FunctionDef, ast.AsyncFunctionDef)
        ) and _direct_yield(node):
            yield node


# ===========================================================================
# Contract 1: no DB session may be held open across a yield.
# ===========================================================================


def find_session_held_across_yield(tree: ast.AST):
    """Return (lineno, message) for every ``with get_user_db_session(...):``
    (or ``async with``) block whose body directly yields -- i.e. the
    session's context-manager scope straddles a ``yield``. Scoped per
    ``with`` statement; a nested ``def`` inside the block is a separate
    generator and is not searched (see ``_direct_yield``)."""
    violations = []
    for node in ast.walk(tree):
        if not isinstance(node, (ast.With, ast.AsyncWith)):
            continue
        if not any(
            _is_db_session_call(item.context_expr) for item in node.items
        ):
            continue
        if _direct_yield(node):
            violations.append(
                (
                    node.lineno,
                    "get_user_db_session(...) held open across a yield "
                    "inside this `with` block",
                )
            )
    return violations


# file::L<lineno> -> justification. Seeded ONLY with verified-safe cases.
# (Currently empty: all 5 streaming generators in the survey close every
# get_user_db_session scope before yielding -- see the module docstring.)
SESSION_ACROSS_YIELD_ALLOWLIST: dict[str, str] = {}


def test_no_db_session_held_across_yield_in_streaming_generator():
    """Regression fence for the DB-session-across-yield bug: Starlette
    drives a sync generator via anyio's threadpool, so a
    ``get_user_db_session`` scope entered on one ``next()`` call and exited
    on a later one can have its ``__enter__``/``__exit__`` land on two
    different pooled OS threads, corrupting the thread-local scope-depth
    counter for every session on that thread going forward. Every session
    must be opened, used, and closed entirely between two yields (or before
    the first / after the last)."""
    violations = []
    for path in SCANNED_FILES:
        tree = _parse(path)
        for lineno, message in find_session_held_across_yield(tree):
            key = f"{_rel(path)}::L{lineno}"
            if key in SESSION_ACROSS_YIELD_ALLOWLIST:
                continue
            violations.append(f"  {_rel(path)}:{lineno}: {message}")

    assert not violations, (
        "get_user_db_session(...) held open across a yield in a streaming "
        "generator. Snapshot whatever the post-yield code needs into plain "
        "values INSIDE the session, close the session, THEN yield -- see "
        "research.py's export_research_logs (batch hydration + full "
        'in-session serialization before `yield "".join(lines)`) or '
        "rag.py's index_collection (short-lived setup session closed before "
        "the SSE loop starts) for the established fix.\n"
        + "\n".join(violations)
    )


class TestSessionAcrossYieldScannerSelfTest:
    """Proves the detector actually distinguishes the bug from the fix."""

    def test_flags_yield_directly_inside_with_db_session_block(self):
        tree = ast.parse(
            "def route():\n"
            "    def generate():\n"
            "        with get_user_db_session(username) as s:\n"
            "            for row in s.query(X).all():\n"
            "                yield row.id\n"
            "    return StreamingResponse(generate())\n"
        )
        violations = find_session_held_across_yield(tree)
        assert len(violations) == 1
        assert violations[0][0] == 3

    def test_flags_yield_nested_in_control_flow_inside_with_block(self):
        """A yield doesn't have to be a direct statement of the `with`
        body -- it just must be reachable without crossing a def/lambda
        boundary (e.g. buried in an if/for)."""
        tree = ast.parse(
            "def generate():\n"
            "    with get_user_db_session(username) as s:\n"
            "        for row in s.query(X).all():\n"
            "            if row.ok:\n"
            "                yield row.id\n"
        )
        assert len(find_session_held_across_yield(tree)) == 1

    def test_flags_attribute_style_db_session_call(self):
        tree = ast.parse(
            "def generate():\n"
            "    with db_utils.get_user_db_session(username) as s:\n"
            "        yield s.id\n"
        )
        assert len(find_session_held_across_yield(tree)) == 1

    def test_ignores_yield_after_with_block_closes(self):
        """The established fix: snapshot inside the session, close it,
        THEN yield -- matches download_bulk / index_collection today."""
        tree = ast.parse(
            "def generate():\n"
            "    with get_user_db_session(username) as s:\n"
            "        ids = [row.id for row in s.query(X).all()]\n"
            "    for i in ids:\n"
            "        yield i\n"
        )
        assert find_session_held_across_yield(tree) == []

    def test_ignores_yield_inside_nested_function_within_with_block(self):
        """A yield belonging to a DIFFERENT nested generator defined
        inside the with-block does not suspend the outer generator that
        holds the session, so it must not be flagged."""
        tree = ast.parse(
            "def generate():\n"
            "    with get_user_db_session(username) as s:\n"
            "        def _inner():\n"
            "            yield 1\n"
            "        list(_inner())\n"
        )
        assert find_session_held_across_yield(tree) == []

    def test_ignores_non_db_session_with_blocks(self):
        tree = ast.parse(
            "def generate():\n"
            "    with open('f') as fh:\n"
            "        yield fh.read()\n"
        )
        assert find_session_held_across_yield(tree) == []

    def test_ignores_with_db_session_that_never_yields(self):
        tree = ast.parse(
            "def route():\n"
            "    with get_user_db_session(username) as s:\n"
            "        return s.query(X).first()\n"
        )
        assert find_session_held_across_yield(tree) == []


# ===========================================================================
# Contract 3: media types.
# ===========================================================================
#
# Closed set: every literal StreamingResponse(...) call site in the router
# layer must declare media_type as EXACTLY one of these two strings. A third
# value appearing here would mean a new kind of stream was added without
# deciding which contract (SSE heartbeats-and-headers, or NDJSON
# line-oriented download) it follows.
SSE_MEDIA_TYPE = "text/event-stream"
NDJSON_MEDIA_TYPE = "application/x-ndjson"
ALLOWED_STREAMING_MEDIA_TYPES = {SSE_MEDIA_TYPE, NDJSON_MEDIA_TYPE}
STREAMING_RESPONSE_NAMES = frozenset(
    {"StreamingResponse", "WorkerCleanupStreamingResponse"}
)


def find_streamingresponse_media_types(tree: ast.AST):
    """Return (lineno, media_type_or_None) for every StreamingResponse(...)
    call, where media_type_or_None is the string value of its media_type
    argument (positional index 1 or keyword), or None if it's missing or
    not a string constant (e.g. computed)."""
    results = []
    for node in ast.walk(tree):
        if _call_target_name(node) not in STREAMING_RESPONSE_NAMES:
            continue
        media_type_node = None
        if len(node.args) >= 2:
            media_type_node = node.args[1]
        for kw in node.keywords:
            if kw.arg == "media_type":
                media_type_node = kw.value
        value = (
            media_type_node.value
            if isinstance(media_type_node, ast.Constant)
            and isinstance(media_type_node.value, str)
            else None
        )
        results.append((node.lineno, value))
    return results


def test_every_streamingresponse_media_type_is_sse_or_ndjson():
    """Generic invariant over the discovered set: a StreamingResponse's
    media_type must be exactly 'text/event-stream' (SSE) or
    'application/x-ndjson' (the line-oriented log export) -- never missing,
    never a typo'd variant (e.g. trailing whitespace, wrong case,
    'application/json'), and never some third undecided contract."""
    violations = []
    sse_count = 0
    ndjson_count = 0
    for path in SCANNED_FILES:
        tree = _parse(path)
        for lineno, media_type in find_streamingresponse_media_types(tree):
            if media_type == SSE_MEDIA_TYPE:
                sse_count += 1
            elif media_type == NDJSON_MEDIA_TYPE:
                ndjson_count += 1
            else:
                violations.append(
                    f"  {_rel(path)}:{lineno}: media_type={media_type!r} "
                    f"is not in {sorted(ALLOWED_STREAMING_MEDIA_TYPES)}"
                )

    assert not violations, (
        "StreamingResponse with an unrecognised (or missing) media_type. "
        "Streaming responses in this codebase are either SSE "
        "('text/event-stream', with the anti-buffering headers -- see "
        "test_sse_response_headers.py) or the NDJSON log export "
        "('application/x-ndjson'). A binary/one-shot download must use "
        "plain Response(content=<bytes>, media_type=...), never "
        "StreamingResponse.\n" + "\n".join(violations)
    )
    # Anti-tautology: pin the survey's counts so a StreamingResponse call
    # silently added or removed doesn't make this test vacuously pass.
    # (See the module docstring survey: 4 SSE + 1 NDJSON.)
    assert sse_count == 4, sse_count
    assert ndjson_count == 1, ndjson_count


class TestMediaTypeScannerSelfTest:
    def test_flags_unrecognised_media_type(self):
        tree = ast.parse(
            "def route():\n"
            "    return StreamingResponse(generate(), "
            "media_type='application/octet-stream')\n"
        )
        assert find_streamingresponse_media_types(tree) == [
            (2, "application/octet-stream")
        ]

    def test_flags_missing_media_type_as_none(self):
        tree = ast.parse(
            "def route():\n    return StreamingResponse(generate())\n"
        )
        assert find_streamingresponse_media_types(tree) == [(2, None)]

    def test_recognises_sse_and_ndjson(self):
        tree = ast.parse(
            "def a():\n"
            "    return WorkerCleanupStreamingResponse(g(), media_type='text/event-stream')\n"
            "def b():\n"
            "    return StreamingResponse(g(), media_type='application/x-ndjson')\n"
        )
        values = [v for _, v in find_streamingresponse_media_types(tree)]
        assert values == ["text/event-stream", "application/x-ndjson"]

    def test_ignores_plain_response_calls(self):
        tree = ast.parse(
            "def route():\n"
            "    return Response(content=b'x', media_type='application/pdf')\n"
        )
        assert find_streamingresponse_media_types(tree) == []


def test_binary_export_routes_use_response_not_streamingresponse():
    """The other half of the media-type contract: routes that serve a
    fully materialised binary payload (PDF, report export) must use plain
    ``Response`` with the file's real mimetype, not ``StreamingResponse``.

    The "must not be StreamingResponse(BytesIO(...))" detection is the
    sibling guard's job -- imported and reused here (not reimplemented) so
    this file stays a single source of truth for that scanning logic:
    tests/web/routers/test_migration_antipattern_guards.py::
    find_streamingresponse_bytesio.
    """
    from tests.web.routers.test_migration_antipattern_guards import (
        find_streamingresponse_bytesio,
    )

    range_response_path = ROUTERS_DIR.parent / "utils" / "range_response.py"
    binary_export_files = [
        ROUTERS_DIR / "library.py",  # PDF serving (view_pdf_page)
        ROUTERS_DIR / "research.py",  # report export (export_research_report)
        range_response_path,  # buffered byte-range helper used by the PDF route
    ]
    for path in binary_export_files:
        tree = _parse(path)
        assert find_streamingresponse_bytesio(tree) == [], (
            f"{_rel(path)}: a binary export route wraps fully materialised "
            "bytes in StreamingResponse -- see the sibling guard's "
            "docstring for the fix (plain Response(content=...))"
        )

    # And the positive half: each of those files actually DOES serve its
    # binary payload via a real, non-streaming media_type on a plain
    # Response(...) call or its range helper -- proving the assertion above
    # isn't vacuously true because a file has no binary export at all.
    real_media_types_by_file = {}
    for path in binary_export_files:
        tree = _parse(path)
        found = []
        for node in ast.walk(tree):
            if _call_target_name(node) not in {
                "Response",
                "in_memory_range_response",
            }:
                continue
            for kw in node.keywords:
                if kw.arg == "media_type":
                    found.append(kw.value)
        real_media_types_by_file[path.name] = found

    # library.py's PDF route: a literal, real mimetype.
    pdf_media_types = [
        n.value
        for n in real_media_types_by_file["library.py"]
        if isinstance(n, ast.Constant)
    ]
    assert "application/pdf" in pdf_media_types, pdf_media_types
    for value in pdf_media_types:
        assert value not in ALLOWED_STREAMING_MEDIA_TYPES, value

    # Following the helper must not hide a new StreamingResponse or an
    # internally replaced media type. Its caller supplies the PDF literal.
    assert find_streamingresponse_media_types(_parse(range_response_path)) == []
    helper_media_types = real_media_types_by_file["range_response.py"]
    assert len(helper_media_types) == 1, helper_media_types
    assert isinstance(helper_media_types[0], ast.Name)
    assert helper_media_types[0].id == "media_type"

    # research.py's export route: media_type is the exporter-resolved
    # `mimetype` variable (PDF/ODT/RIS/... vary per format), not a
    # hardcoded streaming-contract literal.
    assert real_media_types_by_file["research.py"], (
        "no Response(media_type=...) found"
    )
    for node in real_media_types_by_file["research.py"]:
        if isinstance(node, ast.Constant):
            assert node.value not in ALLOWED_STREAMING_MEDIA_TYPES, node.value


# ===========================================================================
# Contract 4: bounded disconnect drain -- generic static invariant.
# ===========================================================================
#
# tests/research_library/routes/test_rag_routes_cancel_and_worker_wiring.py
# (TestIndexCollectionDisconnectWorkerDrain) already drives
# index_collection's real generator through all three disconnect-drain
# branches with a recording Thread substitute and wall-clock timing
# assertions -- that behavioral coverage is not duplicated here. This is a
# complementary, differently shaped check: a static AST invariant that
# holds across EVERY streaming generator (present or future), not just
# index_collection, so a new SSE endpoint reintroducing an unbounded join
# fails here even before anyone writes a behavioral test for it.


def _is_threading_thread_call(node: ast.AST) -> bool:
    return _call_target_name(node) == "Thread"


def find_unbounded_thread_join_in_generator_scope(func_node):
    """Within a single generator function's OWN scope (excluding nested
    def/lambda/class bodies -- the sanctioned "defer the unbounded join to
    a background daemon thread" shape), flag any bare ``<name>.join()``
    call -- no positional timeout, no ``timeout=`` keyword -- on a name
    that was bound from a ``threading.Thread(...)``/``Thread(...)`` call in
    that same scope.

    A bare join on the generator's OWN thread-handling code blocks the
    ASGI teardown path (client disconnect or normal completion) for as
    long as the worker thread takes; the fix is either a bounded
    ``join(timeout=...)`` inline, or handing the join off to a nested
    (typically daemon) thread body, which this scanner does not search.
    """
    thread_names = set()
    violations = []

    def _walk(node):
        for child in ast.iter_child_nodes(node):
            if isinstance(
                child,
                (
                    ast.FunctionDef,
                    ast.AsyncFunctionDef,
                    ast.Lambda,
                    ast.ClassDef,
                ),
            ):
                continue  # separate scope: deferred join is sanctioned
            if (
                isinstance(child, ast.Assign)
                and len(child.targets) == 1
                and isinstance(child.targets[0], ast.Name)
                and _is_threading_thread_call(child.value)
            ):
                thread_names.add(child.targets[0].id)
            if (
                isinstance(child, ast.Call)
                and isinstance(child.func, ast.Attribute)
                and child.func.attr == "join"
                and isinstance(child.func.value, ast.Name)
                and child.func.value.id in thread_names
            ):
                has_timeout = bool(child.args) or any(
                    kw.arg == "timeout" for kw in child.keywords
                )
                if not has_timeout:
                    violations.append(
                        (
                            child.lineno,
                            f"{child.func.value.id}.join() has no timeout "
                            "and is not deferred to a nested thread body",
                        )
                    )
            _walk(child)

    _walk(func_node)
    return violations


# file::L<lineno> -> justification. Seeded ONLY with verified-safe cases.
# (Currently empty: index_collection's only own-scope join is
# `worker_thread.join(timeout=5.0)`; deferred cleanup uses a worker-completion
# callback without another join.)
UNBOUNDED_JOIN_ALLOWLIST: dict[str, str] = {}


def test_no_unbounded_thread_join_in_streaming_generator_own_scope():
    violations = []
    for path in SCANNED_FILES:
        tree = _parse(path)
        for gen_node in _find_generator_functions(tree):
            for (
                lineno,
                message,
            ) in find_unbounded_thread_join_in_generator_scope(gen_node):
                key = f"{_rel(path)}::L{lineno}"
                if key in UNBOUNDED_JOIN_ALLOWLIST:
                    continue
                violations.append(f"  {_rel(path)}:{lineno}: {message}")

    assert not violations, (
        "Unbounded Thread.join() found directly in a streaming generator's "
        "own scope. On client disconnect this runs on the generator-close "
        "path and blocks it for as long as the worker takes -- use "
        "join(timeout=...) with a bounded grace period, and if the worker "
        "is still alive after that, defer cleanup until worker completion, "
        "matching rag.py's index_collection.\n" + "\n".join(violations)
    )


class TestUnboundedJoinScannerSelfTest:
    """Proves the detector fires on the bad (pre-fix) shape and not on the
    real, fixed shape."""

    def test_flags_bare_join_in_generators_own_finally(self):
        tree = ast.parse(
            "def generate():\n"
            "    t = threading.Thread(target=work, daemon=True)\n"
            "    t.start()\n"
            "    try:\n"
            "        yield 'x'\n"
            "    finally:\n"
            "        t.join()\n"
        )
        (gen_node,) = _find_generator_functions(tree)
        violations = find_unbounded_thread_join_in_generator_scope(gen_node)
        assert len(violations) == 1
        assert violations[0][0] == 7

    def test_ignores_bounded_join_with_timeout_kwarg(self):
        tree = ast.parse(
            "def generate():\n"
            "    t = threading.Thread(target=work, daemon=True)\n"
            "    t.start()\n"
            "    yield 'x'\n"
            "    t.join(timeout=5.0)\n"
        )
        (gen_node,) = _find_generator_functions(tree)
        assert find_unbounded_thread_join_in_generator_scope(gen_node) == []

    def test_ignores_bounded_join_with_positional_timeout(self):
        tree = ast.parse(
            "def generate():\n"
            "    t = threading.Thread(target=work, daemon=True)\n"
            "    t.start()\n"
            "    yield 'x'\n"
            "    t.join(5.0)\n"
        )
        (gen_node,) = _find_generator_functions(tree)
        assert find_unbounded_thread_join_in_generator_scope(gen_node) == []

    def test_ignores_unbounded_join_deferred_to_nested_thread_body(self):
        """The real, fixed shape: a bounded grace join inline, and the
        unbounded join deferred into a nested closure run on a background
        daemon thread -- matching rag.py's index_collection exactly."""
        tree = ast.parse(
            "def generate():\n"
            "    worker_thread = threading.Thread(target=work, daemon=True)\n"
            "    worker_thread.start()\n"
            "    try:\n"
            "        yield 'x'\n"
            "    finally:\n"
            "        if worker_thread.is_alive():\n"
            "            worker_thread.join(timeout=5.0)\n"
            "        if worker_thread.is_alive():\n"
            "            lingering = worker_thread\n"
            "            def _drain():\n"
            "                lingering.join()\n"
            "                cleanup()\n"
            "            threading.Thread(target=_drain, daemon=True).start()\n"
        )
        (gen_node,) = _find_generator_functions(tree)
        assert find_unbounded_thread_join_in_generator_scope(gen_node) == []

    def test_ignores_string_join_calls(self):
        """str.join(...) must never be mistaken for Thread.join(...) --
        only names bound from a Thread(...) call are tracked."""
        tree = ast.parse(
            "def generate():\n"
            "    parts = ['a', 'b']\n"
            "    yield ''.join(parts)\n"
        )
        (gen_node,) = _find_generator_functions(tree)
        assert find_unbounded_thread_join_in_generator_scope(gen_node) == []

    def test_ignores_join_on_name_never_bound_from_thread(self):
        tree = ast.parse(
            "def generate():\n"
            "    worker_thread = get_some_other_object()\n"
            "    yield 'x'\n"
            "    worker_thread.join()\n"
        )
        (gen_node,) = _find_generator_functions(tree)
        assert find_unbounded_thread_join_in_generator_scope(gen_node) == []


# ===========================================================================
# Scope sanity: make sure the scan is actually looking at something.
# ===========================================================================


def test_generator_function_finder_matches_the_survey():
    """If the router layout moves or a generator's shape changes such that
    ``_find_generator_functions`` stops finding it, contract 4's test above
    would pass vacuously. Pin the survey's count directly: exactly 5
    streaming generators across library.py (2), rag.py (2), research.py
    (1) -- see the module docstring."""
    counts = {}
    for path in SCANNED_FILES:
        tree = _parse(path)
        n = sum(1 for _ in _find_generator_functions(tree))
        if n:
            counts[path.name] = n

    assert counts == {"library.py": 2, "rag.py": 2, "research.py": 1}, counts