mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
241 lines
12 KiB
Python
241 lines
12 KiB
Python
"""Require exact revision-bound node coverage before accepting release partitions."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
from collections import Counter
|
|
from contextlib import closing
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
import sqlite3
|
|
from xml.etree import ElementTree as ET
|
|
|
|
from coverage import CoverageData
|
|
from coverage.exceptions import CoverageException
|
|
|
|
from scripts.run_release_partition import SHARDS, digest
|
|
from scripts.release_source_identity import IDENTITY_FIELDS, check_origins
|
|
from scripts.release_bytecode_policy import check_policy
|
|
|
|
|
|
def read_object(path: Path) -> dict:
|
|
value = json.loads(path.read_text(encoding="utf-8"))
|
|
if not isinstance(value, dict) or type(value.get("schema_version")) is not int or value["schema_version"] != 1:
|
|
raise ValueError(f"{path}: invalid evidence schema")
|
|
return value
|
|
|
|
|
|
def nodes(path: Path) -> dict[str, list[str]]:
|
|
payload = read_object(path)
|
|
if payload.get("collection_skips") != []:
|
|
raise ValueError(f"{path}: collection-time skips require explicit resolution; see retained reasons")
|
|
value = payload.get("nodes")
|
|
if not isinstance(value, dict) or set(value) != set(SHARDS):
|
|
raise ValueError(f"{path}: invalid node inventory")
|
|
for kind, names in value.items():
|
|
if not isinstance(names, list) or any(not isinstance(name, str) or "::" not in name for name in names):
|
|
raise ValueError(f"{path}: invalid {kind} node IDs")
|
|
all_nodes = value["core"] + value["gloo"]
|
|
if len(set(all_nodes)) != len(all_nodes):
|
|
raise ValueError(f"{path}: duplicate collected node IDs")
|
|
return value
|
|
|
|
|
|
def junit_outcomes(path: Path) -> tuple[list[str], list[dict]]:
|
|
root = ET.parse(path).getroot()
|
|
if root.tag not in {"testsuite", "testsuites"}:
|
|
raise ValueError(f"{path}: invalid JUnit root")
|
|
suites = [root] if root.tag == "testsuite" else list(root)
|
|
if (any(suite.tag != "testsuite" for suite in suites)
|
|
or any(element.tag in {"testsuite", "testsuites"} for suite in suites for element in suite.iter() if element is not suite)
|
|
or sum(len(suite.findall("testcase")) for suite in suites) != len(list(root.iter("testcase")))):
|
|
raise ValueError(f"{path}: unsupported nested JUnit structure")
|
|
found, skipped = [], []
|
|
for case in root.iter("testcase"):
|
|
if case.find("failure") is not None or case.find("error") is not None:
|
|
raise ValueError(f"{path}: JUnit contains failed tests")
|
|
values = [prop.get("value") for prop in case.findall("./properties/property")
|
|
if prop.get("name") == "release_nodeid"]
|
|
if len(values) != 1 or not values[0]:
|
|
raise ValueError(f"{path}: testcase requires one exact node ID")
|
|
found.append(values[0])
|
|
skip = case.find("skipped")
|
|
if skip is not None:
|
|
skipped.append({"nodeid": values[0], "type": skip.get("type", ""),
|
|
"message": skip.get("message", ""), "reason": skip.text or ""})
|
|
return found, skipped
|
|
|
|
|
|
def junit_nodes(path: Path) -> list[str]:
|
|
return junit_outcomes(path)[0]
|
|
|
|
|
|
def check_binding(status: dict, binding: dict) -> None:
|
|
for key, value in binding.items():
|
|
if status.get(key) != value or type(status.get(key)) is not type(value):
|
|
raise ValueError(f"partition {key} binding mismatch")
|
|
|
|
|
|
def check_files(directory: Path, status: dict, *, junit: bool) -> list[Path]:
|
|
files = status.get("files")
|
|
required = {"collection.json", "junit.xml"} if junit else {"collection.json"}
|
|
if not isinstance(files, dict) or not required <= files.keys():
|
|
raise ValueError(f"{directory}: missing evidence file bindings")
|
|
coverage = []
|
|
for name, expected in files.items():
|
|
if (name not in required and not re.fullmatch(r"coverage\.data(?:\.[A-Za-z0-9_.-]+)?", name)):
|
|
raise ValueError(f"{directory}: unexpected bound file {name!r}")
|
|
if not isinstance(expected, str) or not re.fullmatch(r"[0-9a-f]{64}", expected):
|
|
raise ValueError(f"{directory}: invalid evidence digest")
|
|
path = directory / name
|
|
if not path.is_file() or path.is_symlink() or digest(path) != expected:
|
|
raise ValueError(f"{path}: missing or altered evidence")
|
|
if name.startswith("coverage.data"):
|
|
coverage.append(path)
|
|
if {path.name for path in directory.glob("coverage.data*")} != {path.name for path in coverage}:
|
|
raise ValueError(f"{directory}: unbound coverage data")
|
|
return coverage
|
|
|
|
|
|
def check_coverage(path: Path, identity: dict, *, all_skipped: bool = False, sidecar: bool = False) -> bool:
|
|
"""Validate every fragment; return whether it contains no execution observations."""
|
|
try:
|
|
data = CoverageData(basename=str(path))
|
|
data.read()
|
|
measured = data.measured_files()
|
|
executed_arcs = any(data.arcs(name) for name in measured)
|
|
if (not data.has_arcs()
|
|
or not measured and not sidecar
|
|
or not executed_arcs and not (all_skipped or sidecar)):
|
|
raise ValueError(f"{path}: coverage must contain measured branches")
|
|
check_origins(data, identity, unexecuted=not executed_arcs)
|
|
if not executed_arcs:
|
|
# Auto-started stdlib helpers can touch source files without running
|
|
# project code. Reject hidden/orphan observations or mixed-mode data
|
|
# before treating that branch-mode fragment as unexecuted.
|
|
with closing(sqlite3.connect(path.resolve().as_uri() + "?mode=ro", uri=True)) as db:
|
|
if (db.execute("PRAGMA integrity_check").fetchall() != [("ok",)]
|
|
or db.execute("PRAGMA foreign_key_check").fetchall()
|
|
or db.execute("SELECT 1 FROM arc UNION ALL SELECT 1 FROM line_bits LIMIT 1").fetchone()):
|
|
raise ValueError(f"{path}: invalid unexecuted branch coverage")
|
|
# Read all contexts/arcs so lazily detected table corruption cannot pass.
|
|
for name in measured:
|
|
data.arcs(name)
|
|
data.contexts_by_lineno(name)
|
|
return not executed_arcs
|
|
except (CoverageException, sqlite3.DatabaseError) as exc:
|
|
raise ValueError(f"{path}: corrupt coverage data: {exc}") from exc
|
|
|
|
|
|
def validate(
|
|
artifacts: Path, expected: Path, *, candidate_sha: str, target_sha: str,
|
|
target: str, python_version: str, core_result: str, gloo_result: str, checkout_origin: str,
|
|
) -> dict:
|
|
if core_result != "success" or gloo_result != "success":
|
|
raise ValueError("upstream release partition jobs must both succeed")
|
|
if (not all(re.fullmatch(r"[0-9a-f]{40}", sha) for sha in (candidate_sha, target_sha))
|
|
or target not in {"head", "base"} or python_version not in {"3.11", "3.12"}
|
|
or target == "head" and candidate_sha != target_sha):
|
|
raise ValueError("invalid expected target binding")
|
|
binding = {"candidate_sha": candidate_sha, "target_sha": target_sha,
|
|
"target": target, "python_version": python_version}
|
|
status = read_object(expected / "status.json")
|
|
check_policy(status)
|
|
check_binding(status, binding)
|
|
if type(status.get("exit_code")) is not int or status["exit_code"] != 0:
|
|
raise ValueError("independent target collection did not succeed")
|
|
extras = status.get("extras")
|
|
if not isinstance(extras, list) or extras not in [
|
|
["dev"], ["dev", "recovery"], ["dev", "mcp"], ["dev", "recovery", "mcp"],
|
|
]:
|
|
raise ValueError("invalid target extras")
|
|
binding["extras"] = extras
|
|
for key in IDENTITY_FIELDS:
|
|
value = status.get(key)
|
|
if not isinstance(value, dict if key in {"source_files", "bytecode_policy"} else str) or not value:
|
|
raise ValueError(f"missing source/tool identity: {key}")
|
|
binding[key] = value
|
|
roots = status.get("source_roots")
|
|
if not isinstance(roots, list) or len(roots) != 2 or not all(isinstance(root, str) and Path(root).is_absolute() for root in roots):
|
|
raise ValueError("invalid source origins")
|
|
check_files(expected, status, junit=False)
|
|
inventory = nodes(expected / "collection.json")
|
|
if not any(inventory.values()):
|
|
raise ValueError("independent target collection is empty")
|
|
partitions = {f"release-{kind}-py{python_version}-{target}-{index}": (kind, index)
|
|
for kind, count in SHARDS.items() for index in range(count)}
|
|
if not artifacts.is_dir() or {path.name for path in artifacts.iterdir()} != set(partitions):
|
|
raise ValueError("release partition artifact set mismatch")
|
|
accepted, junits, skipped, unexecuted_sidecars = [], [], [], []
|
|
passed = 0
|
|
executed = {kind: [] for kind in SHARDS}
|
|
for name, (kind, index) in partitions.items():
|
|
directory = artifacts / name
|
|
if not directory.is_dir() or directory.is_symlink() or list(directory.glob("status*.json")) != [directory / "status.json"]:
|
|
raise ValueError(f"{name}: missing, duplicate, or invalid partition status")
|
|
part = read_object(directory / "status.json")
|
|
check_policy(part)
|
|
check_binding(part, {**binding, "kind": kind, "shard": index, "shard_count": SHARDS[kind]})
|
|
if part.get("source_roots") != [checkout_origin, roots[1]]:
|
|
raise ValueError(f"{name}: unknown checkout or installed-package source origin")
|
|
code = part.get("exit_code")
|
|
if type(code) is not int or code not in {0, 5}:
|
|
raise ValueError(f"{name}: failed pytest exit status")
|
|
coverage = check_files(directory, part, junit=True)
|
|
selected = nodes(directory / "collection.json")
|
|
if any(selected[other] for other in SHARDS if other != kind):
|
|
raise ValueError(f"{name}: wrong marker partition")
|
|
cases, skipped_cases = junit_outcomes(directory / "junit.xml")
|
|
skipped.extend(skipped_cases)
|
|
passed += len(cases) - len(skipped_cases)
|
|
if Counter(cases) != Counter(selected[kind]):
|
|
raise ValueError(f"{name}: executed node IDs differ from selected collection")
|
|
if (code == 5) != (not cases):
|
|
raise ValueError(f"{name}: pytest exit status contradicts node inventory")
|
|
if cases:
|
|
if directory / "coverage.data" not in coverage:
|
|
raise ValueError(f"{name}: missing primary coverage data")
|
|
for path in coverage:
|
|
sidecar = path.name != "coverage.data"
|
|
unexecuted = check_coverage(path, part, all_skipped=len(skipped_cases) == len(cases), sidecar=sidecar)
|
|
if sidecar and unexecuted:
|
|
unexecuted_sidecars.append({"path": str(path.resolve()), "sha256": part["files"][path.name]})
|
|
accepted.extend(str(path.resolve()) for path in coverage)
|
|
executed[kind].extend(cases)
|
|
junits.append(str((directory / "junit.xml").resolve()))
|
|
for kind in SHARDS:
|
|
if Counter(executed[kind]) != Counter(inventory[kind]):
|
|
raise ValueError(f"{kind}: global node IDs are missing, extra, or duplicated")
|
|
if not passed:
|
|
raise ValueError("all selected testcases were skipped; no successful execution")
|
|
return {"schema_version": 1, **binding, "coverage": accepted, "junit": junits, "nodes": inventory,
|
|
"unexecuted_sidecars": unexecuted_sidecars,
|
|
"bytecode_policy_path": status["bytecode_policy_path"],
|
|
"source_roots": [checkout_origin, roots[1]],
|
|
"outcomes": {"passed": passed, "skipped": skipped},
|
|
"node_counts": {kind: len(names) for kind, names in executed.items()}}
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
for name in ("artifacts", "expected", "output"):
|
|
parser.add_argument("--" + name, type=Path, required=True)
|
|
for name in ("candidate-sha", "target-sha", "target", "python-version", "core-result", "gloo-result", "checkout-origin"):
|
|
parser.add_argument("--" + name, required=True)
|
|
args = parser.parse_args()
|
|
output = args.output
|
|
del args.output
|
|
try:
|
|
output.unlink(missing_ok=True)
|
|
result = validate(**vars(args))
|
|
output.write_text(json.dumps(result, indent=2, sort_keys=True), encoding="utf-8")
|
|
except (OSError, ValueError, ET.ParseError) as exc:
|
|
print(f"Release partition validation failed: {exc}")
|
|
return 1
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|