~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 — imports the SUT inside an isolated subprocess so the
# import order (encrypted_db before the app module) is deterministic in a
# fresh interpreter; importing in-process would leave sys.modules already
# populated from other tests and could mask the regression.
"""CI regression guard for the file-integrity bootstrap import-cycle bug.
Historically, importing ``security/__init__.py`` (directly or transitively)
before ``database.encrypted_db`` had finished loading formed an import cycle:
security/__init__.py
-> security/file_integrity/integrity_manager.py
-> database/session_context.py
-> database/encrypted_db.py (mid-load, not yet fully initialized)
In a no-Flask process, ``integrity_manager`` caught the resulting
``ImportError`` and marked its session context unavailable. Construction of a
``FileIntegrityManager`` still failed closed, but only later and with generic
"install Flask" guidance that hid the original bootstrap error.
The fix: ``security/__init__.py`` no longer re-exports ``file_integrity``
(breaking the cycle), and ``integrity_manager`` treats ``session_context`` as
mandatory. Any import failure, including a load-order regression in a no-Flask
installation, propagates with its original traceback.
The success probe locks in the fix by driving the exact import order that
triggered the original bug -- ``database.encrypted_db`` first, then the web
application module (which imports ``security`` at module scope) -- in a fresh
subprocess and checking the real session-context binding. A companion failure
probe blocks Flask, forces the mandatory import to fail, and verifies that the
original error escapes without a Flask availability probe.
The second import used to be Flask's ``web.app_factory``, which the FastAPI
migration deleted. Its successor is ``web.fastapi_app`` (ADR 0011, row 8),
which pulls ``security`` in at module scope exactly as ``app_factory`` did
(``from ..security import get_security_default``) and so reproduces the same
load order. The property under test is unchanged; only the module that
provokes it was renamed. The companion test below pins that premise, so the
probe cannot go vacuous if the import is later moved inside a function.
"""
import ast
import os
import subprocess
import sys
import textwrap
from pathlib import Path
# The worktree's own src/ must come first on PYTHONPATH so the subprocess
# imports the version under test rather than any shadowing editable install.
_SRC_DIR = str(Path(__file__).resolve().parents[2] / "src")
def _run_script(script):
"""Run an import probe in a fresh interpreter."""
env = dict(os.environ)
existing = env.get("PYTHONPATH")
env["PYTHONPATH"] = (
_SRC_DIR if not existing else _SRC_DIR + os.pathsep + existing
)
return subprocess.run(
[sys.executable, "-c", textwrap.dedent(script)],
capture_output=True,
text=True,
timeout=60,
check=False,
env=env,
)
def _run_probe(script):
"""Run a successful import probe and return its stdout text."""
result = _run_script(script)
assert result.returncode == 0, (
"import probe exited with code {code}.\n"
"STDOUT:\n{stdout}\n"
"STDERR:\n{stderr}\n".format(
code=result.returncode,
stdout=result.stdout,
stderr=result.stderr,
)
)
return result.stdout
def _parse_markers(stdout):
"""Parse key=value lines from probe stdout into a dict."""
return dict(
line.split("=", 1)
for line in stdout.splitlines()
if "=" in line and not line.startswith(" ")
)
def test_module_file_resolves_under_worktree():
"""Sanity check: the probe's PYTHONPATH actually selects this worktree's
module, not a shadowing editable install elsewhere on the path."""
stdout = _run_probe(
"""
import local_deep_research.security.directory_creation as m
print("MODULE_FILE=" + m.__file__)
"""
)
markers = _parse_markers(stdout)
assert markers.get("MODULE_FILE", "").startswith(_SRC_DIR), (
"probe resolved local_deep_research from outside the worktree src/ "
"-- an editable install elsewhere is shadowing it.\n"
"Probe stdout:\n" + stdout
)
def test_encrypted_db_then_fastapi_app_keeps_integrity_session_context():
"""The risky import order must bind the mandatory session context."""
stdout = _run_probe(
"""
import local_deep_research.database.encrypted_db # noqa: F401
import local_deep_research.web.fastapi_app # noqa: F401
from local_deep_research.database import session_context
from local_deep_research.security.file_integrity import (
integrity_manager,
)
print(
"SESSION_CONTEXT_BOUND="
+ (
"yes"
if integrity_manager.get_user_db_session
is session_context.get_user_db_session
else "no"
)
)
"""
)
markers = _parse_markers(stdout)
assert markers.get("SESSION_CONTEXT_BOUND") == "yes", (
"integrity_manager did not retain the mandatory session-context "
"binding after importing database.encrypted_db then web.fastapi_app. "
"This is the exact import-order regression the bootstrap fix guards "
"against.\n"
"Probe stdout:\n" + stdout
)
def test_session_context_import_error_propagates_without_flask():
"""A no-Flask install must not hide the original mandatory import error."""
result = _run_script(
"""
import importlib.abc
import sys
session_context_module = (
"local_deep_research.database.session_context"
)
original_error = "forced session-context import failure"
class MissingDependencies(importlib.abc.MetaPathFinder):
def __init__(self):
self.flask_attempts = 0
def find_spec(self, fullname, path=None, target=None):
if fullname == session_context_module:
raise ImportError(original_error, name=fullname)
if fullname == "flask" or fullname.startswith("flask."):
self.flask_attempts += 1
raise ModuleNotFoundError(
"No module named 'flask'", name=fullname
)
return None
blocker = MissingDependencies()
sys.meta_path.insert(0, blocker)
try:
import flask # noqa: F401
except ModuleNotFoundError:
print("NO_FLASK=yes")
else:
raise AssertionError("test did not create a no-Flask state")
blocker.flask_attempts = 0
try:
import local_deep_research.security.file_integrity.integrity_manager # noqa: E501, F401
except ImportError as exc:
print("ERROR_MESSAGE=" + str(exc))
print("ERROR_MODULE=" + str(exc.name))
print("FLASK_IMPORT_ATTEMPTS=" + str(blocker.flask_attempts))
raise
else:
raise AssertionError("session-context import failure was swallowed")
"""
)
assert result.returncode != 0, (
"failing mandatory import unexpectedly succeeded.\n"
"STDOUT:\n{stdout}\nSTDERR:\n{stderr}\n".format(
stdout=result.stdout,
stderr=result.stderr,
)
)
markers = _parse_markers(result.stdout)
assert markers.get("NO_FLASK") == "yes"
assert (
markers.get("ERROR_MESSAGE") == "forced session-context import failure"
)
assert markers.get("ERROR_MODULE") == (
"local_deep_research.database.session_context"
)
assert markers.get("FLASK_IMPORT_ATTEMPTS") == "0"
assert result.stderr.rstrip().endswith(
"ImportError: forced session-context import failure"
), "the original exception must remain the traceback's terminal cause"
def test_fastapi_app_imports_security_at_module_scope():
"""Anti-vacuity control for the probe above.
That probe only reproduces the original bug if the module it imports
second really does pull ``security`` in while ``encrypted_db`` is still
on the stack -- that is what ``app_factory`` did, and it is the whole
reason ``fastapi_app`` is a valid stand-in for it. Were a future edit to
move the import inside a function, the probe would keep passing while
exercising nothing.
"""
source = (
Path(_SRC_DIR) / "local_deep_research" / "web" / "fastapi_app.py"
).read_text(encoding="utf-8")
module_scope_imports = set()
for node in ast.parse(source).body:
if isinstance(node, ast.ImportFrom) and node.module:
module_scope_imports.add(node.module)
elif isinstance(node, ast.Import):
module_scope_imports.update(alias.name for alias in node.names)
assert any(
name == "security" or name.endswith(".security")
for name in module_scope_imports
), (
"web/fastapi_app.py no longer imports the security package at "
"module scope, so the import-order probe above no longer "
"reproduces the bootstrap cycle it guards against.\n"
f"Module-scope imports: {sorted(module_scope_imports)}"
)