~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
#!/usr/bin/env python3
"""Pre-commit hook: Flask stays gone; werkzeug stays confined.
The Flask -> FastAPI migration (PR #3299) removed Flask entirely and
kept werkzeug ONLY as a security-utility library -- pyproject.toml
documents that exactly two imports remain (the comment above
``werkzeug~=3.1.6``). Neither invariant was machine-enforced: the
antipattern guards in tests/web/routers/test_migration_antipattern_
guards.py scan only the web/ top level and web/routers/, so a
``from flask import ...`` in any other package -- or a re-added
``flask`` dependency -- would not fail any check.
Rules:
1. No flask import anywhere under src/, including Flask ecosystem
packages (flask_wtf, flask_login, flask_socketio, flask-cors, ...).
2. No werkzeug import outside the two allowlisted security modules
named by the pyproject.toml pin comment.
3. No flask* entry among pyproject.toml's declared or optional
dependencies.
Detection is AST-based: 70+ files legitimately mention Flask in
comments and docstrings narrating the migration, which a line regex
would flag. Only files whose text contains the token are parsed, so
the full-tree scan stays fast.
"""
from __future__ import annotations
import ast
import re
import sys
import tomllib
from collections.abc import Iterator
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
# flask, flask.something, flask_something, flask-something -- but not an
# unrelated package that merely starts with those letters.
FLASK_PACKAGE_RE = re.compile(r"^flask(?:[._-]|$)")
# The only modules allowed to import werkzeug, mirroring the
# werkzeug~=3.1.6 comment in pyproject.toml (safe_join and
# secure_filename are load-bearing there). Extend only together with
# that comment.
WERKZEUG_ALLOWLIST: dict[str, str] = {
"src/local_deep_research/security/filename_sanitizer.py": (
"secure_filename for upload sanitisation"
),
"src/local_deep_research/security/path_validator.py": (
"safe_join for path-traversal containment"
),
}
def _imported_modules(tree: ast.Module) -> Iterator[tuple[int, str]]:
"""Yield ``(line, module name)`` for every absolute import."""
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for alias in node.names:
yield node.lineno, alias.name
elif isinstance(node, ast.ImportFrom):
# Relative imports (level > 0) cannot name flask/werkzeug.
if node.level == 0 and node.module:
yield node.lineno, node.module
def _scan_python(root: Path, path: Path) -> list[str]:
"""Return violation strings for one Python file (empty if clean)."""
rel = path.relative_to(root).as_posix()
try:
text = path.read_text(encoding="utf-8")
except (OSError, UnicodeDecodeError) as error:
return [f"{rel}:1: unreadable, cannot scan ({error})"]
# Cheap prefilter: skip parsing files that never mention either
# package. Files that mention them only in comments/docstrings
# still parse -- the AST walk is what clears them.
if "flask" not in text.lower() and "werkzeug" not in text.lower():
return []
try:
tree = ast.parse(text, filename=str(path))
except SyntaxError as error:
return [f"{rel}:{error.lineno}: unparseable ({error.msg})"]
violations: list[str] = []
for lineno, name in _imported_modules(tree):
if FLASK_PACKAGE_RE.match(name):
violations.append(
f"{rel}:{lineno}: flask import '{name}' -- the codebase"
" is FastAPI-only since the #3299 migration"
)
elif name == "werkzeug" or name.startswith("werkzeug."):
if rel not in WERKZEUG_ALLOWLIST:
violations.append(
f"{rel}:{lineno}: werkzeug import '{name}' outside"
" the two allowlisted security modules (see"
" WERKZEUG_ALLOWLIST and the werkzeug pin comment"
" in pyproject.toml)"
)
return violations
def _dependency_name(dependency: str) -> str:
"""Extract the bare package name from a PEP 508 dependency string."""
match = re.match(r"\s*([A-Za-z0-9._-]+)", dependency)
return match.group(1) if match else ""
def _scan_pyproject(path: Path) -> list[str]:
"""Flag flask* entries among declared or optional dependencies."""
rel = path.name
try:
data = tomllib.loads(path.read_text(encoding="utf-8"))
except (OSError, tomllib.TOMLDecodeError) as error:
return [f"{rel}: unreadable, cannot scan ({error})"]
project = data.get("project", {})
declared: list[tuple[str, str]] = [
(dep, "dependencies") for dep in project.get("dependencies", [])
]
for group, extras in project.get("optional-dependencies", {}).items():
declared.extend((dep, f"optional '{group}'") for dep in extras)
return [
f"{rel}: {where} entry '{dep}' re-introduces Flask"
for dep, where in declared
if FLASK_PACKAGE_RE.match(_dependency_name(dep))
]
def find_violations(root: Path) -> list[str]:
"""Scan ``<root>/src/**/*.py`` and ``<root>/pyproject.toml``."""
violations: list[str] = []
for path in sorted((root / "src").rglob("*.py")):
violations.extend(_scan_python(root, path))
pyproject = root / "pyproject.toml"
if pyproject.is_file():
violations.extend(_scan_pyproject(pyproject))
return violations
def main() -> int:
violations = find_violations(ROOT)
if not violations:
return 0
print("❌ Flask regression detected -- this codebase is FastAPI-only.")
print()
print("Issues found:")
for violation in violations:
print(f" - {violation}")
print()
print("Fix:")
print(" - Routing/handlers: FastAPI routers (web/routers/)")
print(" - jsonify -> JSONResponse(...) or a returned dict")
print(" - request.args -> request.query_params")
print(" - flask.session -> auth dependencies (web/dependencies/)")
print(" - werkzeug: only filename_sanitizer.py and path_validator.py")
print(" may import it (see the pyproject.toml pin comment)")
return 1
if __name__ == "__main__":
sys.exit(main())