|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Fail closed when a canonical benchmark uses unpinned TiGrIS core sources.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import json |
| 8 | +import re |
| 9 | +import subprocess |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | + |
| 13 | +ROOT = Path(__file__).resolve().parents[1] |
| 14 | +COMMIT_RE = re.compile(r"[0-9a-f]{40}") |
| 15 | +RUNTIME_SCHEMA_RE = re.compile( |
| 16 | + r"^#define\s+TIGRIS_SCHEMA_VERSION(?:_V\d+)?\s+(\d+)\s*$", |
| 17 | + re.MULTILINE, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +def _schema_list(value: object, label: str, errors: list[str]) -> list[int]: |
| 22 | + if not isinstance(value, list) or not value: |
| 23 | + errors.append(f"{label} must be a non-empty list") |
| 24 | + return [] |
| 25 | + if any(not isinstance(item, int) or item < 1 for item in value): |
| 26 | + errors.append(f"{label} must contain positive integers") |
| 27 | + return [] |
| 28 | + result = list(value) |
| 29 | + if result != sorted(set(result)): |
| 30 | + errors.append(f"{label} must be sorted and duplicate-free") |
| 31 | + return result |
| 32 | + |
| 33 | + |
| 34 | +def validate_manifest(document: object) -> list[str]: |
| 35 | + errors: list[str] = [] |
| 36 | + if not isinstance(document, dict): |
| 37 | + return ["top level must be an object"] |
| 38 | + if document.get("format_version") != 1: |
| 39 | + errors.append("format_version must be 1") |
| 40 | + if not isinstance(document.get("profile"), str) or not document["profile"]: |
| 41 | + errors.append("profile must be a non-empty string") |
| 42 | + plan_schema = document.get("plan_schema") |
| 43 | + if not isinstance(plan_schema, int) or plan_schema < 1: |
| 44 | + errors.append("plan_schema must be a positive integer") |
| 45 | + |
| 46 | + compatibility = document.get("compatibility_manifest") |
| 47 | + compiler = document.get("compiler") |
| 48 | + runtime = document.get("runtime") |
| 49 | + if not all(isinstance(item, dict) for item in (compatibility, compiler, runtime)): |
| 50 | + return errors + [ |
| 51 | + "compatibility_manifest, compiler, and runtime must be objects" |
| 52 | + ] |
| 53 | + assert isinstance(compatibility, dict) |
| 54 | + assert isinstance(compiler, dict) |
| 55 | + assert isinstance(runtime, dict) |
| 56 | + |
| 57 | + for label, component in ( |
| 58 | + ("compatibility_manifest", compatibility), |
| 59 | + ("compiler", compiler), |
| 60 | + ("runtime", runtime), |
| 61 | + ): |
| 62 | + commit = component.get("commit") |
| 63 | + if not isinstance(commit, str) or not COMMIT_RE.fullmatch(commit): |
| 64 | + errors.append(f"{label}.commit must be a full Git SHA") |
| 65 | + repository = component.get("repository") |
| 66 | + if not isinstance(repository, str) or not repository.startswith( |
| 67 | + "https://github.com/raws-labs/" |
| 68 | + ): |
| 69 | + errors.append(f"{label}.repository must be a RAWS Labs HTTPS URL") |
| 70 | + |
| 71 | + if compatibility.get("commit") != compiler.get("commit"): |
| 72 | + errors.append("compatibility manifest must come from the pinned compiler") |
| 73 | + if compatibility.get("path") != "compatibility.json": |
| 74 | + errors.append("compatibility_manifest.path must be compatibility.json") |
| 75 | + for label, component in (("compiler", compiler), ("runtime", runtime)): |
| 76 | + if component.get("branch") != "develop": |
| 77 | + errors.append(f"{label}.branch must be develop") |
| 78 | + |
| 79 | + emitted = compiler.get("emits_schema") |
| 80 | + accepted = _schema_list( |
| 81 | + runtime.get("accepts_schemas"), "runtime.accepts_schemas", errors |
| 82 | + ) |
| 83 | + if emitted != plan_schema: |
| 84 | + errors.append("compiler schema must match plan_schema") |
| 85 | + if isinstance(emitted, int) and emitted not in accepted: |
| 86 | + errors.append("pinned runtime does not accept the compiler schema") |
| 87 | + return errors |
| 88 | + |
| 89 | + |
| 90 | +def _git(path: Path, *args: str) -> str: |
| 91 | + completed = subprocess.run( |
| 92 | + ["git", "-C", str(path), *args], |
| 93 | + text=True, |
| 94 | + capture_output=True, |
| 95 | + check=False, |
| 96 | + ) |
| 97 | + if completed.returncode != 0: |
| 98 | + raise RuntimeError(completed.stderr.strip() or "git command failed") |
| 99 | + return completed.stdout.strip() |
| 100 | + |
| 101 | + |
| 102 | +def validate_checkout( |
| 103 | + document: dict[str, object], compiler_root: Path, runtime_root: Path |
| 104 | +) -> list[str]: |
| 105 | + errors: list[str] = [] |
| 106 | + for label, path, expected in ( |
| 107 | + ("compiler", compiler_root, document["compiler"]), |
| 108 | + ("runtime", runtime_root, document["runtime"]), |
| 109 | + ): |
| 110 | + assert isinstance(expected, dict) |
| 111 | + try: |
| 112 | + actual = _git(path, "rev-parse", "HEAD") |
| 113 | + dirty = _git(path, "status", "--porcelain", "--untracked-files=no") |
| 114 | + except RuntimeError as exc: |
| 115 | + errors.append(f"cannot inspect {label} checkout {path}: {exc}") |
| 116 | + continue |
| 117 | + if actual != expected["commit"]: |
| 118 | + errors.append( |
| 119 | + f"{label} HEAD {actual} does not match pin {expected['commit']}" |
| 120 | + ) |
| 121 | + if dirty: |
| 122 | + errors.append(f"{label} checkout has tracked modifications") |
| 123 | + |
| 124 | + compatibility_path = compiler_root / "compatibility.json" |
| 125 | + try: |
| 126 | + compatibility = json.loads(compatibility_path.read_text()) |
| 127 | + except (OSError, json.JSONDecodeError) as exc: |
| 128 | + errors.append(f"cannot read compiler compatibility manifest: {exc}") |
| 129 | + else: |
| 130 | + integration = compatibility.get("integration", {}) |
| 131 | + compiler = document["compiler"] |
| 132 | + runtime = document["runtime"] |
| 133 | + assert isinstance(compiler, dict) |
| 134 | + assert isinstance(runtime, dict) |
| 135 | + if integration.get("compiler_emits_schema") != compiler.get("emits_schema"): |
| 136 | + errors.append("compiler checkout compatibility schema disagrees with pin") |
| 137 | + if integration.get("runtime_accepts_schemas") != runtime.get( |
| 138 | + "accepts_schemas" |
| 139 | + ): |
| 140 | + errors.append("compiler compatibility runtime set disagrees with pin") |
| 141 | + |
| 142 | + header = runtime_root / "include/tigris.h" |
| 143 | + try: |
| 144 | + accepted = sorted( |
| 145 | + {int(value) for value in RUNTIME_SCHEMA_RE.findall(header.read_text())} |
| 146 | + ) |
| 147 | + except OSError as exc: |
| 148 | + errors.append(f"cannot read runtime schema header: {exc}") |
| 149 | + else: |
| 150 | + runtime = document["runtime"] |
| 151 | + assert isinstance(runtime, dict) |
| 152 | + if accepted != runtime.get("accepts_schemas"): |
| 153 | + errors.append( |
| 154 | + f"runtime header accepts {accepted}, pin declares " |
| 155 | + f"{runtime.get('accepts_schemas')}" |
| 156 | + ) |
| 157 | + return errors |
| 158 | + |
| 159 | + |
| 160 | +def main() -> int: |
| 161 | + parser = argparse.ArgumentParser() |
| 162 | + parser.add_argument("--manifest", type=Path, default=ROOT / "core-versions.json") |
| 163 | + parser.add_argument("--manifest-only", action="store_true") |
| 164 | + parser.add_argument("--compiler-root", type=Path, default=ROOT.parent / "tigris") |
| 165 | + parser.add_argument( |
| 166 | + "--runtime-root", type=Path, default=ROOT.parent / "tigris-runtime" |
| 167 | + ) |
| 168 | + parser.add_argument( |
| 169 | + "--allow-unpinned", |
| 170 | + action="store_true", |
| 171 | + help="warn instead of failing checkout mismatches for development runs", |
| 172 | + ) |
| 173 | + args = parser.parse_args() |
| 174 | + try: |
| 175 | + document = json.loads(args.manifest.read_text()) |
| 176 | + except (OSError, json.JSONDecodeError) as exc: |
| 177 | + print(f"ERROR: cannot read {args.manifest}: {exc}") |
| 178 | + return 1 |
| 179 | + errors = validate_manifest(document) |
| 180 | + if not errors and not args.manifest_only: |
| 181 | + errors.extend( |
| 182 | + validate_checkout( |
| 183 | + document, args.compiler_root.resolve(), args.runtime_root.resolve() |
| 184 | + ) |
| 185 | + ) |
| 186 | + if errors and args.allow_unpinned: |
| 187 | + for error in errors: |
| 188 | + print(f"WARNING: {error}") |
| 189 | + print("Development override accepted unpinned core sources.") |
| 190 | + return 0 |
| 191 | + if errors: |
| 192 | + for error in errors: |
| 193 | + print(f"ERROR: {error}") |
| 194 | + return 1 |
| 195 | + compiler = document["compiler"] |
| 196 | + runtime = document["runtime"] |
| 197 | + assert isinstance(compiler, dict) |
| 198 | + assert isinstance(runtime, dict) |
| 199 | + print( |
| 200 | + "Pinned TiGrIS core verified: " |
| 201 | + f"compiler={compiler['commit']} runtime={runtime['commit']} " |
| 202 | + f"schema={document['plan_schema']}" |
| 203 | + ) |
| 204 | + return 0 |
| 205 | + |
| 206 | + |
| 207 | +if __name__ == "__main__": |
| 208 | + raise SystemExit(main()) |
0 commit comments