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.

33.5 KB
Raw Blame
"""Regression tests for the release artifact size and content gate."""

# allow: no-sut-import — tests invoke or load standalone CI scripts before
# the package is installed, without importing local_deep_research directly.

from __future__ import annotations

import copy
import importlib.util
import io
import re
import stat
import subprocess
import sys
import tarfile
import tomllib
import zipfile
from pathlib import Path

import pytest
import yaml


REPO_ROOT = Path(__file__).resolve().parents[2]
POLICY = REPO_ROOT / ".github" / "scripts" / "package_artifact_policy.py"
PYPROJECT = REPO_ROOT / "pyproject.toml"
PUBLISH_WORKFLOW = REPO_ROOT / ".github" / "workflows" / "publish.yml"
RELEASE_GATE = REPO_ROOT / ".github" / "workflows" / "release-gate.yml"

MIB = 1024 * 1024


def _write_wheel(
    path: Path,
    members: dict[str, bytes],
    compression: int = zipfile.ZIP_DEFLATED,
) -> None:
    with zipfile.ZipFile(path, mode="w", compression=compression) as archive:
        for name, data in members.items():
            archive.writestr(name, data)


def _write_sdist(path: Path, members: dict[str, bytes]) -> None:
    with tarfile.open(path, mode="w:gz") as archive:
        for name, data in members.items():
            info = tarfile.TarInfo(f"local_deep_research-1.0.0/{name}")
            info.size = len(data)
            archive.addfile(info, io.BytesIO(data))


def _run_policy(*paths: Path) -> subprocess.CompletedProcess[str]:
    return subprocess.run(
        [sys.executable, str(POLICY), *map(str, paths)],
        cwd=REPO_ROOT,
        check=False,
        capture_output=True,
        text=True,
        timeout=30,
    )


def _minimal_wheel(path: Path) -> None:
    _write_wheel(
        path,
        {
            "local_deep_research/__init__.py": b'__version__ = "1.0.0"\n',
            "local_deep_research-1.0.0.dist-info/METADATA": (
                b"Metadata-Version: 2.4\nName: local-deep-research\n"
            ),
        },
    )


def _minimal_sdist(path: Path) -> None:
    _write_sdist(
        path,
        {
            "pyproject.toml": b"[build-system]\nrequires = []\n",
            "src/local_deep_research/__init__.py": b"# package\n",
        },
    )


def test_clean_wheel_and_sdist_pass_as_a_complete_dist_directory(tmp_path):
    _minimal_wheel(tmp_path / "local_deep_research-1.0.0-py3-none-any.whl")
    _minimal_sdist(tmp_path / "local_deep_research-1.0.0.tar.gz")

    result = _run_policy(tmp_path)

    assert result.returncode == 0, result.stdout + result.stderr
    assert result.stdout.count("OK ") == 2


@pytest.mark.parametrize(
    "member",
    [
        "tests/test_runtime.py",
        "pdm.lock",
        "src/package-lock.json",
        "src/local_deep_research/__pycache__/module.pyc",
        "src/local_deep_research/.pytest_cache/state",
    ],
)
def test_sdist_rejects_development_only_content(tmp_path, member):
    artifact = tmp_path / "local_deep_research-1.0.0.tar.gz"
    _write_sdist(artifact, {member: b"development-only\n"})

    result = _run_policy(artifact)

    assert result.returncode == 1
    assert member in result.stderr


@pytest.mark.parametrize("artifact_type", ["wheel", "sdist"])
def test_generated_theme_css_is_not_shipped(tmp_path, artifact_type):
    path = "src/local_deep_research/web/static/css/themes.css"
    if artifact_type == "wheel":
        artifact = tmp_path / "local_deep_research-1.0.0-py3-none-any.whl"
        _write_wheel(artifact, {path.removeprefix("src/"): b"generated\n"})
    else:
        artifact = tmp_path / "local_deep_research-1.0.0.tar.gz"
        _write_sdist(artifact, {path: b"generated\n"})

    result = _run_policy(artifact)

    assert result.returncode == 1
    assert "generated theme CSS" in result.stderr


def test_disguised_binary_content_is_rejected(tmp_path):
    artifact = tmp_path / "local_deep_research-1.0.0-py3-none-any.whl"
    _write_wheel(
        artifact,
        {"local_deep_research/reviewable.py": b"PK\x03\x04not-python"},
    )

    result = _run_policy(artifact)

    assert result.returncode == 1
    assert "unexpected binary content (ZIP/container)" in result.stderr


