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.

4.5 KB
Raw Blame
"""Verify schema-neutral model edits using isolated, committed source trees.

Revision 0001 uses live metadata. Comparing its fresh-install schema at
the merge base and at HEAD distinguishes a typing refactor from a change
that existing installations need an Alembic revision to receive.
"""

import io
import json
import subprocess
import sys
import tarfile
import tempfile
from functools import cache
from pathlib import Path

from tests.database.schema_change_rule import (
    MIN_METADATA_TABLES,
    is_revision,
)


_SNAPSHOT_SCRIPT = """
import json
import sys
from pathlib import Path

# -I keeps the checkout and inherited PYTHONPATH out of this process.
# Both refs use the same installed dependencies, but their own models.
source_root = Path(sys.argv[1]).resolve()
sys.path.insert(0, str(source_root))

from sqlalchemy import create_engine
from local_deep_research.database import models

# Installed/editable packages must never stand in for an archived source
# file, even if archive attributes excluded a package marker or model.
if not Path(models.__file__).resolve().is_relative_to(source_root):
    raise ValueError("model package did not load from the archived source")
Base = models.Base
for mapper in Base.registry.mappers:
    module = sys.modules[mapper.class_.__module__]
    if not Path(module.__file__).resolve().is_relative_to(source_root):
        raise ValueError("mapped model did not load from the archived source")

engine = create_engine("sqlite:///:memory:")
try:
    Base.metadata.create_all(engine)
    with engine.connect() as connection:
        rows = connection.exec_driver_sql(
            "SELECT type, name, tbl_name, sql FROM sqlite_schema "
            "WHERE sql IS NOT NULL ORDER BY type, name"
        ).fetchall()
    print(json.dumps([list(row) for row in rows]))
finally:
    engine.dispose()
"""


@cache
def _schema_at_commit(repo_root: Path, commit: str) -> list:
    """Return SQLite's actual DDL, including indexes and DDL event effects."""
    archive = subprocess.run(
        ["git", "archive", "--format=tar", commit, "src/local_deep_research"],
        cwd=repo_root,
        capture_output=True,
        check=True,
        timeout=30,
    )
    with tempfile.TemporaryDirectory(prefix="ldr-schema-") as directory:
        with tarfile.open(fileobj=io.BytesIO(archive.stdout)) as source:
            source.extractall(directory, filter="data")
        result = subprocess.run(
            [
                sys.executable,
                "-I",
                "-c",
                _SNAPSHOT_SCRIPT,
                str(Path(directory) / "src"),
            ],
            cwd=directory,
            capture_output=True,
            text=True,
            check=True,
            timeout=30,
        )
    schema = json.loads(result.stdout)
    if not isinstance(schema, list) or not all(
        isinstance(row, list)
        and len(row) == 4
        and all(isinstance(value, str) for value in row)
        for row in schema
    ):
        raise ValueError("invalid SQLite schema snapshot")
    return schema


def model_edits_preserve_schema(
    repo_root: Path,
    before_ref: str,
    after_ref: str,
    model_changes: dict,
    revision_changes: dict,
) -> bool:
    """Allow only edits proven to create an unchanged, nonempty schema.

    No snapshots are needed when a migration was added or no model code
    was edited. Added/removed model files remain subject to the original
    rule. Import, git, snapshot and timeout failures never grant an exemption.
    """
    if (
        not model_changes["edited"]
        or model_changes["added"]
        or model_changes["removed"]
        or any(is_revision(path) for path in revision_changes["added"])
    ):
        return False

    try:
        # Resolve mutable names before caching; a second local commit must
        # not reuse a snapshot of what HEAD pointed to previously.
        refs = subprocess.run(
            ["git", "rev-parse", before_ref, after_ref],
            cwd=repo_root,
            capture_output=True,
            text=True,
            check=True,
            timeout=10,
        ).stdout.splitlines()
        if len(refs) != 2:
            return False
        before, after = (
            _schema_at_commit(repo_root.resolve(), commit) for commit in refs
        )
        if any(
            sum(row[0] == "table" for row in schema) < MIN_METADATA_TABLES
            for schema in (before, after)
        ):
            return False
        return before == after
    except (OSError, subprocess.SubprocessError, tarfile.TarError, ValueError):
        return False