|
| 1 | +# Copyright 2025, 2026 Query Farm LLC - https://query.farm |
| 2 | + |
| 3 | +"""Fail when vgi-python's engine pins drift from the DuckDB ``vgi`` extension's engine. |
| 4 | +
|
| 5 | +vgi-python pins its embedded engine exactly (the ``haybarn``/``duckdb`` extras, |
| 6 | +which are also the ``vgi.duckdb.standard.v1`` filter oracle). The extension is |
| 7 | +built against the Haybarn tag named in its distribution workflow. When the two |
| 8 | +are on different engine lines (major.minor), vgi-python's own suite tests an |
| 9 | +engine no deployed extension uses: the 1.5 -> 2.0 move went unnoticed for a |
| 10 | +month that way, because every vgi-python test, and every extension test that |
| 11 | +launched a vgi-python worker, ran on 1.5.5. |
| 12 | +
|
| 13 | +The exact release candidate may differ (the extension builds from a Haybarn tag |
| 14 | +that need not have a PyPI wheel), so only the engine line is compared. |
| 15 | +
|
| 16 | +Usage: ``python scripts/check_engine_drift.py`` (set ``GITHUB_TOKEN`` to avoid |
| 17 | +the anonymous API rate limit; ``VGI_REPO`` / ``VGI_REF`` override the source). |
| 18 | +""" |
| 19 | + |
| 20 | +from __future__ import annotations |
| 21 | + |
| 22 | +import json |
| 23 | +import os |
| 24 | +import re |
| 25 | +import sys |
| 26 | +import tomllib |
| 27 | +import urllib.request |
| 28 | +from pathlib import Path |
| 29 | + |
| 30 | +ROOT = Path(__file__).resolve().parents[1] |
| 31 | +VGI_REPO = os.environ.get("VGI_REPO", "Query-farm/vgi") |
| 32 | +WORKFLOW = ".github/workflows/MainDistributionPipeline.yml" |
| 33 | +_LINE = re.compile(r"(\d+)\.(\d+)\.") |
| 34 | + |
| 35 | + |
| 36 | +def _get(url: str) -> bytes: |
| 37 | + request = urllib.request.Request(url, headers={"User-Agent": "vgi-python-engine-drift-check"}) |
| 38 | + token = os.environ.get("GITHUB_TOKEN") |
| 39 | + if token and url.startswith("https://api.github.com/"): |
| 40 | + request.add_header("Authorization", f"Bearer {token}") |
| 41 | + with urllib.request.urlopen(request, timeout=30) as response: # noqa: S310 - fixed https URLs |
| 42 | + data: bytes = response.read() |
| 43 | + return data |
| 44 | + |
| 45 | + |
| 46 | +def extension_engine() -> tuple[str, str]: |
| 47 | + """Return ``(ref, haybarn tag)`` the extension's distribution build uses.""" |
| 48 | + ref = os.environ.get("VGI_REF") or json.loads(_get(f"https://api.github.com/repos/{VGI_REPO}"))["default_branch"] |
| 49 | + text = _get(f"https://raw.githubusercontent.com/{VGI_REPO}/{ref}/{WORKFLOW}").decode() |
| 50 | + tags = set(re.findall(r"^\s*duckdb_version:\s*['\"]?([^'\"\s#]+)", text, re.MULTILINE)) |
| 51 | + if len(tags) != 1: |
| 52 | + raise SystemExit(f"expected one duckdb_version in {VGI_REPO}@{ref}:{WORKFLOW}, found {sorted(tags) or 'none'}") |
| 53 | + return ref, tags.pop() |
| 54 | + |
| 55 | + |
| 56 | +def pinned_engines() -> dict[str, str]: |
| 57 | + """Return ``{distribution: exact version}`` from the engine extras.""" |
| 58 | + extras = tomllib.loads((ROOT / "pyproject.toml").read_text())["project"]["optional-dependencies"] |
| 59 | + pins = {} |
| 60 | + for name in ("haybarn", "duckdb"): |
| 61 | + specs = [s.replace(" ", "") for s in extras[name] if re.match(rf"{name}\b", s)] |
| 62 | + match = re.fullmatch(rf"{name}==(\S+)", specs[0]) if len(specs) == 1 else None |
| 63 | + if match is None: |
| 64 | + raise SystemExit(f"the [{name}] extra must pin {name} exactly, got {specs}") |
| 65 | + pins[name] = match.group(1) |
| 66 | + return pins |
| 67 | + |
| 68 | + |
| 69 | +def engine_line(version: str) -> str: |
| 70 | + """``haybarn-v2.0.0-rc10`` / ``2.0.0rc11`` -> ``2.0``.""" |
| 71 | + match = _LINE.search(version) |
| 72 | + if match is None: |
| 73 | + raise SystemExit(f"cannot read an engine version from {version!r}") |
| 74 | + return f"{match.group(1)}.{match.group(2)}" |
| 75 | + |
| 76 | + |
| 77 | +def main() -> int: |
| 78 | + """Compare the engine lines and report. |
| 79 | +
|
| 80 | + Returns: |
| 81 | + The process exit status (0 when every pin is on the extension's line). |
| 82 | + """ |
| 83 | + ref, tag = extension_engine() |
| 84 | + want = engine_line(tag) |
| 85 | + pins = pinned_engines() |
| 86 | + print(f"extension ({VGI_REPO}@{ref}) builds on {tag} -> engine line {want}") |
| 87 | + drift = [] |
| 88 | + for name, version in pins.items(): |
| 89 | + line = engine_line(version) |
| 90 | + print(f"vgi-python [{name}] pins {name}=={version} -> engine line {line}") |
| 91 | + if line != want: |
| 92 | + drift.append(f"{name}=={version}") |
| 93 | + if drift: |
| 94 | + print( |
| 95 | + f"\nENGINE DRIFT: {', '.join(drift)} is not on the extension's DuckDB {want} line. Move the engine pins " |
| 96 | + "(pyproject extras + dev group), STANDARD_V1_ENGINE_VERSIONS in vgi/filter_v2.py and the oracle in " |
| 97 | + "docs/protocol/vgi-filter-encoding-v2-spec.md section 6 together, then run the suite on the new engine.", |
| 98 | + file=sys.stderr, |
| 99 | + ) |
| 100 | + return 1 |
| 101 | + return 0 |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + sys.exit(main()) |
0 commit comments