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.

46.3 KB
Raw Blame
"""The real-time push layer, driven with two real users over real transports.

WHAT LAYER THIS FILE DRIVES
---------------------------
Everything here runs through the **real ASGI application**
(``local_deep_research.web.fastapi_app.app``) with **no production
function patched out**:

* the Socket.IO tests speak the real **Engine.IO v4 polling protocol** to
  the real ``socket_app`` mounted at ``/ws``, inside a
  ``with TestClient(app)`` block so the app's own lifespan runs and
  ``socketio_asgi.set_main_loop`` / ``init_lock`` bind to the portal's
  event loop exactly as they do under uvicorn. Packets are read back off
  the wire rather than asserted against a mocked ``sio.emit``;
* the SSE / streaming tests are ordinary HTTP requests to the real
  routers;
* both users are registered through the real ``POST /auth/register`` and
  ``POST /auth/login``, so each has its own SQLCipher database, its own
  server-side session and its own cookie jar. ``_owns_research_sync``
  runs against those real databases.

WHY THIS FILE EXISTS ALONGSIDE THE OTHER SOCKET TESTS
-----------------------------------------------------
``tests/web/services/test_socketio_*`` and
``tests/security/test_socket_ownership_edges_fastapi.py`` cover the
handshake gate and the ownership gate thoroughly -- but every one of them
drives **one** socket at a time, and the ones that assert on *delivery*
(``test_subscription_owner_scoping.py``,
``test_socketio_asgi_user_scoping.py``) do it against a mocked
``sio.emit`` with hand-populated ``_subscriptions`` dicts. Before this file,
the suite had not kept **two different users' sockets open at the same
instant** and then checked what each one receives on the wire. That is the
question this file answers.

Likewise, ``grep`` over ``tests/`` for the SSE routes found only framing,
header and middleware-buffering contracts
(``test_sse_response_headers.py``, ``test_streaming_and_sse_contracts.py``,
``test_streaming_contracts.py``). Before this file, no test asked whether a
stream authenticates or whether user B can read the *contents* of a stream
keyed by one of user A's ids.

EVERY NEGATIVE HAS A CONTROL
----------------------------
"B received nothing" is free if B's channel is dead, if the emitter is
broken, or if the id was never subscribed. So every isolation assertion
in this file is preceded, on the identical code path, by:

* proof that B's receive channel is live -- B's refused subscribe comes
  back as a real ``subscribe_error`` packet read off B's own poll, in the
  same connection whose silence is asserted afterwards; and
* proof that the emitter delivers -- A receives the very same event, with
  the very same unique marker payload, through the very same
  ``emit_to_subscribers`` call.

For the streams, B's 404 is paired with A's 200 carrying a unique marker
string, so "B did not see the marker" cannot pass because the marker was
never in any stream to begin with.
"""

from __future__ import annotations

import json
import time
import uuid

import pytest
from fastapi.testclient import TestClient

from local_deep_research.web.services import socketio_asgi

TEST_PASSWORD = "RealtimeChanPass123!"  # noqa: S105

# Engine.IO v4 packet separator for a multi-packet polling payload.
_EIO_SEP = "\x1e"


# ---------------------------------------------------------------------------
# Real-user harness (same recipe as tests/security/test_two_user_attack_
# simulation.py -- register through the real endpoints, unique
# X-Forwarded-For per client so the two registrations do not share
# slowapi's per-IP bucket).
# ---------------------------------------------------------------------------


def _new_client(app) -> TestClient:
    client = TestClient(app, raise_server_exceptions=False)
    octet_a = uuid.uuid4().int % 254 + 1
    octet_b = uuid.uuid4().int % 254 + 1
    client.headers.update({"X-Forwarded-For": f"10.{octet_a}.{octet_b}.1"})
    return client


def _csrf(client: TestClient) -> str:
    client.get("/auth/login")
    resp = client.get("/auth/csrf-token")
    return resp.json().get("csrf_token", "") if resp.status_code == 200 else ""


def _drop_stale_csrf_header(client: TestClient) -> None:
    for name in ("X-CSRFToken", "X-CSRF-Token"):
        client.headers.pop(name, None)


