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.

14.5 KB
Raw Blame
"""Every relative import under ``src/`` must name a module that exists.

This shipped as a real, data-destroying bug on the Flask->FastAPI branch.
``research_library/routes/rag_routes.py`` used, correctly for its old
location::

    from ..deletion.utils.cascade_helper import CascadeHelper

The migration moved that module to ``web/routers/rag.py``, where ``..`` now
means ``local_deep_research.web`` -- and ``web/deletion/`` does not exist.
The file contains *two* such imports 41 lines apart; only the first was
re-based to ``...research_library.deletion.utils.cascade_helper``. The
second was left as-is and raised ``ModuleNotFoundError`` at call time.

What made it expensive is where it sat. ``_unlink_reindex_faiss_files()``
runs on force-reindex, immediately *after* the commit that deletes every
``DocumentChunk`` and ``RAGIndex`` row and flags every document
un-indexed. So the destructive half was durable, the re-index then aborted,
and the collection was left with no chunks, no index rows, orphaned
``.faiss``/``.pkl`` files on disk, and search returning nothing until the
user manually ran a second, non-force index. An ``if not index_paths:
return`` guard above it meant it fired *only* when an index already existed
-- exactly the case force-reindex exists for.

Nothing caught it:

* It is a **function-local** import, so it never runs at import time. The
  module imports fine, the app boots fine, and every other route in the
  file works.
* Linters do not resolve relative imports across a package move; ruff is
  happy with a syntactically valid ``from ..x import Y``.
* No test covered the force-reindex path.

A whole-file move is precisely when this class appears, and a 772-file
migration is a whole-file move repeated 772 times. So the scan here is
deliberately **repo-wide** across ``src/local_deep_research/`` rather than
scoped to the web layer like
``tests/web/routers/test_migration_antipattern_guards.py`` -- the defect is
a property of moving files, not of the request/response plumbing.

Scope of the check: this resolves the **module** half of each relative
import (the part after ``from``), which is what breaks on a move and what
broke here. It deliberately does not verify that the imported *names*
exist -- those can be conditionally defined or re-exported through an
``__init__``, which makes a static name check fragile in a way a module
check is not.

Detection is AST-based and reads nothing at runtime: resolving these by
*importing* them would execute module bodies for hundreds of modules, and
the whole point is to catch imports that only run on a rare code path.
"""

import ast
from pathlib import Path

import pytest

import local_deep_research

# Located by importing the package rather than by walking up from
# __file__, so a move of either this test or the package cannot silently
# point the scan at nothing.
PACKAGE_ROOT = Path(local_deep_research.__file__).resolve().parent
SRC_ROOT = PACKAGE_ROOT.parent
PACKAGE_NAME = PACKAGE_ROOT.name


def _module_exists(parts):
    """True if dotted ``parts`` names a real module or package under src/."""
    if not parts:
        return False
    base = SRC_ROOT.joinpath(*parts)
    return base.with_suffix(".py").is_file() or (base / "__init__.py").is_file()


def _resolve(node, package_parts):
    """Resolve a relative ImportFrom to absolute dotted parts.

    Returns ``None`` when the import climbs above the top-level package,
    which is itself a broken import.
    """
    # level=1 is "the package containing this module"; each extra dot
    # climbs one more. For both `foo/bar.py` and `foo/bar/__init__.py`
    # the containing package is the directory, so package_parts is the
    # directory in both cases.
    climb = node.level - 1
    if climb > len(package_parts):
        return None
    base = (
        package_parts[: len(package_parts) - climb] if climb else package_parts
    )
    return list(base) + (node.module.split(".") if node.module else [])


def _package_parts_for(path):
    """Dotted parts of the package containing ``path``."""
    return path.resolve().parent.relative_to(SRC_ROOT).parts


def _iter_source_files():
    return sorted(PACKAGE_ROOT.rglob("*.py"))


def _broken_relative_imports_in(path):
    """Yield (lineno, written, resolved) for each unresolvable import."""
    tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path))
    package_parts = _package_parts_for(path)
    for node in ast.walk(tree):
        if not isinstance(node, ast.ImportFrom) or not node.level:
            continue
        resolved = _resolve(node, package_parts)
        if resolved is not None and _module_exists(resolved):
            continue
        written = "." * node.level + (node.module or "")
        yield (
            node.lineno,
            written,
            ".".join(resolved) if resolved else "(above the top-level package)",
        )


