Files
rustfs/scripts/ci_gate.py
T
Hauser Zhi e89c5db7a7 chore(deps): refresh Cargo dependencies (#9778)
* chore(deps): refresh Cargo dependencies

Refresh compatible workspace dependencies, align the fixed zstd adapter, and keep the vendored HTTP/2 Expect backport active.

Co-Authored-By: heihutu <heihutu@gmail.com>
Co-Authored-By: zhi22915 <qiuzgang@gmail.com>
Co-Authored-By: qunjiazhu <qunjiazhu@gmail.com>

* chore(deps): upgrade DataFusion and TAR dependencies

Adapt S3 Select to DataFusion 56 while preserving field depth and function error contracts. Update TAR fixtures for the decoder API and hold compatible Hotpath and NIXL HTTP versions.

Co-Authored-By: heihutu <heihutu@gmail.com>
Co-Authored-By: zhi22915 <qiuzgang@gmail.com>
Co-Authored-By: qunjiazhu <qunjiazhu@gmail.com>

* fix(tests): restore Iceberg and IAM test compilation

Pass Arrow metadata arrays directly and update Vector IAM fixtures to use ArcSwap while preserving policy state restoration.

Co-Authored-By: heihutu <heihutu@gmail.com>
Co-Authored-By: zhi22915 <qiuzgang@gmail.com>
Co-Authored-By: qunjiazhu <qunjiazhu@gmail.com>

* fix(ci): select only Cargo workspace members

Keep excluded path crates in the dependency graph for reverse impact analysis. Require the corrected plan contract and fall back to full workspace validation when the trusted base planner is older.

Co-Authored-By: heihutu <heihutu@gmail.com>
Co-Authored-By: zhi22915 <qiuzgang@gmail.com>
Co-Authored-By: qunjiazhu <qunjiazhu@gmail.com>

---------

Co-authored-by: heihutu <heihutu@gmail.com>
Co-authored-by: zhi22915 <qiuzgang@gmail.com>
Co-authored-by: qunjiazhu <qunjiazhu@gmail.com>
2026-11-30 00:02:28 +08:00

914 lines
52 KiB
Python

#!/usr/bin/env python3
"""Select conservative PR scopes and verify the complete required job set."""
from __future__ import annotations
import fnmatch
import hashlib
import json
import posixpath
import os
from pathlib import Path, PurePosixPath
import re
import subprocess
import sys
import tomllib
import unittest
ROOT = Path(__file__).resolve().parent.parent
ALWAYS_JOBS = ("classify-changes", "typos", "quick-checks", "security-advisories", "security-input-gate")
CODE_JOBS = (
"workspace-clippy", "workspace-lint-and-build", "workspace-tests", "core-contract", "connect-feature-boundaries", "fips-provider",
"test-and-lint-protocols", "build-rustfs-debug-binary", "uring-integration",
"e2e-tests", "s3-implemented-tests", "s3-lifecycle-behavior-tests",
)
# Specialized boundary, hardware and alternate-feature lanes do not gate a
# merge. They run only in the scheduled (nightly) and manual full-validation
# runs, which report a regression within a day; repeating them on every PR
# revision and merge group bought no earlier signal worth the runner occupancy
# (Connect alone measured 53.5 median minutes per run and occasionally set the
# wall). Workspace lint and tests, the short core contract, the debug build,
# E2E smoke and the S3 lanes stay on the pull-request path because they cover
# the product's core correctness and S3 compatibility surface.
NIGHTLY_JOBS = ("connect-feature-boundaries", "fips-provider", "uring-integration", "test-and-lint-protocols")
# Bump when a plan produced by an older `select` can no longer be checked
# against this revision's expected job contract. The workflow refuses a plan
# that does not carry the current marker and falls back to the full matrix, so
# a base planner predating the contract cannot authorize a narrower selection.
PLAN_CONTRACT = "12"
# These jobs still exercise the server and its black-box harness when only E2E
# Rust sources change. Production code, manifests and shared test configuration
# always select the full matrix. No production package depends on e2e_test.
E2E_JOBS = (
"workspace-clippy", "workspace-lint-and-build", "workspace-tests", "core-contract", "build-rustfs-debug-binary", "e2e-tests",
"s3-implemented-tests", "s3-lifecycle-behavior-tests",
)
E2E_SELECTION_FILES = {
f".config/{profile}-selection.txt" for profile in (
"e2e-smoke", "e2e-full", "e2e-nightly", "e2e-repl-nightly",
"e2e-distributed", "e2e-protocols", "e2e-odm-interop",
)
}
# Clippy and the test-binary archive share no compiler output, so the lint runs
# beside the archive producer instead of in front of it. LINT_JOB is the only
# job that may lint the workspace; nothing downstream of ARCHIVE_PRODUCER may
# wait for it, or the lint is back on the path every workspace test waits on.
LINT_JOB = "workspace-clippy"
ARCHIVE_PRODUCER = "workspace-lint-and-build"
ARCHIVE_CONSUMERS = ("workspace-tests",)
SETUP = "./.github/actions/setup"
# The condition every cache-warm.yml build carries: Rust-cache does not save over an exact key match.
HIT_SKIP = "steps.setup.outputs.cache-hit != 'true'"
LINT_COMMANDS = ("cargo clippy --all-targets -- -D warnings", "python3 scripts/ci_test_artifact.py cargo lint --configuration workspace")
ADVISORIES_JOB = "security-advisories"
ADVISORIES_COMMAND = "python3 scripts/security/check_advisories.py"
SECURITY_INPUT_JOB = "security-input-gate"
SECURITY_INPUT_WORKFLOW = "./.github/workflows/fuzz.yml"
SECURITY_INPUT_REQUIRED_WITH = ("mode", "targets", "max_total_time", "base_sha", "head_sha", "event_name", "ref_name", "ci_mode")
OPTIONAL_JOBS = ("e2e-full",)
NON_VALIDATION_JOBS = {"required-checks", "cancel-closed-pr-runs", "alert-on-failure"}
def documentation_path(path: str) -> bool:
parts = PurePosixPath(path).parts
if not parts or path.startswith("/") or any(part in (".", "..") for part in parts) or any(ord(c) < 32 for c in path):
return False
if parts[-1] == "AGENTS.md":
return True
if len(parts) == 1 and (path.endswith(".md") or path == "LICENSE" or path.startswith("LICENSE-")):
return True
if path.startswith(("docs/", ".agents/skills/")) and path.endswith(".md"):
return True
return path.startswith("docs/") and path.endswith((".png", ".jpg", ".svg"))
POLICY_PATH = ".config/ci-plan.json"
SHA = re.compile(r"[0-9a-f]{40}")
EVENTS = ("pull_request", "push", "merge_group", "schedule", "workflow_dispatch")
def git(root: Path, *args: str) -> str:
return subprocess.check_output(["git", *args], cwd=root, stderr=subprocess.PIPE).decode("utf-8")
def git_file(root: Path, revision: str, path: str) -> str:
if not SHA.fullmatch(revision):
raise ValueError("revision must be a full commit SHA")
return git(root, "show", f"{revision}:{path}")
def release_metadata_only(root: Path, base: str, head: str, path: str) -> bool:
"""Recognize literal stable-version edits, never general packaging changes."""
stable = r"(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)"
marker = "<version>"
patterns = {
"flake.nix": [rf'(?m)^([ \t]*pname = "rustfs";\n[ \t]*version = ")({stable})(";)$'],
"helm/rustfs/Chart.yaml": [rf'(?m)^({key}: ")({stable})(")$' for key in ("version", "appVersion")],
"rustfs.spec": [rf'(?m)^(Version:[ \t]+)({stable})()$'],
}
if path not in patterns or not SHA.fullmatch(base) or not SHA.fullmatch(head):
return False
try:
normalized, versions = [], []
for revision in (base, head):
# A symlink, added/deleted file or executable-mode change is a
# build input even if its apparent text resembles metadata.
entry = git(root, "ls-tree", "-z", revision, "--", path)
if not re.fullmatch(rf"100644 blob [0-9a-f]{{40}}\t{re.escape(path)}\x00", entry):
return False
content, values = git_file(root, revision, path), []
# Existing markers could make edits to distinct fields collapse
# to identical normalized text.
if marker in content:
return False
for pattern in patterns[path]:
matches = list(re.finditer(pattern, content))
if len(matches) != 1:
return False
values.append(matches[0][2])
content = re.sub(pattern, lambda match: match[1] + marker + match[3], content)
if len(set(values)) != 1:
return False
normalized.append(content)
versions.append(values[0])
if path == "rustfs.spec":
old, new = (content.split("%changelog\n") for content in normalized)
if len(old) != 2 or len(new) != 2 or old[0] != new[0]:
return False
if old[1] == new[1]:
return True
# Only the standard prepended release note is non-executable;
# RPM macro syntax and edits to older history retain the full gate.
note = (r"\* (?:Mon|Tue|Wed|Thu|Fri|Sat|Sun) "
r"(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9]{2} [0-9]{4} "
r"[A-Za-z0-9 ._-]+ <[A-Za-z0-9._+-]+@[A-Za-z0-9.-]+>\n"
rf"- Update RPM package to RustFS {re.escape(versions[1])}\n\n")
return re.fullmatch(note + re.escape(old[1]), new[1]) is not None
return normalized[0] == normalized[1]
except (OSError, subprocess.CalledProcessError, UnicodeError, ValueError):
return False
def dependency_tables(document: dict):
for key, value in document.items():
if key in ("dependencies", "dev-dependencies", "build-dependencies"):
if not isinstance(value, dict):
raise ValueError("dependency table is not an object")
yield value
elif key == "target":
for target in value.values():
yield from dependency_tables(target)
def cargo_graph(root: Path, revision: str) -> tuple[dict[str, str], dict[str, set[str]], set[str]]:
"""Read Cargo inputs without resolving the network or executing candidate code.
All dependency kinds and target predicates are unioned. This intentionally
over-selects optional/target dependencies instead of guessing active cfgs.
Excluded path dependencies inform change propagation but are not build targets.
"""
workspace = tomllib.loads(git_file(root, revision, "Cargo.toml"))
files = set(git(root, "ls-tree", "-r", "--name-only", "-z", revision).rstrip("\0").split("\0"))
manifests = {path for path in files if path.endswith("/Cargo.toml")}
members = workspace.get("workspace", {}).get("members", [])
exclude_patterns = workspace.get("workspace", {}).get("exclude", [])
excluded = {path for path in manifests if any(fnmatch.fnmatchcase(path[:-11], pattern) for pattern in exclude_patterns)}
selected = {path for path in manifests - excluded if any(fnmatch.fnmatchcase(path[:-11], member) for member in members)}
if "package" in workspace:
selected.add("Cargo.toml")
if not selected:
raise ValueError("workspace has no packages")
for member in members:
if not any(fnmatch.fnmatchcase(path[:-11], member) for path in manifests):
raise ValueError(f"workspace member has no manifest: {member}")
shared = workspace.get("workspace", {}).get("dependencies", {})
packages, graph, documents = {}, {}, {}
pending = set(selected)
while pending:
manifest = pending.pop()
if manifest in documents:
continue
document = tomllib.loads(git_file(root, revision, manifest))
directory = posixpath.dirname(manifest)
name = document["package"]["name"]
if not isinstance(name, str) or not re.fullmatch(r"[A-Za-z0-9_-]+", name):
raise ValueError("invalid Cargo package name")
if name in packages and packages[name] != directory:
raise ValueError(f"duplicate workspace package {name}")
packages[name] = directory
documents[manifest] = (name, document)
graph[name] = set()
for table in dependency_tables(document):
for alias, value in table.items():
dependency = shared.get(alias) if isinstance(value, dict) and value.get("workspace") is True else value
if not isinstance(dependency, dict) or "path" not in dependency:
continue
owner = "" if isinstance(value, dict) and value.get("workspace") is True else directory
path = posixpath.normpath(posixpath.join(owner, dependency["path"], "Cargo.toml"))
if path.startswith("../") or path.startswith("/") or path not in files:
raise ValueError(f"unresolved workspace path dependency: {path}")
pending.add(path)
by_directory = {directory: name for name, directory in packages.items()}
for manifest, (name, document) in documents.items():
for table in dependency_tables(document):
for alias, value in table.items():
inherited = isinstance(value, dict) and value.get("workspace") is True
dependency = shared.get(alias) if inherited else value
if inherited and dependency is None:
raise ValueError(f"missing inherited dependency: {alias}")
if isinstance(dependency, dict) and "path" in dependency:
owner = "" if inherited else posixpath.dirname(manifest)
directory = posixpath.normpath(posixpath.join(owner, dependency["path"]))
graph[name].add(by_directory[directory])
else:
dependency_name = dependency.get("package", alias) if isinstance(dependency, dict) else alias
if dependency_name in packages:
graph[name].add(dependency_name)
return packages, graph, {name for manifest, (name, _) in documents.items() if manifest not in excluded}
def closure(seeds: set[str], graph: dict[str, set[str]], reverse: bool = False) -> set[str]:
reached = set(seeds)
while True:
expanded = reached | ({name for name, deps in graph.items() if deps & reached} if reverse
else {dep for name in reached for dep in graph.get(name, set())})
if expanded == reached:
return reached
reached = expanded
def expected_results(mode: str, event: str, ref: str, connect: bool = True, action: str = "") -> dict[str, str]:
if event not in EVENTS:
raise ValueError(f"unsupported CI event: {event!r}")
# `proven`: a push whose tree a successful merge-group run already validated.
if mode not in ("docs", "e2e", "full", "proven") or (mode != "full" and event != ("push" if mode == "proven" else "pull_request")):
raise ValueError(f"invalid CI selection: {mode!r} for {event!r}")
expected = {job: "success" for job in ALWAYS_JOBS}
expected.update({job: "success" if mode == "full" or (mode == "e2e" and job in E2E_JOBS) else "skipped" for job in CODE_JOBS})
nightly = mode == "full" and event in ("schedule", "workflow_dispatch")
expected.update({job: "success" if nightly else "skipped" for job in NIGHTLY_JOBS})
expected["connect-feature-boundaries"] = "success" if nightly and connect else "skipped"
full = mode == "full" and (event in ("merge_group", "workflow_dispatch") or (event == "push" and ref == "refs/heads/main"))
expected["e2e-full"] = "success" if full else "skipped"
if event == "pull_request" and action == "closed":
return {job: "skipped" for job in expected}
return expected
def read_policy(root: Path, revision: str) -> dict:
policy = json.loads(git_file(root, revision, POLICY_PATH))
if policy.get("schema_version") != 1 or policy.get("rollout") not in ("shadow", "enforced"):
raise ValueError("unsupported CI policy")
for field in ("core_packages", "connect_packages", "full_paths"):
if not isinstance(policy.get(field), list) or not all(isinstance(value, str) and value for value in policy[field]):
raise ValueError(f"invalid CI policy {field}")
return policy
def configurations(jobs: dict[str, str]) -> list[dict]:
values = [
("workspace-lint", LINT_JOB, "dev", []),
("workspace-tests", ARCHIVE_PRODUCER, "test", []),
("e2e-harness", "build-rustfs-debug-binary", "test", []),
("uring-tests", "uring-integration", "test", []),
("server-e2e-hooks", "build-rustfs-debug-binary", "dev", ["e2e-test-hooks"]),
("connect-debug", "connect-feature-boundaries", "test", ["connect-e2e-short-credentials"]),
("connect-offline-cli", "connect-feature-boundaries", "dev", ["offline-enrollment-e2e-root"]),
("connect-offline-tests", "connect-feature-boundaries", "test", ["offline-enrollment-e2e-root"]),
("connect-release", "connect-feature-boundaries", "release", []),
("connect-release-rejection", "connect-feature-boundaries", "release", ["connect-e2e-short-credentials"]),
("fips-provider-default-lint", "fips-provider", "dev", []),
("fips-provider-fips-lint", "fips-provider", "dev", ["fips"]),
("fips-tls-runtime-default", "fips-provider", "test", []),
("fips-tls-runtime-fips", "fips-provider", "test", ["fips"]),
("fips-protocols", "fips-provider", "test", ["ftps", "webdav", "fips"]),
("fips-targets", "fips-provider", "test", ["fips"]),
("fips-s3-client", "fips-provider", "test", ["fips"]),
("fips-targets-default", "fips-provider", "test", []),
("fips-s3-client-default", "fips-provider", "test", []),
("fips-provider-default", "fips-provider", "test", []),
("fips-provider-fips", "fips-provider", "test", ["fips"]),
("protocol-swift-lint", "test-and-lint-protocols", "dev", ["swift"]),
("protocol-swift-tests", "test-and-lint-protocols", "test", ["swift"]),
("protocol-sftp-lint", "test-and-lint-protocols", "dev", ["sftp"]),
("protocol-sftp-tests", "test-and-lint-protocols", "test", ["sftp"]),
("protocol-http3-lint", "test-and-lint-protocols", "dev", ["http3", "swift"]),
("protocol-http3-tests", "test-and-lint-protocols", "test", ["http3", "swift"]),
]
return [{"id": identity, "job": job, "target": "x86_64-unknown-linux-gnu", "profile": profile,
"default_features": True, "features": features} for identity, job, profile, features in values if jobs[job] == "success"]
def select_plan(event: str, base: str, head: str, ref: str, root: Path, action: str = "", *, proof: dict | None = None) -> dict:
if event not in EVENTS or not SHA.fullmatch(head):
raise ValueError("unsupported event or missing candidate SHA")
tree = git(root, "rev-parse", f"{head}^{{tree}}").strip()
if not SHA.fullmatch(tree):
raise ValueError("candidate tree cannot be resolved")
head_packages, head_graph, head_members = cargo_graph(root, head)
build_packages = head_members - {"e2e_test"}
all_packages = sorted(build_packages)
if not all_packages:
raise ValueError("candidate has no workspace validation packages")
policy_revision = base if event == "pull_request" and SHA.fullmatch(base) else head
reasons, changed, mode = [], [], "full"
policy = None
full = event != "pull_request"
try:
policy = read_policy(root, policy_revision)
base_packages, base_graph, _ = cargo_graph(root, base) if SHA.fullmatch(base) else (head_packages, head_graph, head_members)
if event == "pull_request":
if not SHA.fullmatch(base):
raise ValueError("missing base SHA")
changed = git(root, "diff", "--no-ext-diff", "--no-textconv", "--no-renames", "--name-only", "-z", base, head, "--").rstrip("\0").split("\0")
if not changed or changed == [""]:
raise ValueError("empty change range")
else:
reasons.append(f"{event} validates the complete candidate")
except (OSError, subprocess.CalledProcessError, UnicodeError, KeyError, TypeError, ValueError) as error:
full = True
base_packages, base_graph = head_packages, head_graph
reasons.append(f"conservative fallback: {type(error).__name__}")
graph = {name: head_graph.get(name, set()) | base_graph.get(name, set()) for name in set(head_graph) | set(base_graph)}
affected, non_runtime = set(), set()
if not full and policy is not None:
for path in changed:
if documentation_path(path):
non_runtime.add(path)
continue
if (policy["rollout"] == "enforced"
and not any(fnmatch.fnmatchcase(path, pattern) for pattern in policy["full_paths"])
and release_metadata_only(root, base, head, path)):
non_runtime.add(path)
reasons.append(f"literal stable release metadata: {path}")
continue
if path in E2E_SELECTION_FILES:
affected.add("e2e_test")
continue
# Cargo's graph cannot describe include_bytes!, fixture readers or
# build-script inputs shared across packages. Narrow only Rust
# sources; all other package-owned inputs retain the full gate.
if (not path.endswith(".rs") or PurePosixPath(path).name == "build.rs"
or "fixtures" in PurePosixPath(path).parts
or any(fnmatch.fnmatchcase(path, pattern) for pattern in policy["full_paths"])):
full = True
reasons.append(f"shared build or policy input: {path}")
continue
owners = {name for packages in (base_packages, head_packages) for name, directory in packages.items()
if directory and path.startswith(directory + "/")}
if not owners or path.startswith("/") or any(part in (".", "..") for part in path.split("/")) or any(ord(c) < 32 for c in path):
full = True
reasons.append(f"unmapped input: {path}")
affected.update(owners)
if not affected and not full:
mode = "docs"
reasons.append("all changes are documentation or literal stable release metadata")
elif affected == {"e2e_test"} and not full and not (closure({"e2e_test"}, graph, reverse=True) - {"e2e_test"}) and all(
path in non_runtime or path in E2E_SELECTION_FILES or
(path.startswith("crates/e2e_test/src/") and path.endswith(".rs")) for path in changed):
mode = "e2e"
reasons.append("isolated E2E harness changes")
core = set(policy["core_packages"]) if policy else set()
try:
# Union both inventories: a PR may add tests, but cannot remove baseline responsibility.
for revision in {policy_revision, head}:
inventory = json.loads(git_file(root, revision, ".config/ecstore-required-tests.json"))
if not inventory["tests"]:
raise ValueError("empty core test inventory")
core.update(test["suite"].split("::", 1)[0] for test in inventory["tests"])
if not core <= head_members:
raise ValueError("core package missing from candidate")
except (subprocess.CalledProcessError, KeyError, TypeError, ValueError):
full = True
mode = "full"
reasons.append("core inventory cannot be resolved; require the full workspace")
selected = (closure(affected, graph, reverse=True) | core) & build_packages
if full:
selected = build_packages
if mode == "docs":
selected = set()
connect_inputs = closure(set(policy["connect_packages"]), graph) if policy else set(head_packages)
connect = mode == "full" and (full or "rustfs" in affected or bool(affected & connect_inputs))
reasons.append("selected changed packages, reverse dependencies and fixed core contracts")
reasons.append("Connect boundary or a shared input changed" if connect else "Connect has no changed credential/configuration dependency")
proposed = {"packages": sorted(selected), "connect": connect, "full_validation": full, "reasons": reasons,
"jobs": expected_results(mode, event, ref, connect, action)}
shadow = policy is None or policy["rollout"] == "shadow"
effective_packages = all_packages if shadow and mode != "docs" else sorted(selected)
effective_connect = mode == "full" if shadow else connect
effective_full = mode == "full" if shadow else full
proven = event == "push" and isinstance(proof, dict) and proof.get("tree") == tree
if proven:
mode, effective_connect, effective_full = "proven", False, False
reasons.append(f"merge_group run {proof['url']} already validated tree {tree}")
jobs = expected_results(mode, event, ref, effective_connect, action)
plan = {"schema_version": 1, "event": event, "action": action, "ref": ref, "base": base if SHA.fullmatch(base) else "",
"head": head, "tree": tree, "policy": {"revision": policy_revision, "rollout": "shadow" if shadow else "enforced"},
"mode": mode, "packages": effective_packages, "full_validation": effective_full, "connect": effective_connect,
"configurations": configurations(jobs), "jobs": jobs, "proposed": proposed,
"reasons": ["shadow rollout preserves current validation responsibility"] + reasons if shadow else reasons,
"not_applicable": {job: f"proven by {proof['url']}" if proven else f"not required for {mode}/{event}"
for job, result in jobs.items() if result == "skipped"},
"proven_by": proof if proven else None}
return plan
def validate_plan(plan: object, event: str, ref: str, head: str = "", tree: str = "", base: str | None = None, action: str | None = None) -> dict[str, str]:
if not isinstance(plan, dict) or type(plan.get("schema_version")) is not int or plan["schema_version"] != 1:
raise ValueError("invalid plan schema")
if plan.get("event") != event or plan.get("ref") != ref:
raise ValueError("plan event/ref does not match this run")
if not isinstance(plan.get("base"), str) or plan["base"] and not SHA.fullmatch(plan["base"]):
raise ValueError("invalid plan base identity")
if not isinstance(plan.get("action"), str):
raise ValueError("invalid plan event action")
if (base is not None and plan["base"] != base) or (action is not None and plan["action"] != action):
raise ValueError("plan base/action does not match this run")
for field in ("head", "tree"):
if not isinstance(plan.get(field), str) or not SHA.fullmatch(plan[field]):
raise ValueError(f"invalid plan {field}")
if (head and plan["head"] != head) or (tree and plan["tree"] != tree):
raise ValueError("plan candidate identity does not match this run")
if any(type(plan.get(name)) is not bool for name in ("connect", "full_validation")):
raise ValueError("invalid plan selection flags")
policy = plan.get("policy")
if not isinstance(policy, dict) or policy.get("rollout") not in ("shadow", "enforced") or not isinstance(policy.get("revision"), str) or not SHA.fullmatch(policy["revision"]):
raise ValueError("invalid plan policy identity")
if policy["rollout"] == "shadow" and plan.get("mode") == "full" and (not plan["connect"] or not plan["full_validation"]):
raise ValueError("shadow plan cannot reduce full validation responsibility")
packages = plan.get("packages")
if not isinstance(packages, list) or not all(isinstance(name, str) and re.fullmatch(r"[A-Za-z0-9_-]+", name) for name in packages) or len(packages) != len(set(packages)):
raise ValueError("invalid plan packages")
if plan.get("mode") != "docs" and not packages:
raise ValueError("code validation plan must not be empty")
proof = plan.get("proven_by")
if (proof is not None or plan.get("mode") == "proven") and not (
isinstance(proof, dict) and plan.get("mode") == "proven" and proof.get("tree") == plan["tree"]
and not plan["connect"] and not plan["full_validation"]
and type(proof.get("run_id")) is int and proof["run_id"] > 0
and re.fullmatch(rf"https://github\.com/[\w.-]+/[\w.-]+/actions/runs/{proof['run_id']}", str(proof.get("url")))):
raise ValueError("a proven push requires the merge-group run that validated its tree")
expected = expected_results(plan.get("mode"), event, ref, plan["connect"], plan.get("action", ""))
if plan.get("jobs") != expected:
raise ValueError("plan job contract differs from selected responsibilities")
if plan.get("configurations") != configurations(expected):
raise ValueError("plan configuration matrix is incomplete")
if not isinstance(plan.get("reasons"), list) or not plan["reasons"] or not all(isinstance(reason, str) and reason for reason in plan["reasons"]):
raise ValueError("plan selection has no explanation")
not_applicable = plan.get("not_applicable")
if not isinstance(not_applicable, dict) or not all(isinstance(reason, str) and reason for reason in not_applicable.values()) or set(not_applicable) != {job for job, value in expected.items() if value == "skipped"}:
raise ValueError("plan skips lack explicit not-applicable reasons")
return expected
def verify_results(needs: object, event: str, ref: str, head: str = "", tree: str = "", base: str | None = None, action: str | None = None) -> list[str]:
if not isinstance(needs, dict):
return ["needs must be a job-result object"]
selection = needs.get("classify-changes", {})
outputs = selection.get("outputs", {}) if isinstance(selection, dict) else {}
try:
raw = outputs.get("plan") if isinstance(outputs, dict) else None
if raw:
plan = json.loads(raw)
expected = validate_plan(plan, event, ref, head, tree, base, action)
if outputs.get("mode") != plan["mode"]:
raise ValueError("plan mode differs from scheduling output")
for name in ("connect", "full_validation"):
if name in outputs and outputs[name] != str(plan[name]).lower():
raise ValueError(f"plan {name} differs from scheduling output")
if outputs.get("packages") and json.loads(outputs["packages"]) != plan["packages"]:
raise ValueError("plan packages differ from scheduling output")
else:
# During adoption the trusted base script only emits `mode`. No
# legacy output may authorize an omitted job in the new workflow.
if not isinstance(outputs, dict) or outputs.get("mode") != "full":
raise ValueError("missing plan requires conservative full mode")
expected = expected_results("full", event, ref)
except (TypeError, ValueError) as error:
return [str(error)]
errors = []
if set(needs) != set(expected):
errors.append(f"job set differs: missing={sorted(set(expected) - set(needs))}, unexpected={sorted(set(needs) - set(expected))}")
for job, required in expected.items():
result = needs.get(job, {})
actual = result.get("result") if isinstance(result, dict) else None
if actual != required:
errors.append(f"{job}: expected {required}, got {actual!r}")
return errors
def workflow_document(text: str) -> dict:
try:
import yaml
except ImportError as error:
raise ValueError("workflow checking requires PyYAML") from error
class UniqueLoader(yaml.BaseLoader):
pass
def mapping(loader, node):
result = {}
for key_node, value_node in node.value:
key = loader.construct_object(key_node)
if key in result:
raise ValueError(f"duplicate YAML key: {key}")
result[key] = loader.construct_object(value_node)
return result
UniqueLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, mapping)
try:
value = yaml.load(text, Loader=UniqueLoader)
except yaml.YAMLError as error:
raise ValueError(f"invalid workflow YAML: {error}") from error
if not isinstance(value, dict):
raise ValueError("workflow must be a mapping")
return value
def condition(expression: str | bool, context: dict) -> bool:
"""Evaluate the finite condition grammar used by CI, never Python eval."""
if isinstance(expression, bool):
return expression
expression = expression.strip()
if expression.startswith("${{") and expression.endswith("}}"):
expression = expression[3:-2].strip()
token = re.compile(r"\s*(?:('(?:[^']|'')*')|([A-Za-z_][A-Za-z_0-9.-]*)|(&&|\|\||==|!=|[!(),]))")
values, cursor = [], 0
while cursor < len(expression):
match = token.match(expression, cursor)
if not match:
raise ValueError(f"unsupported condition near {expression[cursor:]!r}")
values.append(next(value for value in match.groups() if value is not None))
cursor = match.end()
position = 0
def atom():
nonlocal position
if position >= len(values):
raise ValueError("incomplete condition")
value = values[position]
position += 1
if value == "!":
return not atom()
if value == "(":
result = parse(0)
if position >= len(values) or values[position] != ")":
raise ValueError("unclosed condition")
position += 1
return result
if value.startswith("'"):
return value[1:-1].replace("''", "'")
if position < len(values) and values[position] == "(":
position += 1
arguments = []
while position < len(values) and values[position] != ")":
arguments.append(parse(0))
if position < len(values) and values[position] == ",":
position += 1
else:
break
if position >= len(values) or values[position] != ")":
raise ValueError("unclosed function")
position += 1
if value == "contains" and len(arguments) == 2:
return arguments[1] in arguments[0]
if value == "fromJSON" and len(arguments) == 1:
return json.loads(arguments[0])
if value in ("always", "success", "failure", "cancelled") and not arguments:
return True if value == "always" else context.get(value, False)
raise ValueError(f"unsupported condition function: {value}")
if value in ("true", "false"):
return value == "true"
if value not in context:
raise ValueError(f"unknown condition input: {value}")
return context[value]
precedence = {"||": 1, "&&": 2, "==": 3, "!=": 3}
def parse(minimum):
nonlocal position
left = atom()
while position < len(values) and precedence.get(values[position], 0) > minimum:
operator = values[position]
position += 1
right = parse(precedence[operator])
if operator == "||":
left = left or right
elif operator == "&&":
left = left and right
elif operator == "==":
left = left == right
else:
left = left != right
return left
result = parse(0)
if position != len(values):
raise ValueError("unexpected condition tokens")
return bool(result)
def job_needs(jobs: dict, job: str) -> list[str]:
dependencies = jobs.get(job, {}).get("needs", [])
return [dependencies] if isinstance(dependencies, str) else list(dependencies)
def check_lint_lane(jobs: dict) -> list[str]:
"""Keep workspace Clippy gating every code event without delaying the test archive."""
errors = []
lint_steps = {job: [step for step in block.get("steps", [])
if any(command in step.get("run", "") for command in LINT_COMMANDS)]
for job, block in jobs.items()}
clippy = lint_steps.get(LINT_JOB, [])
if len(clippy) != 1 or any("if" in step for step in clippy):
errors.append("workspace Clippy must run on every code event, pull requests included")
if any(steps for job, steps in lint_steps.items() if job != LINT_JOB):
errors.append(f"workspace Clippy belongs to {LINT_JOB} alone")
lint, producer = jobs.get(LINT_JOB, {}), jobs.get(ARCHIVE_PRODUCER, {})
# The lint replaces a step of the producer; it must keep that job's scope.
if not lint or lint.get("if") != producer.get("if"):
errors.append(f"{LINT_JOB} must run under the same condition as {ARCHIVE_PRODUCER}")
for job in (ARCHIVE_PRODUCER, *ARCHIVE_CONSUMERS):
reached, pending = set(), [job]
while pending:
current = pending.pop()
if current not in reached:
reached.add(current)
pending.extend(job_needs(jobs, current))
if LINT_JOB in reached:
errors.append(f"{job} must not wait for {LINT_JOB}")
if ARCHIVE_PRODUCER not in job_needs(jobs, "workspace-tests"):
errors.append(f"workspace-tests must consume the archive of {ARCHIVE_PRODUCER}")
return errors
def ci_dev_workflows(root: Path) -> tuple[dict[str, dict], dict]:
"""The jobs of every workflow that reads ci-dev, by file name, and cache-warm.yml's jobs."""
reader_files = (
"ci.yml",
"dual-stack-nightly.yml",
"e2e-replication-nightly.yml",
"fuzz.yml",
"gateway-acceptance.yml",
"mint.yml",
"reference-parity.yml",
)
readers = {name: workflow_document((root / ".github/workflows" / name).read_text())["jobs"] for name in reader_files}
return readers, workflow_document((root / ".github/workflows/cache-warm.yml").read_text())["jobs"]
def cache_warm_errors(readers: dict[str, dict], warm_jobs: dict) -> list[str]:
"""Keep ci-dev holding the readers' own builds under a key they share, and stop warm jobs rebuilding a stored key.
`readers` maps each workflow that reads ci-dev to its jobs; ci.yml's archive builds define the selections.
"""
errors = []
# Only the steps that run whenever the key is written count; the --timings step runs on request.
builds = [step.get("run", "") for step in warm_jobs.get("warm-ci-dev", {}).get("steps", []) if step.get("if") == HIT_SKIP]
def selections(command: str, text: str) -> set[str]:
return set(re.findall(rf"scripts/ci_test_artifact\.py {command} --configuration[= ](\S+)", text))
archived = {name for block in readers["ci.yml"].values() for step in block.get("steps", []) for name in selections("build", step.get("run", ""))}
warmed = selections("cargo warm", "\n".join(builds))
errors += [f"warm-ci-dev must build the {name} archive selection" for name in sorted(archived - warmed)]
errors += [f"warm-ci-dev builds the {name} selection, which no ci.yml job archives" for name in sorted(warmed - archived)]
if not any(LINT_COMMANDS[1] in run for run in builds):
errors.append("warm-ci-dev must run the workspace lint command")
# Rust-cache keeps an entry until its key changes. The key names a digest of the warm commands, so changing
# what is warmed writes a new entry instead of leaving readers on the old one; every reader names the same key.
key = "ci-dev-" + hashlib.sha256(json.dumps(builds).encode()).hexdigest()[:12]
for workflow, jobs in {"cache-warm.yml": {"warm-ci-dev": warm_jobs.get("warm-ci-dev", {})}, **readers}.items():
for job, block in jobs.items():
used = next((step.get("with", {}).get("cache-shared-key") for step in block.get("steps", []) if step.get("uses") == SETUP), None)
if (job == "warm-ci-dev" or str(used).startswith("ci-dev")) and used != key:
errors.append(f"{workflow} {job} uses cache key {used}; the ci-dev key for these warm commands is {key}")
# Rust-cache does not save over an exact key match, so such a build is lost.
for job, block in warm_jobs.items():
steps = block.get("steps", [])
restored = any(step.get("id") == "setup" and step.get("uses") == SETUP for step in steps)
if any(re.search(r"(^|\s)cargo\s|ci_test_artifact\.py", step.get("run", ""), re.M) and step.get("if") != "inputs.emit_timings"
and (not restored or step.get("if") != HIT_SKIP) for step in steps):
errors.append(f"{job} builds after an exact cache hit")
return errors
def check_plan_contract_marker(jobs: dict) -> list[str]:
"""The workflow's fallback must test for the marker this revision's planner emits."""
scopes = [step for step in jobs.get("classify-changes", {}).get("steps", []) if step.get("id") == "scope"]
markers = re.findall(r"grep -q '\^plan_contract=([^$']*)\$'", scopes[0].get("run", "")) if len(scopes) == 1 else []
if markers != [PLAN_CONTRACT]:
return [f"classify-changes must fall back to the full matrix unless the plan carries contract {PLAN_CONTRACT}"]
return []
def check_advisories_lane(jobs: dict) -> list[str]:
"""Keep security-advisories running the shared fail-closed checker without clock overrides."""
errors = []
block = jobs.get(ADVISORIES_JOB, {})
if block.get("runs-on") != "ubuntu-latest":
errors.append(f"{ADVISORIES_JOB} must run on ubuntu-latest")
steps = [step for step in block.get("steps", []) if ADVISORIES_COMMAND in step.get("run", "")]
if len(steps) != 1 or "if" in steps[0]:
errors.append(f"{ADVISORIES_JOB} must unconditionally run {ADVISORIES_COMMAND}")
else:
body = steps[0].get("run", "")
if "|| true" in body or "--now" in body or "--allow-ci-clock-override-for-test" in body:
errors.append(f"{ADVISORIES_JOB} must not mask failures or override the CI clock")
return errors
def check_security_input_gate_lane(jobs: dict) -> list[str]:
"""Keep security-input-gate calling the reusable fuzz workflow with bounded inputs."""
errors = []
block = jobs.get(SECURITY_INPUT_JOB, {})
if block.get("name") != "Security Input Gate":
errors.append(f"{SECURITY_INPUT_JOB} must keep stable display name 'Security Input Gate'")
if block.get("uses") != SECURITY_INPUT_WORKFLOW:
errors.append(f"{SECURITY_INPUT_JOB} must call reusable workflow {SECURITY_INPUT_WORKFLOW}")
if "classify-changes" not in job_needs(jobs, SECURITY_INPUT_JOB):
errors.append(f"{SECURITY_INPUT_JOB} must depend on classify-changes for ci_mode")
inputs = block.get("with", {})
if not isinstance(inputs, dict):
errors.append(f"{SECURITY_INPUT_JOB} must pass typed with inputs")
else:
missing = [key for key in SECURITY_INPUT_REQUIRED_WITH if key not in inputs]
extra = sorted(set(inputs) - set(SECURITY_INPUT_REQUIRED_WITH))
if missing:
errors.append(f"{SECURITY_INPUT_JOB} missing required with inputs: {', '.join(missing)}")
if extra:
errors.append(f"{SECURITY_INPUT_JOB} has unexpected with inputs: {', '.join(extra)}")
if inputs.get("mode") != "smoke":
errors.append(f"{SECURITY_INPUT_JOB} must invoke fuzz.yml in smoke mode")
return errors
def check_workflow(root: Path) -> list[str]:
try:
document = workflow_document((root / ".github/workflows/ci.yml").read_text())
jobs = document["jobs"]
if not isinstance(jobs, dict):
raise ValueError("jobs must be a mapping")
except (OSError, KeyError, TypeError, ValueError) as error:
return [str(error)]
errors = []
required = set(ALWAYS_JOBS + CODE_JOBS + OPTIONAL_JOBS)
if set(jobs) - NON_VALIDATION_JOBS != required:
errors.append("CI verification jobs and the required gate contract differ")
for job in required | {"required-checks"}:
block = jobs.get(job, {})
for item in [block, *block.get("steps", [])]:
if item.get("continue-on-error", "false") != "false":
errors.append(f"{job} cannot convert a validation failure into success")
gate = jobs.get("required-checks", {})
dependencies = gate.get("needs", [])
if not isinstance(dependencies, list) or set(dependencies) != required or len(dependencies) != len(required):
errors.append("required-checks must directly depend on every verification job exactly once")
if gate.get("name") != "Test and Lint" or sum(block.get("name") == "Test and Lint" for block in jobs.values()) != 1:
errors.append("Test and Lint must uniquely name the aggregate gate")
for job, block in jobs.items():
if not isinstance(block.get("name"), str):
errors.append(f"{job} needs a stable display name")
name = block.get("name", "")
if "${{" in name and any(value.strip() not in ("matrix.features.name",) for value in re.findall(r"\$\{\{(.*?)\}\}", name)):
errors.append(f"{job} has an unverifiable dynamic display name")
verifiers = [step for step in gate.get("steps", []) if "scripts/ci_gate.py verify" in step.get("run", "")]
if len(verifiers) != 1 or (verifiers[0].get("run", "").strip() != "python3 scripts/ci_gate.py verify"
or verifiers[0].get("env", {}).get("CI_NEEDS") != "${{ toJSON(needs) }}"
or verifiers[0].get("shell") != "bash" or "if" in verifiers[0]):
errors.append("required-checks must unconditionally verify actual needs and preserve its exit status")
errors.extend(check_lint_lane(jobs))
errors.extend(check_advisories_lane(jobs))
errors.extend(check_security_input_gate_lane(jobs))
errors.extend(check_plan_contract_marker(jobs))
matrix = jobs.get("test-and-lint-protocols", {}).get("strategy", {}).get("matrix", {})
features = matrix.get("features", [])
if not isinstance(features, list) or {item.get("name"): item.get("flags") for item in features} != {
"swift": "--features swift", "sftp": "--features sftp", "http3": "--features http3,swift",
"internode-transport": "--features rustfs/internode-transport"} or len(features) != 4:
errors.append("protocol matrix must retain every required feature configuration")
triggers = document.get("on", {})
if isinstance(triggers, dict) and isinstance(triggers.get("pull_request"), dict) and any(key in triggers["pull_request"] for key in ("paths", "paths-ignore")):
errors.append("all pull requests must enter the single CI workflow")
if (root / ".github/workflows/ci-docs-only.yml").exists():
errors.append("the duplicate required-status companion must be removed")
# Truth tables protect behavior while allowing equivalent expression and YAML formatting.
for event in EVENTS:
for mode in {"pull_request": ("docs", "e2e", "full"), "push": ("full", "proven")}.get(event, ("full",)):
for ref in ("refs/heads/main", "refs/heads/release"):
for connect in (True, False) if event == "pull_request" and mode == "full" else (mode == "full",):
for action in ("opened", "closed") if event == "pull_request" else ("",):
expected = expected_results(mode, event, ref, connect, action)
context = {"github.event_name": event, "github.ref": ref, "github.event.action": action,
"needs.classify-changes.outputs.mode": mode, "needs.classify-changes.outputs.connect": str(connect).lower(),
"success": True}
evaluated, evaluating = {}, set()
def scheduled(job):
if job in evaluated:
return evaluated[job]
if job in evaluating or job not in jobs:
raise ValueError(f"cyclic or unknown job dependency: {job}")
evaluating.add(job)
block = jobs[job]
dependencies = block.get("needs", [])
if isinstance(dependencies, str):
dependencies = [dependencies]
dependency_results = [scheduled(dependency) for dependency in dependencies]
success = all(dependency_results)
expression = block.get("if", "success()")
unquoted = re.sub(r"'(?:[^']|'')*'", "", expression)
status_function = re.search(r"\b(always|success|failure|cancelled)\s*\(", unquoted)
actual = condition(expression, dict(context, success=success))
evaluated[job] = actual and (bool(status_function) or success)
evaluating.remove(job)
return evaluated[job]
for job in required | {"required-checks"}:
should_run = expected.get(job, "success") == "success" if job != "required-checks" else action != "closed"
try:
actual = scheduled(job)
except (KeyError, TypeError, ValueError) as error:
errors.append(f"{job}: {error}")
continue
if actual != should_run:
errors.append(f"{job} condition differs from plan for {event}/{mode}/{connect}/{action}/{ref}")
for status in ("failure", "cancelled"):
context.update(success=False, failure=status == "failure", cancelled=status == "cancelled")
try:
if condition(gate.get("if", "true"), context) != (action != "closed") or not re.search(r"\balways\s*\(", re.sub(r"'(?:[^']|'')*'", "", gate.get("if", ""))):
errors.append("required-checks must run after failed or skipped dependencies")
except (TypeError, ValueError) as error:
errors.append(str(error))
return sorted(set(errors))
def main() -> int:
if sys.argv[1:] == ["--self-test"]:
suite = unittest.defaultTestLoader.discover(str(ROOT / "scripts"), pattern="test_ci_plan.py")
return not unittest.TextTestRunner(verbosity=2).run(suite).wasSuccessful()
if sys.argv[1:] == ["select"]:
try:
# scripts/ci_merge_group_proof.py output; select_plan accepts it only for a push of the same tree.
try:
proof = json.loads(os.environ.get("CI_MERGE_GROUP_PROOF") or "null")
except ValueError:
proof = None
plan = select_plan(os.environ.get("GITHUB_EVENT_NAME", ""), os.environ.get("CI_BASE_SHA", ""),
os.environ.get("GITHUB_SHA", ""), os.environ.get("GITHUB_REF", ""), Path.cwd(),
os.environ.get("CI_EVENT_ACTION", ""), proof=proof)
with open(os.environ["GITHUB_OUTPUT"], "a") as output:
for name, value in (("mode", plan["mode"]), ("plan", json.dumps(plan, separators=(",", ":"))),
("connect", str(plan["connect"]).lower()), ("packages", json.dumps(plan["packages"], separators=(",", ":"))),
("full_validation", str(plan["full_validation"]).lower()),
("plan_contract", PLAN_CONTRACT)):
output.write(f"{name}={value}\n")
print(json.dumps(plan, indent=2))
if os.environ.get("GITHUB_STEP_SUMMARY"):
with open(os.environ["GITHUB_STEP_SUMMARY"], "a") as summary:
summary.write("### CI validation plan\n\n```json\n" + json.dumps(plan, indent=2) + "\n```\n")
return 0
except (OSError, subprocess.CalledProcessError, UnicodeError, KeyError, TypeError, ValueError) as error:
print(f"ERROR: cannot safely plan CI: {error}", file=sys.stderr)
return 1
if sys.argv[1:] == ["verify"]:
try:
head = os.environ.get("GITHUB_SHA", "")
tree = git(Path.cwd(), "rev-parse", f"{head}^{{tree}}").strip() if SHA.fullmatch(head) else ""
event = os.environ.get("GITHUB_EVENT_NAME", "")
base, action = None, None
if os.environ.get("GITHUB_EVENT_PATH"):
payload = json.loads(Path(os.environ["GITHUB_EVENT_PATH"]).read_text())
base = (payload["pull_request"]["base"]["sha"] if event == "pull_request" else
payload["merge_group"]["base_sha"] if event == "merge_group" else payload.get("before", ""))
action = payload.get("action", "")
needs = json.loads(os.environ["CI_NEEDS"])
errors = verify_results(needs, event, os.environ.get("GITHUB_REF", ""), head, tree, base, action)
plan = json.loads(needs["classify-changes"]["outputs"].get("plan") or "null")
if not errors and isinstance(plan, dict) and plan["mode"] == "proven":
# The check list shows these jobs as skipped like any other; say why.
note = f"Validation jobs were skipped as proven: merge_group run {plan['proven_by']['url']} passed tree {plan['tree']}."
print(note)
if os.environ.get("GITHUB_STEP_SUMMARY"):
with open(os.environ["GITHUB_STEP_SUMMARY"], "a") as summary:
summary.write(note + "\n")
except (OSError, KeyError, TypeError, ValueError, subprocess.CalledProcessError) as error:
errors = [str(error)]
elif sys.argv[1:] == ["--check-workflow"]:
errors = check_workflow(ROOT)
try:
errors += cache_warm_errors(*ci_dev_workflows(ROOT))
except (OSError, KeyError, TypeError, ValueError) as error:
errors.append(f"ci-dev cache: {error}")
else:
print("usage: ci_gate.py {select|verify|--check-workflow|--self-test}", file=sys.stderr)
return 2
for error in errors:
print(f"ERROR: {error}", file=sys.stderr)
if not errors:
print("CI contract passed")
return bool(errors)
if __name__ == "__main__":
raise SystemExit(main())