def _register_and_login(client: TestClient, username: str) -> None:
    _drop_stale_csrf_header(client)
    resp = client.post(
        "/auth/register",
        data={
            "username": username,
            "password": TEST_PASSWORD,
            "confirm_password": TEST_PASSWORD,
            "acknowledge": "true",
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )
    assert resp.status_code == 302, (
        f"register failed for {username!r}: "
        f"{resp.status_code} {resp.text[:300]}"
    )
    _drop_stale_csrf_header(client)
    resp = client.post(
        "/auth/login",
        data={
            "username": username,
            "password": TEST_PASSWORD,
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )
    assert resp.status_code == 302, (
        f"login failed for {username!r}: {resp.status_code} {resp.text[:300]}"
    )
    token = client.get("/auth/csrf-token")
    if token.status_code == 200:
        client.headers.update({"X-CSRFToken": token.json()["csrf_token"]})


class _User:
    __slots__ = ("client", "name")

    def __init__(self, client: TestClient, name: str):
        self.client = client
        self.name = name

    @property
    def session_cookie(self) -> str:
        value = self.client.cookies.get("session")
        assert value, f"{self.name} has no session cookie after login"
        return value


@pytest.fixture
def two_users(app):
    """Two genuinely separate principals: own DB, own session, own jar."""
    users = []
    for prefix in ("rtc_a", "rtc_b"):
        name = f"{prefix}_{uuid.uuid4().hex[:10]}"
        client = _new_client(app)
        _register_and_login(client, name)
        users.append(_User(client, name))
    return users[0], users[1]


@pytest.fixture(autouse=True)
def _isolated_socketio_state():
    """Snapshot/restore socketio_asgi's module globals.

    ``_sid_users`` / ``_sid_sessions`` / ``_subscriptions`` / ``_lock`` /
    ``_main_loop`` are module-level and shared with every other socket
    test file in the run, and ``_lock`` must not be left bound to this
    file's (now-closed) TestClient portal loop. Same contract as the
    fixture of the same name in
    ``tests/web/services/test_socketio_real_websocket_transport.py``.
    """
    saved = (
        dict(socketio_asgi._sid_users),
        dict(socketio_asgi._sid_sessions),
        {k: set(v) for k, v in socketio_asgi._subscriptions.items()},
        socketio_asgi._lock,
        socketio_asgi._main_loop,
    )
    socketio_asgi._sid_users.clear()
    socketio_asgi._sid_sessions.clear()
    socketio_asgi._subscriptions.clear()
    socketio_asgi._lock = None
    yield
    users, sessions, subs, lock, loop = saved
    socketio_asgi._sid_users.clear()
    socketio_asgi._sid_users.update(users)
    socketio_asgi._sid_sessions.clear()
    socketio_asgi._sid_sessions.update(sessions)
    socketio_asgi._subscriptions.clear()
    socketio_asgi._subscriptions.update(subs)
    socketio_asgi._lock = lock
    socketio_asgi._main_loop = loop


def _seed_research(username: str, log_messages: list[str] | None = None) -> str:
    """Insert a real ResearchHistory row (+ optional log rows) into a
    user's real SQLCipher database and return its UUID.

    Deliberately NOT ``POST /api/start_research``: that spawns a real
    background worker whose teardown calls
    ``remove_subscriptions_for_research`` when the run dies, which races
    the subscribe under test and made an earlier draft of this file flaky
    (``_subscriptions`` observed empty right after a successful
    subscribe). The row, the database and every ownership check against
    them are just as real this way; only the racing worker is gone.
    """
    from datetime import datetime, timedelta, timezone

    from local_deep_research.database.models import (
        ResearchHistory,
        ResearchLog,
    )
    from local_deep_research.database.session_context import (
        get_user_db_session,
    )

    rid = str(uuid.uuid4())
    base_time = datetime(2025, 1, 1, tzinfo=timezone.utc)
    with get_user_db_session(username) as db_session:
        db_session.add(
            ResearchHistory(
                id=rid,
                query=f"realtime channel probe {uuid.uuid4().hex[:8]}",
                mode="quick",
                status="completed",
                created_at="2025-01-01T00:00:00+00:00",
            )
        )
        # Flush before the child rows: ResearchLog.research_id carries a real
        # FOREIGN KEY to research_history.id and SQLCipher enforces it, so an
        # unflushed parent makes the log inserts fail with IntegrityError.
        db_session.flush()
        for i, message in enumerate(log_messages or []):
            db_session.add(
                ResearchLog(
                    research_id=rid,
                    timestamp=base_time + timedelta(minutes=i),
                    message=message,
                    module="realtime_channel_isolation_test",
                    function="seed",
                    line_no=i,
                    level="INFO",
                )
            )
        db_session.commit()
    return rid


def _wait_until(predicate, timeout: float = 5.0, interval: float = 0.02):
    """Poll ``predicate`` from the test thread until truthy.

    ``on_subscribe`` runs as a coroutine on the app's event loop and its
    success path sends NO reply packet, so there is nothing to block a
    poll on -- module state is the only externally observable completion
    signal for a successful subscribe.
    """
    deadline = time.monotonic() + timeout
    while time.monotonic() < deadline:
        if predicate():
            return True
        time.sleep(interval)
    return predicate()


# ---------------------------------------------------------------------------
# Real Engine.IO v4 polling client. No socketio client library: the packets
# are written and read verbatim so the assertions are about what actually
# crossed the wire.
# ---------------------------------------------------------------------------


class EioSession:
    """One real Engine.IO polling session against the mounted /ws app."""

    def __init__(self, client: TestClient, cookie: str | None):
        self.client = client
        self.cookie = cookie
        self.eio_sid: str | None = None
        self.sio_sid: str | None = None
        self.refusal: str | None = None

    def _with_cookie(self):
        # Engine.IO polling is stateless per request (the session lives in
        # the ``sid`` query param), so one TestClient -- and therefore ONE
        # event loop, which the shared asyncio.Lock in socketio_asgi
        # requires -- can carry both users' sessions by swapping the
        # cookie for the handshake request.
        # clear() first: httpx's jar keys cookies by (domain, path, name)
        # and a second `set("session", ...)` leaves BOTH in the jar, so the
        # request carries two `session=` cookies and http.cookies' SimpleCookie
        # keeps whichever came first -- i.e. user B's handshake would silently
        # authenticate as user A. Observed while building this file.
        self.client.cookies.clear()
        if self.cookie is not None:
            self.client.cookies.set("session", self.cookie)

    def open(self) -> str:
        """Run the real handshake. Returns the CONNECT ack/refusal packet."""
        self._with_cookie()
        r_open = self.client.get("/ws/socket.io/?EIO=4&transport=polling")
        assert r_open.status_code == 200, r_open.text[:200]
        self.eio_sid = json.loads(r_open.text[1:])["sid"]
        r_connect = self.client.post(
            f"/ws/socket.io/?EIO=4&transport=polling&sid={self.eio_sid}",
            content="40",
            headers={"Content-Type": "text/plain"},
        )
        assert r_connect.status_code == 200, r_connect.text[:200]
        ack = self.poll_raw()
        if ack.startswith("40"):
            self.sio_sid = json.loads(ack[2:])["sid"]
        else:
            self.refusal = ack
        return ack

    def emit(self, event: str, payload) -> None:
        self.client.post(
            f"/ws/socket.io/?EIO=4&transport=polling&sid={self.eio_sid}",
            content="42" + json.dumps([event, payload]),
            headers={"Content-Type": "text/plain"},
        )

    def poll_raw(self) -> str:
        r = self.client.get(
            f"/ws/socket.io/?EIO=4&transport=polling&sid={self.eio_sid}"
        )
        assert r.status_code == 200, f"{r.status_code} {r.text[:200]}"
        return r.text

    def poll_events(self) -> list:
        """One real long-poll, decoded into ``[name, payload]`` pairs.

        With no data pending, engine.io holds the poll open until its
        5s ping interval and then answers with a bare ``2`` (PING) --
        which is exactly what a "received nothing" assertion needs to
        see, and is why the negative polls in this file cost ~5s each.
        """
        events = []
        for packet in self.poll_raw().split(_EIO_SEP):
            if packet.startswith("42"):
                events.append(json.loads(packet[2:]))
        return events


# ---------------------------------------------------------------------------
# 1. Two real users, two live sockets, one research: who receives what.
# ---------------------------------------------------------------------------


def test_two_live_sockets_only_the_owner_receives_the_research_event(
    app, two_users
):
    """THE cross-user question, driven end to end on the real wire.

    A and B are two registered users with separate encrypted databases.
    Both hold an open, authenticated Socket.IO session at the same
    instant. A owns research ``rid``; B is given ``rid`` and tries to
    subscribe to it.

    Four assertions, in this order, so no negative can pass vacuously:

    1. CONTROL -- B's socket is genuinely connected and its receive
       channel genuinely delivers: B's refused subscribe comes back to B
       as a real ``subscribe_error`` packet, read off B's own poll.
    2. A's subscribe is accepted (observed in ``_subscriptions``, keyed
       ``(owner, research_id)``).
    3. CONTROL -- the emitter really delivers: one
       ``emit_to_subscribers(..., owner=A)`` call puts a uniquely
       marked payload on A's poll.
    4. ISOLATION -- the same call put nothing on B's poll: B's next
       long-poll carries no ``42`` event frames at all, only engine.io's
       bare PING.
    """
    user_a, user_b = two_users
    rid = _seed_research(user_a.name)
    marker = f"leak-marker-{uuid.uuid4().hex}"

    with TestClient(app, raise_server_exceptions=False) as ws_client:
        # The app's real lifespan just ran in this client's portal, so
        # socketio_asgi._main_loop / ._lock are bound to the same loop
        # that will serve the handshakes below -- exactly as under uvicorn.
        assert socketio_asgi._lock is not None, (
            "lifespan did not run init_lock(); the socket handlers would "
            "fail on `async with _lock` and every assertion below would be "
            "about the wrong thing"
        )

        sess_a = EioSession(ws_client, user_a.session_cookie)
        ack_a = sess_a.open()
        assert ack_a.startswith("40"), f"A's handshake was refused: {ack_a!r}"

        sess_b = EioSession(ws_client, user_b.session_cookie)
        ack_b = sess_b.open()
        assert ack_b.startswith("40"), f"B's handshake was refused: {ack_b!r}"

        assert socketio_asgi._sid_users.get(sess_a.sio_sid) == user_a.name
        assert socketio_asgi._sid_users.get(sess_b.sio_sid) == user_b.name
        assert sess_a.sio_sid != sess_b.sio_sid

        # --- (1) CONTROL + attack: B subscribes to A's research id.
        sess_b.emit("subscribe_to_research", {"research_id": rid})
        b_events = sess_b.poll_events()
        assert b_events, (
            "B's poll returned no event frames at all -- B's receive "
            "channel is not proven live, so the silence asserted in (4) "
            "would be meaningless"
        )
        assert b_events[0][0] == "subscribe_error", b_events
        assert b_events[0][1]["error"] == "Not authorized", b_events
        assert b_events[0][1]["research_id"] == rid, b_events
        assert (user_b.name, rid) not in socketio_asgi._subscriptions

        # --- (2) A subscribes to their own research.
        sess_a.emit("subscribe_to_research", {"research_id": rid})
        assert _wait_until(
            lambda: (
                sess_a.sio_sid
                in socketio_asgi._subscriptions.get((user_a.name, rid), set())
            )
        ), (
            f"A's own subscribe never landed; _subscriptions="
            f"{socketio_asgi._subscriptions!r}"
        )

        # --- (3) CONTROL: one emit, read back off A's real poll.
        from local_deep_research.web.services.socketio_asgi import (
            emit_to_subscribers,
        )

        emit_to_subscribers(
            "research_progress",
            rid,
            {"progress": 42, "message": marker},
            owner=user_a.name,
        )
        a_events = sess_a.poll_events()
        assert a_events, (
            "the owner received NOTHING -- the emitter is broken, so the "
            "isolation assertion below would pass for the wrong reason"
        )
        assert a_events[0][0] == f"research_progress_{rid}", a_events
        assert a_events[0][1]["message"] == marker, a_events

        # --- (4) ISOLATION: the same emit reached nothing of B's.
        b_after = sess_b.poll_raw()
        leaked = [p for p in b_after.split(_EIO_SEP) if p.startswith("42")]
        assert leaked == [], (
            f"B received event frames from A's research emit: {leaked!r}"
        )
        assert marker not in b_after, (
            f"A's payload marker appeared on B's socket: {b_after!r}"
        )


# ---------------------------------------------------------------------------
# 2. The case where isolation is NOT free: two users who BOTH legitimately
#    own the same subscription id.
# ---------------------------------------------------------------------------


def _seed_benchmark_run(username: str, run_id: int, name: str) -> None:
    """Insert a BenchmarkRun with an explicit id into a user's real DB.

    ``BenchmarkRun.id`` is an autoincrementing INTEGER inside each user's
    OWN encrypted database, so "run 1" exists, legitimately, for every
    user on the instance. That is the collision the subscription map has
    to survive.
    """
    from local_deep_research.database.models.benchmark import (
        BenchmarkRun,
        BenchmarkStatus,
    )
    from local_deep_research.database.session_context import (
        get_user_db_session,
    )

    with get_user_db_session(username) as db_session:
        db_session.add(
            BenchmarkRun(
                id=run_id,
                run_name=name,
                config_hash="deadbeefdeadbeef",
                query_hash_list=[],
                search_config={},
                evaluation_config={},
                datasets_config={},
                status=BenchmarkStatus.IN_PROGRESS,
            )
        )
        db_session.commit()


def test_same_benchmark_id_owned_by_both_users_still_does_not_cross(
    app, two_users
):
    """Both users pass the ownership gate for the SAME id -- and still
    receive only their own run's events.

    This is the one scenario where "B received nothing" is a real result
    rather than an accident of B never having subscribed. ``BenchmarkRun.id``
    autoincrements inside each user's own encrypted database, so both A
    and B genuinely own a run with id ``1``; ``_owns_research_sync`` says
    yes to both, correctly, and both sids end up subscribed to the same
    id at the same time. Only the ``(owner, research_id)`` key in
    ``_subscriptions`` separates them -- a map keyed by the bare id would
    put both sids in one set and deliver each user's benchmark progress
    to the other's browser.

    Controls: BOTH subscriptions are asserted present in the map before
    any emit, and BOTH directions are emitted -- A's marker must reach A
    and not B, AND B's marker must reach B. The reverse emit is what
    proves B's subscription is live and would have delivered A's event
    had the key been the id alone.
    """
    user_a, user_b = two_users
    run_id = 1
    _seed_benchmark_run(user_a.name, run_id, "A-run")
    _seed_benchmark_run(user_b.name, run_id, "B-run")

    marker_a = f"a-only-{uuid.uuid4().hex}"
    marker_b = f"b-only-{uuid.uuid4().hex}"

    with TestClient(app, raise_server_exceptions=False) as ws_client:
        sess_a = EioSession(ws_client, user_a.session_cookie)
        assert sess_a.open().startswith("40")
        sess_b = EioSession(ws_client, user_b.session_cookie)
        assert sess_b.open().startswith("40")

        sess_a.emit("subscribe_to_research", {"research_id": run_id})
        sess_b.emit("subscribe_to_research", {"research_id": run_id})

        # CONTROL: both subscribes were ACCEPTED. The gate is supposed to
        # say yes to both -- if it said no to either, the isolation
        # assertion below would be about an absent subscription instead of
        # a live one.
        assert _wait_until(
            lambda: (
                sess_a.sio_sid
                in socketio_asgi._subscriptions.get(
                    (user_a.name, run_id), set()
                )
            )
        ), f"A never subscribed: {socketio_asgi._subscriptions!r}"
        assert _wait_until(
            lambda: (
                sess_b.sio_sid
                in socketio_asgi._subscriptions.get(
                    (user_b.name, run_id), set()
                )
            )
        ), f"B never subscribed: {socketio_asgi._subscriptions!r}"

        from local_deep_research.web.services.socketio_asgi import (
            emit_to_subscribers,
        )

        emit_to_subscribers(
            "benchmark_progress", run_id, {"note": marker_a}, owner=user_a.name
        )
        a_events = sess_a.poll_events()
        assert a_events and a_events[0][1].get("note") == marker_a, (
            f"owner A did not receive their own run's event: {a_events!r}"
        )

        # ISOLATION: B is subscribed to the very same id, on the very same
        # event name, and got nothing from A's emit.
        b_raw = sess_b.poll_raw()
        assert marker_a not in b_raw, (
            f"A's benchmark run {run_id} leaked to B: {b_raw!r}"
        )

        # REVERSE CONTROL: B's subscription really is live -- an emit
        # owned by B, on that same event name and id, does reach B.
        emit_to_subscribers(
            "benchmark_progress", run_id, {"note": marker_b}, owner=user_b.name
        )
        b_events = sess_b.poll_events()
        assert b_events and b_events[0][1].get("note") == marker_b, (
            "B's subscription to run 1 was not live, so 'B did not get A's "
            f"event' proves nothing: {b_events!r}"
        )
        assert b_events[0][0] == f"benchmark_progress_{run_id}", b_events


# ---------------------------------------------------------------------------
# 3. Revocation on an ALREADY-ESTABLISHED connection.
# ---------------------------------------------------------------------------


def test_logout_severs_the_users_already_open_socket(app, two_users):
    """A socket authorised before logout must stop receiving after it.

    A socket is authorised once, at the handshake, and its identity is
    then frozen in ``_sid_users`` for the connection's whole lifetime --
    so nothing about the emit path re-checks the session. What makes
    logout actually revoke a live socket is the explicit
    ``disconnect_session`` call in the logout handler.

    Control first: the same emit, on the same connection, before logout,
    delivers a uniquely marked payload to A. Only then is logout driven
    through the real ``POST /auth/logout``, and the second marker
    asserted absent.
    """
    user_a, _user_b = two_users
    rid = _seed_research(user_a.name)
    marker_live = f"before-logout-{uuid.uuid4().hex}"
    marker_after = f"after-logout-{uuid.uuid4().hex}"

    with TestClient(app, raise_server_exceptions=False) as ws_client:
        sess_a = EioSession(ws_client, user_a.session_cookie)
        assert sess_a.open().startswith("40")
        sess_a.emit("subscribe_to_research", {"research_id": rid})
        assert _wait_until(
            lambda: (
                sess_a.sio_sid
                in socketio_asgi._subscriptions.get((user_a.name, rid), set())
            )
        )

        from local_deep_research.web.services.socketio_asgi import (
            emit_to_subscribers,
        )

        # CONTROL: this connection demonstrably receives A's research events.
        emit_to_subscribers(
            "research_progress",
            rid,
            {"message": marker_live},
            owner=user_a.name,
        )
        events = sess_a.poll_events()
        assert events and events[0][1]["message"] == marker_live, events

        # Real logout, real route, A's real client/cookie jar.
        resp = user_a.client.post("/auth/logout", follow_redirects=False)
        assert resp.status_code in (200, 302), (
            f"{resp.status_code} {resp.text[:200]}"
        )

        assert _wait_until(
            lambda: sess_a.sio_sid not in socketio_asgi._sid_users
        ), (
            "logout did not sever the already-open socket: sid "
            f"{sess_a.sio_sid} is still in _sid_users "
            f"({socketio_asgi._sid_users!r})"
        )
        assert (user_a.name, rid) not in socketio_asgi._subscriptions, (
            "the severed socket's subscription survived logout: "
            f"{socketio_asgi._subscriptions!r}"
        )

        emit_to_subscribers(
            "research_progress",
            rid,
            {"message": marker_after},
            owner=user_a.name,
        )
        # The engine.io session is gone, so the poll is either refused
        # outright or carries nothing. Either way the marker must not
        # appear -- and the control above proves it WOULD have.
        tail = ws_client.get(
            f"/ws/socket.io/?EIO=4&transport=polling&sid={sess_a.eio_sid}"
        )
        assert marker_after not in tail.text, (
            "a post-logout emit still reached the pre-logout socket: "
            f"{tail.status_code} {tail.text[:300]!r}"
        )


def test_captured_cookie_cannot_open_a_new_socket_after_logout(app, two_users):
    """Reconnection re-authenticates; it does not replay trusted state.

    The signed session cookie stays cryptographically valid for its full
    itsdangerous window after logout. The question is whether the
    handshake trusts the signature or the server-side session record.

    Control: the EXACT SAME cookie string is used for both handshakes.
    The first one (pre-logout) must be accepted, or "the second was
    refused" would just mean the cookie never worked.
    """
    user_a, _user_b = two_users
    captured_cookie = user_a.session_cookie

    with TestClient(app, raise_server_exceptions=False) as ws_client:
        before = EioSession(ws_client, captured_cookie)
        ack_before = before.open()
        assert ack_before.startswith("40"), (
            f"the captured cookie did not authenticate even BEFORE logout, "
            f"so the post-logout refusal proves nothing: {ack_before!r}"
        )

        resp = user_a.client.post("/auth/logout", follow_redirects=False)
        assert resp.status_code in (200, 302), resp.status_code

        after = EioSession(ws_client, captured_cookie)
        ack_after = after.open()

    assert ack_after.startswith("44"), (
        "a captured, still-signature-valid cookie opened a BRAND NEW "
        f"authenticated socket after logout: {ack_after!r}"
    )
    assert after.sio_sid is None
    assert user_a.name not in socketio_asgi._sid_users.values(), (
        f"post-logout sids for {user_a.name}: {socketio_asgi._sid_users!r}"
    )


# ---------------------------------------------------------------------------
# 4. The OTHER real-time channel: server-sent / streamed responses.
#
# grep over tests/ for these routes found only framing, header and
# middleware-buffering contracts (test_sse_response_headers.py,
# test_streaming_and_sse_contracts.py, test_streaming_contracts.py).
# Nothing asked whether a stream authenticates, or whether the CONTENTS
# of a stream keyed by one of user A's ids are reachable by user B.
# ---------------------------------------------------------------------------

# Every streaming route in web/routers (StreamingResponse with an SSE or
# NDJSON media type), by (method, path template). Kept explicit rather
# than discovered so a route that stops streaming shows up as a failure
# here rather than silently dropping out of the sweep.
_STREAM_ROUTES = [
    ("GET", "/api/research/{rid}/logs/export"),
    ("POST", "/library/api/rag/index-all"),
    ("POST", "/library/api/collections/{cid}/index"),
    ("POST", "/library/api/download-all-text"),
    ("POST", "/library/api/download-bulk"),
]


def test_the_stream_route_inventory_still_matches_the_source(app):
    """Guard for the sweep below: if a streaming route is added or
    renamed, this fails instead of the sweep silently shrinking."""
    import ast
    from pathlib import Path

    import local_deep_research

    routers = Path(local_deep_research.__file__).parent / "web" / "routers"
    found = set()
    for path in sorted(routers.glob("*.py")):
        tree = ast.parse(path.read_text(encoding="utf-8"))
        streaming_fns = set()
        for node in ast.walk(tree):
            if not isinstance(node, ast.Call):
                continue
            fn = node.func
            if getattr(fn, "id", None) not in {
                "StreamingResponse",
                "WorkerCleanupStreamingResponse",
            }:
                continue
            media = next(
                (
                    kw.value.value
                    for kw in node.keywords
                    if kw.arg == "media_type"
                    and isinstance(kw.value, ast.Constant)
                ),
                None,
            )
            if media in ("text/event-stream", "application/x-ndjson"):
                streaming_fns.add(node.lineno)
        if not streaming_fns:
            continue
        for node in ast.walk(tree):
            if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
                continue
            if not any(
                node.lineno <= ln <= (node.end_lineno or node.lineno)
                for ln in streaming_fns
            ):
                continue
            for dec in node.decorator_list:
                target = dec.func if isinstance(dec, ast.Call) else dec
                if not isinstance(target, ast.Attribute):
                    continue
                if target.attr not in ("get", "post", "put", "delete", "head"):
                    continue
                if isinstance(dec, ast.Call) and dec.args:
                    route = dec.args[0]
                    if isinstance(route, ast.Constant):
                        found.add((target.attr.upper(), route.value))

    # Normalise the router-relative paths to the inventory's shape.
    normalised = set()
    for method, route in found:
        route = route.replace("{research_id}", "{rid}").replace(
            "{collection_id}", "{cid}"
        )
        if (
            route.startswith("/api/rag")
            or route.startswith("/api/collections")
            or route.startswith("/api/download")
        ):
            route = "/library" + route
        # HEAD on the log export is the same handler as GET (registered
        # twice so the log panel's pre-flight does not 405); the sweep
        # exercises the GET.
        if method == "HEAD":
            continue
        normalised.add((method, route))

    assert normalised == {(m, p) for m, p in _STREAM_ROUTES}, (
        f"streaming route inventory drifted: {sorted(normalised)}"
    )


def test_no_streaming_route_serves_an_unauthenticated_client(app, two_users):
    """Every stream refuses an anonymous caller.

    Control: the identical request, on the identical path, made by a
    logged-in user does NOT get the auth refusal -- so the 401s below are
    the auth gate firing, not a route that is broken or absent for
    everybody. (The authenticated call may legitimately answer 404 or
    422; what it must never answer is 401/403.)
    """
    user_a, _user_b = two_users
    rid = _seed_research(user_a.name)
    anon = _new_client(app)

    # Bodies chosen so the AUTHENTICATED control returns immediately
    # instead of actually indexing or downloading anything: an empty
    # research_ids list is a 400 in download_bulk, a fresh user has no
    # downloadable resources, and collection 999999 exists for nobody.
    bodies = {
        "/library/api/download-bulk": {"research_ids": []},
        "/library/api/download-all-text": {},
    }

    refusals = {}
    control = {}
    for method, template in _STREAM_ROUTES:
        path = template.format(rid=rid, cid="999999")
        body = bodies.get(template, {})
        anon_resp = anon.request(method, path, json=body)
        refusals[(method, template)] = anon_resp.status_code

        auth_resp = user_a.client.request(method, path, json=body)
        control[(method, template)] = auth_resp.status_code

    assert all(code in (401, 403) for code in refusals.values()), (
        f"a stream served an anonymous caller: {refusals}"
    )
    assert not any(code in (401, 403) for code in control.values()), (
        "the authenticated control was ALSO refused, so the anonymous "
        f"refusals prove nothing about the auth gate: {control}"
    )


def test_user_b_cannot_read_the_contents_of_user_as_log_stream(app, two_users):
    """The NDJSON log stream is keyed by a research UUID. B has the UUID.

    Control first, on the identical path: A's own request returns 200 and
    the stream body really does carry A's private log line, verbatim. So
    when B's request for the same id comes back 404 with the marker
    nowhere in the body, that is isolation and not an empty stream.
    """
    user_a, user_b = two_users
    marker = f"private-log-{uuid.uuid4().hex}"
    rid = _seed_research(user_a.name, log_messages=[marker, "second line"])

    # CONTROL: the owner's stream carries the marker.
    a_resp = user_a.client.get(f"/api/research/{rid}/logs/export")
    assert a_resp.status_code == 200, (
        f"{a_resp.status_code} {a_resp.text[:200]}"
    )
    a_lines = [
        json.loads(line) for line in a_resp.text.splitlines() if line.strip()
    ]
    assert marker in [row["message"] for row in a_lines], (
        f"the owner's own stream did not contain the seeded marker, so the "
        f"attacker's empty stream would prove nothing: {a_resp.text[:300]!r}"
    )

    # ATTACK: same id, same route, different principal.
    b_resp = user_b.client.get(f"/api/research/{rid}/logs/export")
    assert b_resp.status_code == 404, (
        f"B read A's log stream: {b_resp.status_code} {b_resp.text[:300]}"
    )
    assert b_resp.json().get("error") == "Research not found", b_resp.text[:300]
    assert marker not in b_resp.text, (
        f"A's private log line appeared in B's response: {b_resp.text[:300]!r}"
    )


def test_user_b_cannot_read_the_contents_of_user_as_collection_sse(
    app, two_users
):
    """The RAG index SSE names the collection it is indexing.

    ``/library/api/collections/{id}/index`` streams
    ``Indexing N documents in collection: <name>`` -- a display name A
    chose -- and 'Collection not found' otherwise. Collection ids
    autoincrement inside each user's own database, so A's id is a
    perfectly plausible id in B's namespace too; only the per-user
    database keeps the two apart.

    Control: A's own stream for the same id resolves the collection (a
    ``complete``/``start`` frame), so B's ``error``/``Collection not
    found`` frame is a refusal and not a route that fails for everyone.
    """
    user_a, user_b = two_users
    name = f"collection-{uuid.uuid4().hex}"
    created = user_a.client.post(
        "/library/api/collections", json={"name": name}
    )
    assert created.status_code == 200, created.text[:300]
    cid = created.json()["collection"]["id"]

    path = f"/library/api/collections/{cid}/index"

    a_resp = user_a.client.post(path)
    assert a_resp.status_code == 200, a_resp.status_code
    a_frames = [
        json.loads(chunk.split("data: ", 1)[1])
        for chunk in a_resp.text.split("\n\n")
        if chunk.startswith("data: ")
    ]
    assert a_frames, f"A's own SSE stream was empty: {a_resp.text[:300]!r}"
    assert a_frames[-1].get("type") != "error", (
        "A's own stream could not resolve A's collection, so B's failure "
        f"below proves nothing: {a_frames!r}"
    )

    b_resp = user_b.client.post(path)
    assert b_resp.status_code == 200, b_resp.status_code
    b_frames = [
        json.loads(chunk.split("data: ", 1)[1])
        for chunk in b_resp.text.split("\n\n")
        if chunk.startswith("data: ")
    ]
    assert b_frames, f"B's SSE stream was empty: {b_resp.text[:300]!r}"
    assert b_frames[-1] == {
        "type": "error",
        "error": "Collection not found",
    }, f"B's stream resolved A's collection id: {b_frames!r}"
    assert name not in b_resp.text, (
        f"A's collection name leaked into B's stream: {b_resp.text[:300]!r}"
    )


# ---------------------------------------------------------------------------
# 5. The bounded idle-expiry revocation-latency window, executed.
#
#    Identity is captured at handshake. After idle expiry, an established
#    socket can continue receiving its owner's events until the periodic
#    sweep or its next subscribe/unsubscribe action revalidates and severs it.
#    This drives both halves of that bounded timing property; it is not an
#    unguarded cross-user path.
# ---------------------------------------------------------------------------


def _session_id_of(user: _User) -> str:
    """Read the session id out of the user's real cookie.

    Uses the production decoder rather than a re-implementation of
    Starlette's cookie format -- a hand-rolled copy would be exactly the
    "assert against your own re-implementation" trap.
    """
    data = socketio_asgi._decode_session_cookie(
        f"session={user.session_cookie}"
    )
    assert data and data.get("session_id"), (
        f"undecodable cookie for {user.name}"
    )
    return data["session_id"]


def test_idle_expired_session_keeps_receiving_until_the_socket_next_speaks(
    app, two_users
):
    """A socket whose session is already gone server-side still receives.

    This is NOT a hole in the ownership model -- the events are still the
    socket owner's OWN events, so nothing crosses between users. It is a
    revocation-latency property, and it is worth pinning because it is the
    exact question "can a client with a revoked session still receive
    events on an established connection?", and the answer is a qualified
    yes:

    * the emit path (``emit_to_subscribers`` -> ``sio.emit(room=sid)``)
      NEVER re-validates the session, so between the moment the session
      record disappears and the moment something severs the socket, that
      socket keeps receiving;
    * the three teardown paths that make this bounded are all explicit
      calls, not properties of the emit path: logout ->
      ``disconnect_session`` (driven in
      ``test_logout_severs_the_users_already_open_socket``), password
      change / idle sweep -> ``disconnect_user``
      (``auth/connection_cleanup.py::_disconnect_all_user_sockets``, at
      most one sweep interval late), and
    * the socket's own next ``subscribe``/``unsubscribe``, which
      re-validates and severs -- driven below.

    Both halves are asserted here so a future change that removes the
    re-validation gate (making the window unbounded until the sweep) or
    one that adds re-validation to the emit path (closing the window
    entirely) both show up as a failure that has to be looked at.
    """
    user_a, _user_b = two_users
    rid = _seed_research(user_a.name)
    marker_before = f"pre-expiry-{uuid.uuid4().hex}"
    marker_after = f"post-expiry-{uuid.uuid4().hex}"
    marker_final = f"post-sever-{uuid.uuid4().hex}"

    from local_deep_research.web.auth.session_manager import session_manager
    from local_deep_research.web.services.socketio_asgi import (
        emit_to_subscribers,
    )

    session_id = _session_id_of(user_a)

    with TestClient(app, raise_server_exceptions=False) as ws_client:
        sess_a = EioSession(ws_client, user_a.session_cookie)
        assert sess_a.open().startswith("40")
        sess_a.emit("subscribe_to_research", {"research_id": rid})
        assert _wait_until(
            lambda: (
                sess_a.sio_sid
                in socketio_asgi._subscriptions.get((user_a.name, rid), set())
            )
        )

        # CONTROL: the connection delivers while the session is alive.
        emit_to_subscribers(
            "research_progress",
            rid,
            {"message": marker_before},
            owner=user_a.name,
        )
        events = sess_a.poll_events()
        assert events and events[0][1]["message"] == marker_before, events

        # Idle expiry, reproduced exactly as the sweep leaves it: the
        # session record is gone from the store, and NOTHING has called
        # disconnect_user/disconnect_session yet.
        session_manager.destroy_session(session_id)
        assert session_manager.validate_session(session_id) is None
        assert sess_a.sio_sid in socketio_asgi._sid_users, (
            "destroying the session already severed the socket -- then the "
            "window this test documents does not exist and the docstring "
            "in socketio_asgi._socket_session_still_valid is stale"
        )

        # HALF ONE: the emit path does not re-validate, so the socket with
        # the dead session still receives.
        emit_to_subscribers(
            "research_progress",
            rid,
            {"message": marker_after},
            owner=user_a.name,
        )
        still = sess_a.poll_events()
        assert still and still[0][1]["message"] == marker_after, (
            "the emit path re-validated the session -- good news, but this "
            "test and the comments in socketio_asgi.py now disagree with "
            f"the code: {still!r}"
        )

        # HALF TWO: the socket's next word re-validates and severs it.
        sess_a.emit("subscribe_to_research", {"research_id": rid})
        refusal = sess_a.poll_events()
        assert refusal, "no reply to the post-expiry subscribe"
        assert refusal[0][0] == "subscribe_error", refusal
        assert refusal[0][1]["error"] == "Session expired", refusal
        assert _wait_until(
            lambda: sess_a.sio_sid not in socketio_asgi._sid_users
        ), (
            "the expired-session socket was refused but NOT severed, so it "
            "would keep receiving until the sweep: "
            f"{socketio_asgi._sid_users!r}"
        )

        emit_to_subscribers(
            "research_progress",
            rid,
            {"message": marker_final},
            owner=user_a.name,
        )
        tail = ws_client.get(
            f"/ws/socket.io/?EIO=4&transport=polling&sid={sess_a.eio_sid}"
        )
        assert marker_final not in tail.text, (
            f"the severed socket still received: {tail.text[:300]!r}"
        )


# ---------------------------------------------------------------------------
# 6. Latent future-safety invariant for owner-scoped snapshot replies.
# ---------------------------------------------------------------------------


@pytest.mark.xfail(
    strict=True,
    reason=(
        "Current _active_research writers use UUIDs. Because subscriptions "
        "also accept numeric benchmark IDs, this strict xfail pins the latent "
        "requirement that snapshot lookup become owner-scoped before numeric "
        "active-state writers are added."
    ),
)
def test_subscribe_snapshot_is_not_scoped_to_the_subscribing_owner(
    app, two_users
):
    """Future-safety check for numeric active-state identifiers.

    Current active-state writers use UUIDs. This synthetic numeric case models
    the identifier type already accepted by benchmark subscriptions: both
    users own benchmark run ``1`` in separate databases and therefore pass
    ``_owns_research_sync``. ``_subscriptions`` is owner-scoped; the snapshot
    lookup must gain the same scope before numeric active-state writers exist.

    CONTROL: user A, the owner of the global entry, subscribes first and
    is shown to receive the snapshot through the identical code path. So
    when B is asserted not to receive it, the assertion is about scoping
    and not about a snapshot reply that never fires.
    """
    from local_deep_research.web import research_state

    user_a, user_b = two_users
    run_id = 1
    _seed_benchmark_run(user_a.name, run_id, "A-run")
    _seed_benchmark_run(user_b.name, run_id, "B-run")

    private_line = f"A-private-log-{uuid.uuid4().hex}"
    research_state.set_active_research(
        run_id,
        {
            "progress": 77,
            "status": "in_progress",
            "log": [{"message": private_line, "type": "info"}],
            "settings": None,
        },
    )
    try:
        with TestClient(app, raise_server_exceptions=False) as ws_client:
            sess_a = EioSession(ws_client, user_a.session_cookie)
            assert sess_a.open().startswith("40")
            sess_a.emit("subscribe_to_research", {"research_id": run_id})
            a_events = sess_a.poll_events()
            assert a_events, (
                "the owner got no snapshot reply at all, so B's silence "
                "would prove nothing"
            )
            assert a_events[0][0] == f"research_progress_{run_id}", a_events
            assert a_events[0][1]["message"] == private_line, a_events

            sess_b = EioSession(ws_client, user_b.session_cookie)
            assert sess_b.open().startswith("40")
            sess_b.emit("subscribe_to_research", {"research_id": run_id})
            assert _wait_until(
                lambda: (
                    sess_b.sio_sid
                    in socketio_asgi._subscriptions.get(
                        (user_b.name, run_id), set()
                    )
                )
            ), "B's own subscribe was refused; the collision premise is gone"

            b_raw = sess_b.poll_raw()
    finally:
        research_state.remove_active_research(run_id)

    assert private_line not in b_raw, (
        "user B, subscribing to a benchmark id B legitimately owns, was "
        f"handed user A's live log line: {b_raw!r}"
    )