def test_only_expected_runtime_binary_locations_pass(tmp_path):
    artifact = tmp_path / "local_deep_research-1.0.0-py3-none-any.whl"
    _write_wheel(
        artifact,
        {
            "local_deep_research/web/static/favicon.png": (
                b"\x89PNG\r\n\x1a\nfixture"
            ),
            "local_deep_research/web/static/sounds/error.mp3": (
                b"\xff\xfb\x90\xc4fixture"
            ),
            "local_deep_research/web/static/sounds/success.mp3": (
                b"ID3fixture"
            ),
            "local_deep_research/web/static/dist/fonts/math.hash.woff": (
                b"wOFFfixture"
            ),
            "local_deep_research/web/static/dist/fonts/math.hash.woff2": (
                b"wOF2fixture"
            ),
            "local_deep_research/web/static/dist/fonts/math.hash.ttf": (
                b"\x00\x01\x00\x00fixture"
            ),
        },
    )

    result = _run_policy(artifact)

    assert result.returncode == 0, result.stdout + result.stderr


def test_expected_binary_path_with_wrong_signature_fails(tmp_path):
    artifact = tmp_path / "local_deep_research-1.0.0-py3-none-any.whl"
    _write_wheel(
        artifact,
        {"local_deep_research/web/static/favicon.png": b"not a PNG\n"},
    )

    result = _run_policy(artifact)

    assert result.returncode == 1
    assert "approved PNG path has an invalid signature" in result.stderr


@pytest.mark.parametrize(
    "member",
    [
        "../outside.py",
        "/outside.py",
        "C:/outside.py",
        "bad\\name.py",
        "bad\nname.py",
    ],
)
def test_unsafe_archive_member_path_fails(tmp_path, member):
    artifact = tmp_path / "local_deep_research-1.0.0-py3-none-any.whl"
    _write_wheel(artifact, {member: b"payload\n"})

    result = _run_policy(artifact)

    assert result.returncode == 1
    assert "unsafe wheel member path" in result.stderr


def test_oversized_artifact_fails_before_archive_parsing(tmp_path):
    artifact = tmp_path / "oversized-py3-none-any.whl"
    with artifact.open("wb") as stream:
        stream.truncate(7 * 1024 * 1024 + 1)

    result = _run_policy(artifact)

    assert result.returncode == 1
    assert "limit is 7,340,032" in result.stderr


def test_dist_directory_rejects_extra_or_missing_artifacts(tmp_path):
    _minimal_wheel(tmp_path / "local_deep_research-1.0.0-py3-none-any.whl")
    (tmp_path / "unexpected.zip").write_bytes(b"PK\x03\x04")

    result = _run_policy(tmp_path)

    assert result.returncode == 1
    assert "unexpected files" in result.stderr
    assert "expected exactly one sdist" in result.stderr


def test_pdm_build_excludes_tests_and_generated_theme_css():
    config = tomllib.loads(PYPROJECT.read_text(encoding="utf-8"))

    assert config["tool"]["pdm"]["build"]["excludes"] == [
        "tests/",
        "src/local_deep_research/web/static/css/themes.css",
    ]


@pytest.mark.parametrize(
    ("workflow_path", "job_name"),
    [
        (PUBLISH_WORKFLOW, "build-package"),
        (RELEASE_GATE, "pip-install-check"),
    ],
)
def test_real_release_builds_enforce_the_artifact_policy(
    workflow_path, job_name
):
    workflow = yaml.safe_load(workflow_path.read_text(encoding="utf-8"))
    steps = workflow["jobs"][job_name]["steps"]
    runs = [step.get("run", "") for step in steps]
    commands = [line.strip() for run in runs for line in run.splitlines()]

    assert "pdm build --no-sdist" not in commands
    if workflow_path == RELEASE_GATE:
        assert "pdm build" in commands
        assert (
            "python .github/scripts/package_artifact_policy.py dist/"
            in commands
        )
    else:
        assert "pdm build" not in commands
        assert (
            "python .github/scripts/package_artifact_policy.py tested-package/dist/"
            in commands
        )
        assert any("release_package.py verify" in run for run in runs)


RELEASE_WORKFLOW = REPO_ROOT / ".github" / "workflows" / "release.yml"