def _suggest(resolved):
    """If a module with this basename exists elsewhere, name it.

    Turns "cannot resolve X" into "did you mean Y", which is the whole
    fix for a half-updated move.
    """
    if not resolved or resolved.endswith(")"):
        return ""
    leaf = resolved.rsplit(".", 1)[-1]
    hits = sorted(
        ".".join(p.relative_to(SRC_ROOT).with_suffix("").parts)
        for p in PACKAGE_ROOT.rglob(f"{leaf}.py")
    )
    return f"  did you mean: {', '.join(hits)}" if hits else ""


# Broken relative imports that PRE-DATE the FastAPI migration -- both are
# present on `main` at the same line numbers, so neither is migration
# damage, and neither is fixed here: this branch is a framework port, and
# changing unrelated runtime behaviour inside it would be scope creep that
# a reviewer cannot separate from the port itself.
#
# They are recorded rather than skipped so this stays a ratchet: a NEW
# broken import fails immediately, while these two are visible, attributed,
# and test-enforced to still be broken (see
# TestKnownBrokenAreStillBroken) -- an entry that gets fixed must be
# deleted from this dict or CI fails, so the list cannot rot into a
# blanket exemption.
#
# Both deserve their own follow-up (issue #5710), and NEITHER is a
# one-line fix. An earlier version of this comment claimed the first one
# was; that was wrong, and the correction matters more than the original
# claim did, because the "obvious" fix makes things worse:
#
# * news/core/search_integration.py -- do NOT re-base this to
#   `...metrics.search_tracker`. That module's `SearchTracker` has no
#   `track_search` method at all: it exposes `record_search`, a
#   @staticmethod taking (engine_name, query, results_count, ...) that
#   logs per-search-ENGINE calls for the metrics dashboard off
#   thread-local context. The call site here wants per-USER
#   personalization -- track_search(user_id=, query=, search_id=,
#   result_quality=, result_count=, strategy_used=) -- which nothing in
#   the repo implements, and `preference_manager/storage.py` has no
#   search-history store to put it in. Re-basing would trade
#   ModuleNotFoundError for AttributeError on a wrong-tool call.
#   Two further corrections: it is not "silent" (the surrounding
#   `except Exception` calls `logger.exception`, so it IS logged), and it
#   is not reachable at all today -- `tracking_enabled` returns a
#   hardcoded False, so `_track_user_search` never executes. This is
#   unfinished scaffolding, not a live user-facing regression, and
#   finishing it is a small feature rather than an import fix.
#
# * utilities/setup_utils.py -- `config.config_files` does not exist at
#   all, so `setup_user_directories()` raises for every caller. Its only
#   test (`tests/test_reexport_modules.py:76`) mocks the lazy import, so
#   it passes green against a function that cannot run. This one is dead
#   code, not a bug to repair: `config/config_files.py` was deliberately
#   deleted when settings moved from TOML files to the database, that
#   commit missed this orphan, there are zero callers left in src/, and
#   the directory setup it used to do is handled by
#   `config/paths.py::get_data_directory()`. The fix is to delete the
#   module and its mock-based test, not to repoint the import.
KNOWN_BROKEN = {
    (
        "local_deep_research/news/core/search_integration.py",
        "..preference_manager.search_tracker",
    ): "pre-existing on main; SearchTracker lives in ...metrics.search_tracker",
    (
        "local_deep_research/utilities/setup_utils.py",
        "..config.config_files",
    ): "pre-existing on main; config.config_files does not exist",
}


def _scan_all():
    """Yield (posix_relpath, lineno, written, resolved) for every break."""
    for path in _iter_source_files():
        rel = path.relative_to(SRC_ROOT).as_posix()
        for lineno, written, resolved in _broken_relative_imports_in(path):
            yield rel, lineno, written, resolved


def test_every_relative_import_resolves_to_a_real_module():
    failures = [
        f"{rel}:{lineno}: `from {written} import ...` resolves to "
        f"`{resolved}`, which does not exist.{_suggest(resolved)}"
        for rel, lineno, written, resolved in _scan_all()
        if (rel, written) not in KNOWN_BROKEN
    ]
    assert not failures, (
        "Relative import(s) name a module that does not exist. This is the "
        "signature of a file that moved without its imports being re-based; "
        "a function-local one raises ModuleNotFoundError only when its code "
        "path runs, so it boots and tests fine until a user hits it:\n  "
        + "\n  ".join(failures)
    )


