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.

3.4 KB
Raw Blame
"""Inbound Socket.IO / WebSocket message size is bounded explicitly.

Clients send only tiny ``subscribe_to_research`` /
``unsubscribe_from_research`` payloads and engine.io ping/pong frames, so
the server caps inbound messages well below uvicorn's 16 MiB WebSocket
default and engine.io's 1 MB buffer default:

* engine.io's ``max_http_buffer_size`` (app level, also bounds polling);
* uvicorn's ``ws_max_size`` on every launch path (transport level, rejects
  an oversized frame before it is buffered).

``web/app.py`` repeats the limit as a literal (it must not import the app
before uvicorn starts), so these tests keep the copies in agreement.
"""

import ast
import json
from pathlib import Path

from local_deep_research.web import app as web_app_module
from local_deep_research.web.services import socketio_asgi as sio_mod

REPO_ROOT = Path(__file__).resolve().parents[3]
ZAP_TARGET = REPO_ROOT / "scripts" / "ci" / "zap_api_target.py"
UI_TEST_SERVER = (
    REPO_ROOT / "tests" / "ui_tests" / "research_completion_server.py"
)


def _uvicorn_run_kwargs(path: Path) -> dict[str, ast.expr]:
    tree = ast.parse(path.read_text(encoding="utf-8"))
    calls = [
        node
        for node in ast.walk(tree)
        if isinstance(node, ast.Call)
        and ast.unparse(node.func) == "uvicorn.run"
    ]
    assert len(calls) == 1, (
        f"expected exactly one uvicorn.run() call in {path}, found {len(calls)}"
    )
    return {kw.arg: kw.value for kw in calls[0].keywords if kw.arg}


def _literal_ws_max_size(path: Path) -> int:
    kwargs = _uvicorn_run_kwargs(path)
    assert "ws_max_size" in kwargs, (
        f"uvicorn.run() in {path} no longer passes ws_max_size; uvicorn "
        "would fall back to its 16 MiB default WebSocket message size"
    )
    value = kwargs["ws_max_size"]
    assert isinstance(value, ast.Constant) and isinstance(value.value, int), (
        f"ws_max_size in {path} is not an int literal: {ast.unparse(value)}"
    )
    return value.value


def test_limit_is_small_and_explicit():
    limit = sio_mod.MAX_INBOUND_MESSAGE_BYTES
    assert isinstance(limit, int)
    # Far below uvicorn's 16 MiB default and engine.io's 1 MB default.
    assert 0 < limit <= 64 * 1024


def test_limit_fits_the_real_client_messages():
    """The largest message a client sends must fit with wide headroom."""
    # Socket.IO EVENT packet framing ("42" + JSON array) around the
    # subscribe payload with a generously long research id.
    payload = json.dumps(["subscribe_to_research", {"research_id": "x" * 256}])
    packet = "42" + payload
    assert len(packet.encode("utf-8")) * 16 < sio_mod.MAX_INBOUND_MESSAGE_BYTES


def test_engineio_buffer_uses_the_limit():
    """App-level check (also covers the long-polling transport)."""
    assert (
        sio_mod.sio.eio.max_http_buffer_size
        == sio_mod.MAX_INBOUND_MESSAGE_BYTES
    )


def test_production_launcher_sets_transport_limit():
    path = Path(web_app_module.__file__).resolve()
    assert _literal_ws_max_size(path) == sio_mod.MAX_INBOUND_MESSAGE_BYTES


def test_zap_ci_target_mirrors_transport_limit():
    assert ZAP_TARGET.is_file(), f"missing {ZAP_TARGET}"
    assert _literal_ws_max_size(ZAP_TARGET) == sio_mod.MAX_INBOUND_MESSAGE_BYTES


def test_ui_test_server_mirrors_transport_limit():
    assert UI_TEST_SERVER.is_file(), f"missing {UI_TEST_SERVER}"
    assert (
        _literal_ws_max_size(UI_TEST_SERVER)
        == sio_mod.MAX_INBOUND_MESSAGE_BYTES
    )