def test_publisher_requires_the_package_jobs_named_by_the_workflows():
    """Use real workflow names rather than copying the validator's names."""
    release = yaml.safe_load(RELEASE_WORKFLOW.read_text(encoding="utf-8"))
    gate = yaml.safe_load(RELEASE_GATE.read_text(encoding="utf-8"))
    caller_id, caller = next(
        (job_id, job)
        for job_id, job in release["jobs"].items()
        if job.get("uses", "").endswith("/release-gate.yml")
    )
    prefix = caller.get("name", caller_id)
    names = [
        f"{prefix} / {gate['jobs'][job_id]['name']}"
        for job_id in ("pip-install-check", "release-gate-summary")
    ]
    install = gate["jobs"]["package-install-matrix"]
    for row in install["strategy"]["matrix"]["include"]:
        name = install["name"]
        for key, value in row.items():
            name = name.replace("${{ matrix." + key + " }}", str(value))
        assert "${{" not in name, "unresolved matrix job name"
        names.append(f"{prefix} / {name}")
    names.append(release["jobs"]["build"].get("name", "build"))
    # The publisher also requires the security decision recorded by build.
    upload = next(
        step
        for step in release["jobs"]["build"]["steps"]
        if step.get("id") == "security-decision"
    )
    decision_name = upload["with"]["name"].replace(
        "${{ github.run_attempt }}", "1"
    )
    assert "${{" not in decision_name, "unresolved decision artifact name"
    jobs = [
        {
            "id": i,
            "name": name,
            "run_attempt": 1,
            "status": "completed",
            "conclusion": "success",
        }
        for i, name in enumerate(names, 1)
    ]
    sha = "a" * 40
    root = "repos/owner/repo"
    responses = {
        f"{root}/actions/runs/123": {
            "repository": {"full_name": "owner/repo"},
            "path": ".github/workflows/release.yml",
            "head_sha": sha,
            "head_branch": "main",
            "event": "push",
            "run_attempt": 1,
        },
        f"{root}/actions/runs/123/jobs?filter=all&per_page=100&page=1": {
            "jobs": jobs
        },
        f"{root}/compare/{sha}...heads/main": {"status": "ahead"},
        f"{root}/actions/runs/123/artifacts?per_page=100&page=1": {
            "artifacts": [{"name": "verified-python-dist-1"}]
        },
        f"{root}/actions/artifacts/456": {
            "id": 456,
            "name": "verified-python-dist-1",
            "expired": False,
            "workflow_run": {"id": 123, "head_sha": sha},
        },
        f"{root}/actions/artifacts/789": {
            "id": 789,
            "name": decision_name,
            "expired": False,
            "workflow_run": {"id": 123, "head_sha": sha},
        },
    }
    payload = {
        "sha": sha,
        "run_id": "123",
        "artifact_id": "456",
        "release_decision_artifact_id": "789",
        "manifest_sha256": "b" * 64,
        "tag": "v1.2.3",
    }
    spec = importlib.util.spec_from_file_location(
        "release_package", REPO_ROOT / ".github/scripts/release_package.py"
    )
    policy = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(policy)
    assert (
        policy.validate_source("owner/repo", payload, responses.__getitem__)
        == "1"
    )
    for job in jobs:
        job["conclusion"] = "failure"
        with pytest.raises(
            ValueError, match="Required source job did not succeed"
        ):
            policy.validate_source("owner/repo", payload, responses.__getitem__)
        job["conclusion"] = "success"


def test_publish_job_revalidates_the_source_after_approval():
    """Re-running only ``publish`` reuses build-package's pre-approval outputs,
    so the OIDC job itself must recheck the live source run before upload."""
    publish = yaml.safe_load(PUBLISH_WORKFLOW.read_text(encoding="utf-8"))
    job = publish["jobs"]["publish"]
    assert job["environment"] == "release"
    steps = job["steps"]
    source = [
        i
        for i, step in enumerate(steps)
        if "release_package.py source" in step.get("run", "")
    ]
    uploads = [
        i
        for i, step in enumerate(steps)
        if step.get("uses", "").startswith("pypa/gh-action-pypi-publish@")
    ]
    assert len(source) == 1 and uploads
    assert source[0] < min(uploads)
    step = steps[source[0]]
    assert step["env"]["EXPECTED_ARTIFACT_ATTEMPT"] == (
        "${{ needs.build-package.outputs.artifact_attempt }}"
    )
    assert publish["jobs"]["build-package"]["outputs"]["artifact_attempt"] == (
        "${{ steps.source.outputs.artifact_attempt }}"
    )
    assert job["permissions"]["actions"] == "read"
    checkout = next(
        s for s in steps if s.get("uses", "").startswith("actions/checkout@")
    )
    assert checkout["with"]["persist-credentials"] is False
    assert checkout["with"]["sparse-checkout"] == (
        ".github/scripts/release_package.py"
    )
    # No step in the OIDC job installs or runs the downloaded package.
    for step in steps:
        assert "pip install" not in step.get("run", "")