def test_the_scan_actually_reached_the_source_tree():
    """Premise guard: a scan that silently found nothing would pass above.

    If a refactor moves the package and the rglob starts returning an
    empty list, the real test would go green while checking zero imports.
    """
    files = _iter_source_files()
    assert len(files) > 500, (
        f"expected the scan to reach the whole package, found {len(files)} "
        f".py files under {PACKAGE_ROOT}"
    )
    relative_imports = sum(
        1
        for path in files
        for node in ast.walk(ast.parse(path.read_text(encoding="utf-8")))
        if isinstance(node, ast.ImportFrom) and node.level
    )
    assert relative_imports > 200, (
        f"expected the package to contain many relative imports, found "
        f"{relative_imports} -- the guard above may be scanning nothing"
    )


class TestKnownBrokenAreStillBroken:
    """Keep KNOWN_BROKEN honest.

    An allowlist nobody prunes becomes a permanent exemption. If one of
    these is fixed (or the file moves), its entry must be deleted, and
    this test is what forces that.
    """

    def test_each_known_broken_entry_is_still_present(self):
        live = {(rel, written) for rel, _, written, _ in _scan_all()}
        stale = sorted(set(KNOWN_BROKEN) - live)
        assert not stale, (
            "KNOWN_BROKEN lists import(s) that are no longer broken (fixed, "
            "or the file moved). Delete these entries -- leaving them in "
            "would exempt a path that no longer needs exempting:\n  "
            + "\n  ".join(f"{rel}: from {written}" for rel, written in stale)
        )


class TestResolverSelfTest:
    """Negative controls.

    A guard that cannot be shown to fail on the real defect is not a
    guard. These reconstruct the shipped bug and its correct form on a
    synthetic tree with the same shape.
    """

    @staticmethod
    def _find(tmp_path, monkeypatch, source):
        """Run the resolver against a synthetic package tree."""
        pkg = tmp_path / PACKAGE_NAME
        (pkg / "web" / "routers").mkdir(parents=True)
        (pkg / "research_library" / "deletion" / "utils").mkdir(parents=True)
        for marker in [
            pkg / "__init__.py",
            pkg / "web" / "__init__.py",
            pkg / "web" / "routers" / "__init__.py",
            pkg / "research_library" / "__init__.py",
            pkg / "research_library" / "deletion" / "__init__.py",
            pkg / "research_library" / "deletion" / "utils" / "__init__.py",
            pkg
            / "research_library"
            / "deletion"
            / "utils"
            / "cascade_helper.py",
        ]:
            marker.write_text("", encoding="utf-8")
        target = pkg / "web" / "routers" / "rag.py"
        target.write_text(source, encoding="utf-8")

        monkeypatch.setattr(
            "tests.test_relative_imports_resolve.SRC_ROOT", tmp_path
        )
        monkeypatch.setattr(
            "tests.test_relative_imports_resolve.PACKAGE_ROOT", pkg
        )
        return list(_broken_relative_imports_in(target))

    def test_flags_the_import_that_actually_shipped(
        self, tmp_path, monkeypatch
    ):
        """The exact line from rag.py:2805, in its original location."""
        found = self._find(
            tmp_path,
            monkeypatch,
            "def _unlink():\n"
            "    from ..deletion.utils.cascade_helper import CascadeHelper\n",
        )
        assert len(found) == 1, found
        lineno, written, resolved = found[0]
        assert lineno == 2
        assert written == "..deletion.utils.cascade_helper"
        assert resolved == f"{PACKAGE_NAME}.web.deletion.utils.cascade_helper"

    def test_accepts_the_corrected_form(self, tmp_path, monkeypatch):
        """The sibling 41 lines earlier, which was re-based correctly."""
        found = self._find(
            tmp_path,
            monkeypatch,
            "def _reset():\n"
            "    from ...research_library.deletion.utils.cascade_helper "
            "import CascadeHelper\n",
        )
        assert found == []

    def test_flags_an_import_that_climbs_above_the_package(
        self, tmp_path, monkeypatch
    ):
        found = self._find(
            tmp_path, monkeypatch, "from ......nope import Thing\n"
        )
        assert len(found) == 1, found
        assert "above the top-level package" in found[0][2]

    def test_module_level_imports_are_scanned_too(self, tmp_path, monkeypatch):
        """ast.walk must reach both nested and top-level imports."""
        found = self._find(
            tmp_path,
            monkeypatch,
            "from ..deletion.utils.cascade_helper import CascadeHelper\n"
            "def f():\n"
            "    from ..deletion.utils.cascade_helper import CascadeHelper\n",
        )
        assert len(found) == 2, found

    @pytest.mark.parametrize(
        "form", ["from . import sibling", "from .. import web"]
    )
    def test_dotless_relative_imports_resolve_to_their_package(
        self, tmp_path, monkeypatch, form
    ):
        """`from . import x` has module=None; the package must still resolve."""
        assert self._find(tmp_path, monkeypatch, form + "\n") == []