~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: forbid ``src.local_deep_research`` references in tests.
The package is importable under two distinct names: the canonical
``local_deep_research`` (what the running app and every production module use)
and ``src.local_deep_research`` (resolvable only because the repo root is on
``sys.path`` during test collection). Python treats these as SEPARATE module
objects with separate globals — so importing the SAME module under both names
loads TWO copies of it.
For most modules that is merely wasteful, but for process-global singletons it
is a latent bug. ``security/dns_pinning`` installs a process-wide
``socket.getaddrinfo`` shim and the notification send path fails CLOSED unless
that shim is *this* module's object (strict identity). A single ``src.``
import anywhere in the suite loads a second ``dns_pinning``, whose shim
displaces the canonical one, so the notification-send guard reports "shim
missing" and refuses to send — a failure that surfaces only in the full/xdist
run where a ``src.`` importer lands in the same worker before the send tests.
The fix was to normalize every test import to the canonical package; this hook
keeps it that way. It flags, in ``tests/**/*.py``:
* ``import src.local_deep_research...`` / ``from src.local_deep_research... import ...``
* string literals that START with ``src.local_deep_research`` — i.e. mock
``patch(...)`` / ``patch.object(...)`` targets and ``module_path`` values,
which import the ``src.`` module tree when the patch is entered exactly as a
real import would.
Comments naming the banned alias are never flagged (the AST drops comments).
Prose in a docstring is only flagged if the docstring string literal itself
*starts with* ``src.local_deep_research`` (``visit_Constant`` matches any such
string constant); ordinary docstrings that merely mention the alias mid-text do
not. ``tests/hooks/`` is exempt: those tests carry the alias as literal test
data for the hook checks themselves.
"""
import ast
import sys
from pathlib import Path
BANNED_PREFIX = "src.local_deep_research"
# Directories (as path components) whose test files legitimately carry the
# banned alias as test data for the pre-commit hooks themselves.
EXEMPT_DIR_COMPONENTS = ("hooks",)
def _is_test_file(p: Path) -> bool:
parts = p.parts
if "tests" not in parts:
return False
# Everything under tests/hooks is exempt (hook test data).
tests_idx = parts.index("tests")
tail = parts[tests_idx + 1 :]
return not any(c in tail for c in EXEMPT_DIR_COMPONENTS)
def _banned(name) -> bool:
return isinstance(name, str) and (
name == BANNED_PREFIX or name.startswith(BANNED_PREFIX + ".")
)
class _Checker(ast.NodeVisitor):
def __init__(self):
self.errors = []
def visit_Import(self, node):
for alias in node.names:
# import src.local_deep_research[...] OR import src (aliased)
if _banned(alias.name) or alias.name == "src":
self.errors.append(
(node.lineno, f"import of banned alias '{alias.name}'")
)
self.generic_visit(node)
def visit_ImportFrom(self, node):
module = node.module or ""
# from src.local_deep_research[...] import ...
if _banned(module):
self.errors.append(
(node.lineno, f"import from banned alias '{module}'")
)
# from src import local_deep_research
elif module == "src" and any(
a.name == "local_deep_research" for a in node.names
):
self.errors.append(
(node.lineno, "import of 'local_deep_research' from 'src'")
)
self.generic_visit(node)
def visit_Constant(self, node):
# patch(...) targets / module_path values that START with the alias.
if isinstance(node.value, str) and _banned(node.value):
self.errors.append(
(
node.lineno,
f"string references banned alias '{node.value}'",
)
)
self.generic_visit(node)
def check_file(filename: str) -> bool:
p = Path(filename)
if not _is_test_file(p):
return True
try:
content = p.read_text(encoding="utf-8")
except Exception as e: # pragma: no cover - unreadable file
print(f"Error reading {filename}: {e}")
return False
try:
tree = ast.parse(content, filename=filename)
except SyntaxError:
# Let ruff / the compiler report syntax errors.
return True
checker = _Checker()
checker.visit(tree)
if checker.errors:
print(f"\n{filename}:")
for lineno, msg in checker.errors:
print(f" Line {lineno}: {msg}")
return False
return True
def main() -> int:
if len(sys.argv) < 2:
print("Usage: check-no-src-test-imports.py <file1> <file2> ...")
return 1
has_errors = False
for filename in sys.argv[1:]:
if filename.endswith(".py") and not check_file(filename):
has_errors = True
if has_errors:
print("\n" + "=" * 70)
print("Banned 'src.local_deep_research' reference in tests!")
print("=" * 70)
print(
"\nImport the package under its canonical name 'local_deep_research'"
" instead\nof 'src.local_deep_research'. The two names are DISTINCT"
" module objects;\nimporting a module under both loads two copies,"
" which breaks process-\nglobal singletons (e.g. the"
" security.dns_pinning getaddrinfo shim that\nthe notification send"
" path fails closed on).\n"
)
print("Examples:")
print(" BAD: from src.local_deep_research.chat.service import X")
print(" GOOD: from local_deep_research.chat.service import X")
print(
' BAD: patch("src.local_deep_research.config.llm_config.get_llm")'
)
print(' GOOD: patch("local_deep_research.config.llm_config.get_llm")')
return 1
return 0
if __name__ == "__main__":
sys.exit(main())