~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
# allow: no-sut-import — a guardian test over the ROUTER SOURCE, not its behaviour.
# It parses the router modules with ast/ruff precisely because the defects it
# catches (undefined Flask names) are invisible to importing or calling them:
# the module imports fine and only raises when the line is reached.
"""No Flask idioms may survive in the FastAPI routers.
This is the migration's most expensive defect class, and it keeps coming back
through merges: `main` is still a Flask codebase, so every merge drags Flask
code into files that have no Flask imports. Git resolves it cleanly, Python
imports fine, and the call only fails when the line is *reached*.
The instance that motivated this guard: `followup.py` gained main's per-user
concurrency admission control, which returns `jsonify({...}), 429`. `jsonify`
is not imported there. Both call sites sat inside `except Exception:` blocks,
so the `NameError`:
* turned the intended 429 "at research capacity" into a generic 500, and
* on the post-commit recheck path, was swallowed by an *inner* handler that
logged a warning and let execution fall through to start the research
anyway -- after its tracking rows had just been deleted and committed. The
per-user cap was bypassed in exactly the race it exists to close, and the
client was told "success".
Neither showed up in the suite: the whole-surface smoke test denylists
`/api/followup/start` because it starts real background work.
A grep-based guard is the right shape here precisely because the defect is
invisible to import checks and to any test that does not reach the line.
"""
import ast
import pathlib
import pytest
ROUTERS = sorted(
(
pathlib.Path(__file__).resolve().parents[3]
/ "src"
/ "local_deep_research"
/ "web"
/ "routers"
).glob("*.py")
)
# Flask names with no Starlette/FastAPI meaning. Reaching one is a NameError
# (it is not imported) or an AttributeError (Starlette's Request has no such
# member) -- in both cases a 500, or worse when swallowed.
FORBIDDEN_CALLS = {
"jsonify": "return JSONResponse(payload, status_code=...)",
"url_for": "use request.url_for(...) or a literal path",
"abort": "raise HTTPException(status_code=...)",
"make_response": "return a Response/JSONResponse directly",
"send_from_directory": "use FileResponse",
"flash": "use the app's own flash(request, ...) helper",
}
# Flask module-level globals. These are *bare names*, not calls or attributes,
# so neither check below would see them -- which is exactly how
# `session.get("username")` survived in rag.py's embeddings-policy closure and
# raised NameError on every call, permanently emptying the model dropdown.
FORBIDDEN_BARE_NAMES = {
"session": "request.session",
"flask_session": "request.session",
"g": "request.state",
"current_app": "request.app",
}
# `request.<attr>` members that exist on a Flask request and not on Starlette's.
FORBIDDEN_REQUEST_ATTRS = {
"args": "request.query_params",
"get_json": "await request.json()",
"is_json": "check the content-type header",
"remote_addr": "request.client.host",
"view_args": "request.path_params",
"endpoint": "request.scope['endpoint']",
}
def _module_defines_or_imports(tree: ast.AST, name: str) -> bool:
"""True if `name` is imported or defined in this module.
Keeps the guard honest: a router that legitimately defines its own
helper called e.g. `flash` is not using the Flask one.
"""
for node in ast.walk(tree):
if isinstance(node, ast.ImportFrom):
if any(a.asname == name or a.name == name for a in node.names):
return True
elif isinstance(node, ast.Import):
if any(
(a.asname or a.name.split(".")[0]) == name for a in node.names
):
return True
elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
if node.name == name:
return True
elif isinstance(node, ast.Assign):
for t in node.targets:
if isinstance(t, ast.Name) and t.id == name:
return True
return False
@pytest.mark.parametrize("path", ROUTERS, ids=lambda p: p.name)
def test_router_has_no_unresolvable_flask_calls(path):
tree = ast.parse(path.read_text())
bad = []
for node in ast.walk(tree):
if not isinstance(node, ast.Call) or not isinstance(
node.func, ast.Name
):
continue
name = node.func.id
if name in FORBIDDEN_CALLS and not _module_defines_or_imports(
tree, name
):
bad.append(
f"{path.name}:{node.lineno} calls Flask's {name}() -- "
f"{FORBIDDEN_CALLS[name]}"
)
assert not bad, "Flask idiom(s) reachable at runtime:\n " + "\n ".join(
bad
)
@pytest.mark.parametrize("path", ROUTERS, ids=lambda p: p.name)
def test_router_uses_no_flask_only_request_attrs(path):
tree = ast.parse(path.read_text())
bad = []
for node in ast.walk(tree):
if not isinstance(node, ast.Attribute):
continue
value = node.value
if not isinstance(value, ast.Name) or value.id != "request":
continue
if node.attr in FORBIDDEN_REQUEST_ATTRS:
bad.append(
f"{path.name}:{node.lineno} uses request.{node.attr} -- "
f"Starlette has no such member; use "
f"{FORBIDDEN_REQUEST_ATTRS[node.attr]}"
)
assert not bad, "Flask-only request member(s):\n " + "\n ".join(bad)
def test_no_undefined_names_in_web_package():
"""No undefined name may survive anywhere in the web package.
This is the `session.get("username")` bug in rag.py: a leftover Flask
global, undefined in a FastAPI module, sitting inside a policy closure
wrapped in `except Exception`. It raised NameError on every call and the
handler failed closed, so the embeddings model dropdown was permanently
empty for every user, with nothing in any log tied to a user report.
Delegated to ruff's F821 rather than an AST walk here: distinguishing a
Flask global from a legitimate local (`with get_user_db_session(...) as
session:` appears all over these routers) needs real scope analysis, and
a hand-rolled version produces false positives on exactly that idiom.
F821 also generalises -- it catches any undefined name, not only the
Flask ones we thought to enumerate.
Note the routers carry a blanket `# ruff: noqa: F811, F841`, which does
NOT cover F821; this check is therefore not redundant with that
suppression.
"""
import subprocess
root = pathlib.Path(__file__).resolve().parents[3]
result = subprocess.run(
[
"ruff",
"check",
"--select",
"F821",
"--no-cache",
"--output-format",
"concise",
str(root / "src" / "local_deep_research" / "web"),
],
capture_output=True,
text=True,
cwd=root,
)
if result.returncode not in (0, 1):
pytest.skip(f"ruff unavailable: {result.stderr.strip()[:200]}")
hits = [ln for ln in result.stdout.splitlines() if "F821" in ln]
assert not hits, "undefined name(s) in the web package:\n " + "\n ".join(
hits
)
def test_the_guard_catches_the_rag_session_regression():
"""The exact shape that slipped past the call/attribute checks."""
sample = ast.parse(
"def closure(snapshot):\n"
" return snapshot.get('_username') or session.get('username')\n"
)
hits = [
n
for n in ast.walk(sample)
if isinstance(n, ast.Name)
and isinstance(n.ctx, ast.Load)
and n.id in FORBIDDEN_BARE_NAMES
and not _module_defines_or_imports(sample, n.id)
]
assert len(hits) == 1
def test_the_guard_would_actually_catch_the_followup_regression():
"""Pin that the guard detects the real shape it was written for, so a
future refactor cannot quietly turn it into a no-op."""
sample = ast.parse(
"from fastapi.responses import JSONResponse\n"
"def handler():\n"
" return jsonify({'a': 1}), 429\n"
)
hits = [
n
for n in ast.walk(sample)
if isinstance(n, ast.Call)
and isinstance(n.func, ast.Name)
and n.func.id in FORBIDDEN_CALLS
and not _module_defines_or_imports(sample, n.func.id)
]
assert len(hits) == 1