def _console_scripts():
    config = tomllib.loads(PYPROJECT.read_text(encoding="utf-8"))
    return tuple(sorted(config["project"].get("scripts", {})))


# A marker-list check: a step counts as running the built package (or its
# dependencies, or inspecting dist/ before the hashes are recorded) when its
# ``run`` contains any of these substrings. It is a tripwire for ordinary
# edits, not a shell parser; a new way of executing the package needs a new
# marker here.
_PACKAGE_EXECUTION_MARKERS = (
    "package_artifact_policy.py",
    "pip install",
    "pip3 install",
    "pdm install",
    "pdm sync",
    "uv run",
    "uv pip",
    "python -c",
    "python -",
    "python3 -c",
    "python3 -",
    "entry_points",
    "twine",
    "ldr ",
    *_console_scripts(),
)

_MANIFEST_OUTPUT = "${{ steps.package-manifest.outputs.manifest_sha256 }}"


def _runs_package(run):
    return any(marker in run for marker in _PACKAGE_EXECUTION_MARKERS)


def _upload_paths(step):
    path = (step.get("with") or {}).get("path", "")
    return [line.strip() for line in str(path).splitlines() if line.strip()]


def _package_binding_violations(steps):
    """Return why ``steps`` could record or upload bytes that were not tested."""
    runs = [step.get("run", "") for step in steps]
    creates = [
        i for i, run in enumerate(runs) if "release_package.py create" in run
    ]
    if len(creates) != 1:
        return ["expected exactly one manifest create step"]
    create = creates[0]
    problems = []
    builds = [
        i
        for i, run in enumerate(runs)
        if "pdm build" in (line.strip() for line in run.splitlines())
    ]
    if not builds or builds[-1] > create:
        problems.append("manifest create step does not follow pdm build")
    executions = [i for i, run in enumerate(runs) if _runs_package(run)]
    problems += [
        f"step {i} ({steps[i].get('name')}) runs package code before create"
        for i in executions
        if i < create
    ]
    verifies = [
        i
        for i, run in enumerate(runs)
        if "release_package.py verify" in run
        and steps[i].get("env", {}).get("MANIFEST_SHA256") == _MANIFEST_OUTPUT
    ]
    for i in verifies:
        step = steps[i]
        if (
            "if" in step
            or step.get("continue-on-error") not in (None, False)
            or re.search(r"\|\|\s*(true|:)(\s|$)", runs[i])
        ):
            problems.append(
                f"verify step {i} ({step.get('name')}) can be skipped"
            )
    last_execution = max(executions, default=create)
    if not any(i > last_execution for i in verifies):
        problems.append("no hash verification after the package smoke tests")
    # Only this artifact ID is exported to the release/publish chain.
    # Inventory and scanner diagnostics are independent evidence uploads.
    uploads = [
        i
        for i, step in enumerate(steps)
        if step.get("id") == "package-upload"
        and str(step.get("uses", "")).startswith("actions/upload-artifact@")
    ]
    if len(uploads) != 1:
        return [*problems, "expected exactly one tested distribution upload"]
    if not verifies or min(uploads) < max(verifies):
        problems.append("distributions are uploaded before re-verification")
    elif any(max(verifies) < i < min(uploads) for i in executions):
        problems.append("package code runs between verification and upload")
    problems += [
        f"upload step {i} ({steps[i].get('name')}) omits release-package.json"
        for i in uploads
        if "release-package.json" not in _upload_paths(steps[i])
    ]
    return problems


def _step_index(steps, text):
    return next(
        i for i, step in enumerate(steps) if text in step.get("run", "")
    )


def test_release_gate_records_hashes_before_running_the_package():
    workflow = yaml.safe_load(RELEASE_GATE.read_text(encoding="utf-8"))
    job = workflow["jobs"]["pip-install-check"]
    assert job["outputs"]["artifact_id"] == (
        "${{ steps.package-upload.outputs.artifact-id }}"
    )
    steps = job["steps"]
    create = _step_index(steps, "release_package.py create")
    assert steps[create]["id"] == "package-manifest"

    assert _package_binding_violations(steps) == []


