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.

2.6 KB
Raw Blame
"""Regression: ``domain_classifier.models`` must import without a cycle.

``domain_classifier.models`` defines the ``DomainClassification`` model and
needs SQLAlchemy's ``Base``. It used to import ``Base`` from the
``..database.models`` *package*, whose ``__init__`` eagerly imports every model
— including ``DomainClassification`` back from this module. Importing
``domain_classifier.models`` first (before ``database.models`` was cached)
crashed at import time::

    ImportError: cannot import name 'DomainClassification' from partially
    initialized module 'local_deep_research.domain_classifier.models'

The fix imports ``Base`` from the dependency-free ``database.base`` leaf
instead. Because ``database`` is a namespace package (no ``__init__.py``),
importing ``database.base`` never triggers ``database.models.__init__`` — so the
eager re-import of ``DomainClassification`` is never reached. (Importing from
``database.models.base`` would NOT work: pulling any submodule of
``database.models`` first runs that package's ``__init__``, re-triggering the
cycle.) These checks import the cycle-critical modules in FRESH interpreters —
pytest's module cache would mask a real cycle within a single process (whatever
imports ``database.models`` first hides it) — and assert they load cleanly.
"""

import subprocess
import sys

import pytest

# Each statement must import on its own in a clean interpreter. The first is the
# one that regressed; the others guard the surrounding cycle members.
CYCLE_CRITICAL = [
    "import local_deep_research.domain_classifier.models",
    "from local_deep_research.domain_classifier.models import DomainClassification",
    "import local_deep_research.database.models",
    "import local_deep_research.domain_classifier",
]


@pytest.mark.parametrize("stmt", CYCLE_CRITICAL)
def test_cycle_critical_module_imports_clean(stmt):
    result = subprocess.run(
        [sys.executable, "-c", stmt],
        capture_output=True,
        text=True,
        timeout=120,
    )
    assert result.returncode == 0, (
        "Import failed (possible reintroduced circular import):\n"
        f"  statement: {stmt}\n{result.stderr}"
    )


def test_domain_classification_shares_the_canonical_base():
    """The model must register on the same Base as the rest of the schema."""
    from local_deep_research.database.models import Base
    from local_deep_research.database.models.base import Base as LeafBase
    from local_deep_research.domain_classifier.models import (
        DomainClassification,
    )

    assert Base is LeafBase
    assert DomainClassification.__table__.metadata is Base.metadata