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.

5.2 KB
Raw Blame
"""Keep clean package checks on reviewed pip without locking app resolution."""

# allow: no-sut-import — guards the release workflow and its installer lock.

from pathlib import Path
import re
import shlex

from packaging.version import Version
import pytest
import yaml


ROOT = Path(__file__).resolve().parents[2]
LOCK = "requirements/bootstrap-runtime.txt"
# Reviewed pip 26.2.2 artifact digests (PyPI: pip-26.2.2-py3-none-any.whl and
# pip-26.2.2.tar.gz). Asserted exactly so a missing, extra, or substituted
# --hash line fails here; bump deliberately alongside the lock.
PIP_HASHES = [
    "--hash=sha256:3a8e5c800492787f33a4114ef54444546c340df428974ebba17e0fd47e11af2f",
    "--hash=sha256:fd7f4dfa8190e014013b9f08f3e973f4e4c26ffd179a204895533a7254813a70",
]


def commands(script):
    joined = re.sub(r"\\\n\s*", " ", script)
    return [
        line.strip()
        for line in joined.splitlines()
        if line.strip() and not line.lstrip().startswith("#")
    ]


@pytest.mark.parametrize(
    "job_id,environment",
    [
        ("pip-install-check", "pip-verify-env"),
        ("package-install-matrix", "package-install-env"),
    ],
)
def test_clean_package_environment_bootstraps_before_fresh_resolution(
    job_id, environment
):
    workflow = yaml.safe_load(
        (ROOT / ".github/workflows/release-gate.yml").read_text()
    )
    scripts = [
        step["run"]
        for step in workflow["jobs"][job_id]["steps"]
        if "run" in step
    ]
    script = next(
        script
        for script in scripts
        if f"python -m venv /tmp/{environment}" in script
    )
    lines = commands(script)
    activate = lines.index(f". /tmp/{environment}/bin/activate")
    bootstrap = next(
        index
        for index, line in enumerate(lines)
        if line.startswith("python -m pip install ") and LOCK in line
    )
    bootstrap_args = shlex.split(lines[bootstrap])
    assert "--require-hashes" in bootstrap_args
    assert "--only-binary=:all:" in bootstrap_args
    assert bootstrap_args[bootstrap_args.index("-r") + 1] == LOCK
    resolution = next(
        index
        for index, line in enumerate(lines)
        if line.startswith(
            ("pip install --no-cache-dir ", "if ! pip install --no-cache-dir ")
        )
    )
    assert activate < bootstrap < resolution
    resolution_args = shlex.split(lines[resolution])
    for option in ("--no-deps", "--require-hashes", "-r", "-c", "--constraint"):
        assert option not in resolution_args


def test_installer_lock_contains_only_a_patched_hash_pinned_pip():
    """The runtime bootstrap lock pins a patched pip and no other entry.

    The reviewed hash set is asserted exactly (wheel + sdist digests), so a
    missing, extra, or substituted ``--hash`` line fails here rather than
    passing as a well-formed-looking edit. Junk lines fail the single-entry
    assertion; an unpatched pip fails the version floor.
    """
    requirements = commands((ROOT / LOCK).read_text())
    assert len(requirements) == 1
    args = shlex.split(requirements[0])
    assert args[0].startswith("pip==")
    assert Version(args[0].split("==", 1)[1]) >= Version("26.2.1")
    assert sorted(args[1:]) == sorted(PIP_HASHES)


def test_dockerfile_hash_checked_installs_enforce_wheels_only():
    """Every ``--require-hashes`` install in the Dockerfile is wheels-only.

    The bootstrap locks' hash sets list an sdist alongside the wheel for
    some pins; without ``--only-binary=:all:`` a wheel-tag drift could
    silently source-build from that sdist hash instead of failing loudly,
    which is the failure mode the locks' headers promise. The release-gate
    install sites are pinned by
    ``test_clean_package_environment_bootstraps_before_fresh_resolution``;
    this covers the Dockerfile consumers. The revert this catches: dropping
    ``--only-binary=:all:`` from a Dockerfile pip install or download.
    """
    for line in commands((ROOT / "Dockerfile").read_text()):
        arguments = line.split()
        if "--require-hashes" in arguments:
            assert "--only-binary=:all:" in arguments


@pytest.mark.parametrize(
    "workflow_name",
    [
        "fuzz.yml",
        "mcp-tests.yml",
        "milvus-tests.yml",
        "mypy-type-check.yml",
    ],
)
def test_ci_bootstrap_install_enforces_wheels_only(workflow_name):
    """CI bootstrap consumers preserve the lock's wheel-only contract.

    The revert this catches: dropping ``--only-binary=:all:`` from the
    ``requirements/bootstrap-ci.txt`` installs in CI workflows.
    """
    workflow = yaml.safe_load(
        (ROOT / ".github/workflows" / workflow_name).read_text()
    )
    scripts = [
        step["run"]
        for job in workflow["jobs"].values()
        for step in job["steps"]
        if "run" in step
    ]
    script = next(
        script
        for script in scripts
        if "requirements/bootstrap-ci.txt" in script
    )
    install = next(
        line
        for line in commands(script)
        if "requirements/bootstrap-ci.txt" in line and "pip install" in line
    )
    assert "--only-binary=:all:" in install.split()
    arguments = shlex.split(install)
    assert "--require-hashes" in arguments
    assert (
        arguments[arguments.index("-r") + 1] == "requirements/bootstrap-ci.txt"
    )