def test_package_binding_allows_independent_diagnostic_uploads():
    workflow = yaml.safe_load(RELEASE_GATE.read_text(encoding="utf-8"))
    steps = workflow["jobs"]["pip-install-check"]["steps"]
    diagnostic = {
        "name": "Retain scanner diagnostics",
        "uses": "actions/upload-artifact@pinned",
        "if": "always()",
        "with": {"path": "scanner-evidence/"},
    }
    for index in range(len(steps) + 1):
        with_diagnostics = copy.deepcopy(steps)
        with_diagnostics.insert(index, diagnostic)
        assert _package_binding_violations(with_diagnostics) == []


def _mutants(steps):
    """Yield (label, steps, expected problem) for each rule of the check."""
    create = _step_index(steps, "release_package.py create")
    install = _step_index(steps, "pip install")
    verify = _step_index(steps, "release_package.py verify")
    upload = next(
        i for i, step in enumerate(steps) if step.get("id") == "package-upload"
    )

    missing_upload = [s for i, s in enumerate(steps) if i != upload]
    yield "missing package upload", missing_upload, "exactly one tested"

    for label, change in (
        ("renamed package upload", {"id": "different-artifact"}),
        ("wrong upload action", {"uses": "different/upload@pinned"}),
    ):
        wrong_upload = copy.deepcopy(steps)
        wrong_upload[upload].update(change)
        yield label, wrong_upload, "exactly one tested"

    late_create = copy.deepcopy(steps)
    late_create.insert(install + 1, late_create.pop(create))
    yield "create after install", late_create, "runs package code before"

    no_reverify = [s for i, s in enumerate(steps) if i != verify]
    yield "no re-verification", no_reverify, "no hash verification after"

    early_upload = copy.deepcopy(steps)
    early_upload.insert(verify, early_upload.pop(upload))
    yield "upload before verify", early_upload, "uploaded before re-verif"

    commands = (
        "pip3 install dist/*.whl",
        "pdm install",
        "pdm sync",
        "uv run pytest",
        "uv pip install dist/*.whl",
        "ldr --version",
        "python3 -c 'import local_deep_research'",
        "python3 - <<'PY'",
        *_console_scripts(),
    )
    for command in commands:
        early = copy.deepcopy(steps)
        early.insert(create, {"name": "early", "run": command})
        yield f"{command!r} before create", early, "runs package code before"

        between = copy.deepcopy(steps)
        between.insert(upload, {"name": "late", "run": command})
        yield (
            f"{command!r} between verify and upload",
            between,
            "runs between verification and upload",
        )

    for label, change in (
        ("continue-on-error", {"continue-on-error": True}),
        ("if condition", {"if": "success()"}),
        ("always condition", {"if": "${{ always() }}"}),
        ("|| true", {"run": steps[verify]["run"] + " || true"}),
        ("|| :", {"run": steps[verify]["run"] + " || :"}),
    ):
        skippable = copy.deepcopy(steps)
        skippable[verify].update(change)
        yield f"verify with {label}", skippable, "can be skipped"

    for label, path in (
        ("without release-package.json", "dist/\n"),
        ("naming the manifest in a comment", "dist/  # release-package.json\n"),
    ):
        no_manifest = copy.deepcopy(steps)
        no_manifest[upload]["with"]["path"] = path
        yield f"upload {label}", no_manifest, "omits release-package.json"


def test_package_binding_check_rejects_every_mutant():
    """Each rule fires on its own mutant of the real job, with its own reason."""
    workflow = yaml.safe_load(RELEASE_GATE.read_text(encoding="utf-8"))
    steps = workflow["jobs"]["pip-install-check"]["steps"]
    missed = []
    for label, mutant, expected in _mutants(steps):
        problems = _package_binding_violations(mutant)
        if not any(expected in problem for problem in problems):
            missed.append((label, problems))

    assert missed == []


def test_console_scripts_are_execution_markers():
    scripts = _console_scripts()

    assert scripts
    assert set(scripts) <= set(_PACKAGE_EXECUTION_MARKERS)


def _dispatch_guard(step_name, env):
    """Run the workflow_dispatch refusal block of a release.yml build step."""
    workflow = yaml.safe_load(RELEASE_WORKFLOW.read_text(encoding="utf-8"))
    step = next(
        step
        for step in workflow["jobs"]["build"]["steps"]
        if step.get("name") == step_name
    )
    lines = step["run"].splitlines()
    start = next(
        i
        for i, line in enumerate(lines)
        if line.lstrip().startswith("if [[")
        and "workflow_dispatch" in line
        and "REF_NAME" in line
    )
    end = next(i for i in range(start, len(lines)) if lines[i].strip() == "fi")
    assert set(step["env"]) >= {"EVENT_NAME", "REF_NAME"}
    return subprocess.run(
        ["bash", "-c", "\n".join(lines[start : end + 1])],
        env={"PATH": "/usr/bin:/bin", **env},
        check=False,
        capture_output=True,
        text=True,
        timeout=30,
    ).returncode


@pytest.mark.parametrize(
    ("event", "ref", "allowed"),
    [
        ("workflow_dispatch", "main", True),
        ("workflow_dispatch", "v1.2.3", True),
        ("workflow_dispatch", "feature/release-fix", False),
        ("workflow_dispatch", "v1.2", False),
        ("push", "main", True),
        ("push", "v1.2.3", True),
    ],
)
def test_release_refuses_dispatches_publish_would_reject(event, ref, allowed):
    """Mirror publish.yml's head_branch rule before Docker is promoted."""
    code = _dispatch_guard(
        "Verify build commit is on main",
        {"EVENT_NAME": event, "REF_NAME": ref},
    )

    assert (code == 0) is allowed


@pytest.mark.parametrize(
    ("event", "ref", "allowed"),
    [
        ("workflow_dispatch", "main", True),
        ("workflow_dispatch", "v1.2.3", True),
        ("workflow_dispatch", "v1.2.4", False),
        # A prefix-match mutant (`!= v"$VERSION"*`) would wrongly accept
        # this: "v1.2.30" starts with "v1.2.3" but names a different
        # release than VERSION=1.2.3.
        ("workflow_dispatch", "v1.2.30", False),
        ("push", "main", True),
    ],
)
def test_release_dispatch_tag_must_name_the_built_version(event, ref, allowed):
    code = _dispatch_guard(
        "Determine version",
        {"EVENT_NAME": event, "REF_NAME": ref, "VERSION": "1.2.3"},
    )

    assert (code == 0) is allowed


def test_release_dispatch_guard_runs_after_version_is_assigned():
    """The guard must see the real VERSION, not an unset/stale one."""
    workflow = yaml.safe_load(RELEASE_WORKFLOW.read_text(encoding="utf-8"))
    step = next(
        step
        for step in workflow["jobs"]["build"]["steps"]
        if step.get("name") == "Determine version"
    )
    lines = step["run"].splitlines()
    version_assign = next(
        i
        for i, line in enumerate(lines)
        if line.lstrip().startswith("VERSION=")
    )
    guard_if = next(
        i
        for i, line in enumerate(lines)
        if line.lstrip().startswith("if [[")
        and "workflow_dispatch" in line
        and "REF_NAME" in line
    )

    assert guard_if > version_assign


def test_monitor_pypi_window_exceeds_the_publish_path():
    """A slow PyPI index must not look like a failed publish.

    monitor-pypi opens a failure issue and skips create-release when its
    poll budget runs out, so it has to outlast publish.yml's automated
    worst case with room left over for the ``release`` environment approval.
    """
    release = yaml.safe_load(RELEASE_WORKFLOW.read_text(encoding="utf-8"))
    publish = yaml.safe_load(PUBLISH_WORKFLOW.read_text(encoding="utf-8"))
    monitor = release["jobs"]["monitor-pypi"]
    run = next(
        step["run"]
        for step in monitor["steps"]
        if "max_wait=" in step.get("run", "")
    )
    max_wait = int(re.search(r"^\s*max_wait=(\d+)\s*$", run, re.M)[1])
    automated = sum(
        publish["jobs"][job]["timeout-minutes"] * 60
        for job in ("build-package", "verify-publish")
    )
    approval_allowance = 45 * 60

    assert max_wait >= automated + approval_allowance

    # The budget must be wall-clock time. A counter that only adds up the
    # sleeps ignores how long each ``gh run list | jq`` poll takes, so the
    # loop could outlive the job timeout (which cancels the failure-issue
    # step) before max_wait ran out.
    assert re.search(r"^\s*start=\$SECONDS\s*$", run, re.M)
    assert re.search(
        r'^\s*while \[ \$\(\(SECONDS - start\)\) -lt "\$max_wait" \]; do\s*$',
        run,
        re.M,
    )
    assert not re.search(r"elapsed=\$\(\(elapsed \+", run)

    # The job timeout must not cut the poll loop short: it has to cover the
    # initial sleep, the whole budget, and a last poll that starts just
    # before the budget ends (a slow gh call plus its 30s sleep), with room
    # left for the runner setup and the failure-issue step.
    initial_sleep = last_sleep = 30
    last_poll_allowance = 5 * 60
    setup_and_report_allowance = 5 * 60
    assert monitor["timeout-minutes"] * 60 >= (
        initial_sleep
        + max_wait
        + last_poll_allowance
        + last_sleep
        + setup_and_report_allowance
    )


def test_release_gate_frontend_install_skips_dependency_scripts():
    workflow = yaml.safe_load(RELEASE_GATE.read_text(encoding="utf-8"))
    steps = workflow["jobs"]["pip-install-check"]["steps"]
    npm = [
        line.strip()
        for step in steps
        for line in step.get("run", "").splitlines()
        if line.strip().startswith("npm ci")
    ]

    assert npm
    assert all("--ignore-scripts" in line.split() for line in npm)


@pytest.mark.parametrize("kind", ["wheel", "sdist"])
@pytest.mark.parametrize(
    "name", ["../outside/", "/outside/", "bad\\name/", "./"]
)
def test_unsafe_directory_entries_fail(tmp_path, kind, name):
    artifact = tmp_path / ("sample.whl" if kind == "wheel" else "sample.tar.gz")
    if kind == "wheel":
        _minimal_wheel(artifact)
        with zipfile.ZipFile(artifact, "a") as archive:
            archive.writestr(name, b"")
    else:
        with tarfile.open(artifact, "w:gz") as archive:
            regular = tarfile.TarInfo("package/pyproject.toml")
            regular.size = 1
            archive.addfile(regular, io.BytesIO(b"#"))
            directory = tarfile.TarInfo(name)
            directory.type = tarfile.DIRTYPE
            archive.addfile(directory)

    result = _run_policy(artifact)

    assert result.returncode == 1
    assert f"unsafe {kind} member path" in result.stderr


def test_wheel_rejects_symlink_mode_with_directory_name(tmp_path):
    artifact = tmp_path / "sample.whl"
    _minimal_wheel(artifact)
    with zipfile.ZipFile(artifact, "a") as archive:
        link = zipfile.ZipInfo("local_deep_research/link/")
        link.create_system = 3
        link.external_attr = (stat.S_IFLNK | 0o777) << 16
        archive.writestr(link, b"../target")

    result = _run_policy(artifact)

    assert result.returncode == 1
    assert "symbolic link is not permitted" in result.stderr


@pytest.mark.parametrize("kind", ["wheel", "sdist"])
def test_ordinary_directory_entries_remain_supported(tmp_path, kind):
    artifact = tmp_path / ("sample.whl" if kind == "wheel" else "sample.tar.gz")
    if kind == "wheel":
        _minimal_wheel(artifact)
        with zipfile.ZipFile(artifact, "a") as archive:
            archive.writestr("local_deep_research/nested/", b"")
    else:
        with tarfile.open(artifact, "w:gz") as archive:
            for name in ["package", "package/src", "package/src/nested"]:
                directory = tarfile.TarInfo(name)
                directory.type = tarfile.DIRTYPE
                archive.addfile(directory)
            regular = tarfile.TarInfo("package/src/nested/module.py")
            regular.size = 1
            archive.addfile(regular, io.BytesIO(b"#"))

    result = _run_policy(artifact)

    assert result.returncode == 0, result.stderr


@pytest.mark.parametrize("kind", ["wheel", "sdist"])
def test_directory_entry_carrying_data_is_rejected(tmp_path, kind):
    artifact = tmp_path / ("sample.whl" if kind == "wheel" else "sample.tar.gz")
    if kind == "wheel":
        _minimal_wheel(artifact)
        with zipfile.ZipFile(artifact, "a") as archive:
            archive.writestr(
                "local_deep_research/payload.so/", b"\x7fELF" + b"\x00" * 204
            )
    else:
        with tarfile.open(artifact, "w:gz") as archive:
            regular = tarfile.TarInfo("package/pyproject.toml")
            regular.size = 1
            archive.addfile(regular, io.BytesIO(b"#"))
            payload = b"\x7fELF" + b"\x00" * 204
            directory = tarfile.TarInfo("package/payload.so")
            directory.type = tarfile.DIRTYPE
            directory.size = len(payload)
            archive.addfile(directory, io.BytesIO(payload))

    result = _run_policy(artifact)

    assert result.returncode == 1
    assert "payload.so: directory entry carries data" in result.stderr


def test_wheel_file_count_limit_is_enforced(tmp_path):
    artifact = tmp_path / "local_deep_research-1.0.0-py3-none-any.whl"
    members = {f"local_deep_research/f{i}.txt": b"" for i in range(2001)}
    _write_wheel(artifact, members)

    result = _run_policy(artifact)

    assert result.returncode == 1
    assert "contains 2,001 files; limit is 2,000" in result.stderr


def test_wheel_member_size_limit_is_enforced(tmp_path):
    # Deflate-compressible zero bytes keep the on-disk artifact under the
    # 7 MiB artifact-size limit while the member's *uncompressed* size
    # (what MAX_MEMBER_BYTES checks) is one byte over 8 MiB, so it is the
    # per-file limit that fires rather than the whole-artifact limit.
    artifact = tmp_path / "local_deep_research-1.0.0-py3-none-any.whl"
    _write_wheel(
        artifact,
        {"local_deep_research/big.bin": b"\x00" * (8 * MIB + 1)},
    )

    result = _run_policy(artifact)

    assert result.returncode == 1
    assert "limit of 8,388,608" in result.stderr


def test_sdist_unpacked_size_limit_is_enforced(tmp_path):
    artifact = tmp_path / "local_deep_research-1.0.0.tar.gz"
    _write_sdist(
        artifact,
        {"local_deep_research/big.bin": b"\x00" * (32 * MIB + 1)},
    )

    result = _run_policy(artifact)

    assert result.returncode == 1
    assert "limit is 33,554,432" in result.stderr


def test_pyc_suffix_outside_pycache_fires_the_suffix_rule(tmp_path):
    artifact = tmp_path / "local_deep_research-1.0.0.tar.gz"
    _write_sdist(artifact, {"local_deep_research/mod.pyc": b"bytecode\n"})

    result = _run_policy(artifact)

    assert result.returncode == 1
    assert "forbidden generated bytecode: .pyc" in result.stderr
    assert "__pycache__" not in result.stderr


def test_wheel_rejects_forbidden_path(tmp_path):
    artifact = tmp_path / "local_deep_research-1.0.0-py3-none-any.whl"
    _write_wheel(artifact, {"tests/x.py": b"content\n"})

    result = _run_policy(artifact)

    assert result.returncode == 1
    assert "forbidden directory component(s): tests" in result.stderr


def test_sdist_rejects_forbidden_content(tmp_path):
    artifact = tmp_path / "local_deep_research-1.0.0.tar.gz"
    _write_sdist(
        artifact, {"local_deep_research/native.so": b"\x7fELFnot-real"}
    )

    result = _run_policy(artifact)

    assert result.returncode == 1
    assert "unexpected binary content (ELF executable/object)" in result.stderr


def test_oversized_artifact_message_lists_largest_members(tmp_path):
    artifact = tmp_path / "oversized-py3-none-any.whl"
    # Stored, not deflated: zero-filled members compress to almost nothing,
    # and the on-disk size is what the artifact limit measures.
    _write_wheel(
        artifact,
        {
            "local_deep_research/big.bin": b"\x00" * (5 * MIB),
            "local_deep_research/medium.bin": b"\x00" * (2 * MIB),
            "local_deep_research/small.bin": b"\x00" * 1024,
        },
        compression=zipfile.ZIP_STORED,
    )

    result = _run_policy(artifact)

    assert result.returncode == 1
    assert "limit is 7,340,032" in result.stderr
    assert "local_deep_research/big.bin (5,242,880 bytes)" in result.stderr
    assert "local_deep_research/medium.bin (2,097,152 bytes)" in result.stderr


def test_dist_directory_rejects_non_file_children(tmp_path):
    _minimal_wheel(tmp_path / "local_deep_research-1.0.0-py3-none-any.whl")
    _minimal_sdist(tmp_path / "local_deep_research-1.0.0.tar.gz")
    (tmp_path / "extra").mkdir()

    result = _run_policy(tmp_path)

    assert result.returncode == 1
    assert "unexpected files" in result.stderr
    assert "'extra'" in result.stderr