Wave 352/509: Kernel rig: each area runs only when the files its PATHS lists change, every area on the shared core (and 3 more)
4 commits, squashed; the originals stay reachable from tag archive/merge-main-develop. - e71177769 jyviko: Docs: the kernel rig gate, updating a known-failing list, re-recording the ladder golden - d84d00bb4 jyviko: Release gate: the packages' dry run is unsigned and skips the release job - 02e1f1841 jyviko: Kernel rig: each area runs only when the files its PATHS lists change, every area on the shared core - b67e93211 jyviko: Kernel rig: an area's PATHS names only its own files, never the shared core Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DtL5M41G8Nua7HvuFPZkmJ
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Select the kernel rig areas a pull request affects (.github/workflows/kernel-rig.yml).
|
||||
|
||||
Usage: kernel_rig_plan.py (--all | --paths FILE) [--root DIR] [--github-output FILE]
|
||||
|
||||
FILE holds the changed paths relative to the repository root, separated by NUL (git diff -z);
|
||||
"-" reads standard input. --all selects every area (pushes, merge groups, the release gate, a run
|
||||
by hand). --root is the repository (default: two folders up from this script).
|
||||
|
||||
The areas are the folders of tests/kernel-cases/ (the ladder aside), so a new area joins by
|
||||
having a folder with its cases, its KNOWN_FAILING and its PATHS. A changed path selects:
|
||||
- every area, when it matches tests/kernel-cases/PATHS-ALL (the shared core: SMLib, geom, the
|
||||
kernel's core, the rig's grader and runner, the workflow);
|
||||
- an area, when it is under tests/kernel-cases/<area>/ or matches that area's PATHS: one git
|
||||
pathspec glob a line (`*` within a folder, `**` across folders), `#` starts a comment.
|
||||
The ladder spans every operation: it runs whenever any area does, and on a change under
|
||||
tests/kernel-cases/ladder/.
|
||||
|
||||
The coverage check (`cargo xtask kernel-cases --coverage`) runs in the build job. The build runs
|
||||
when a shard does, or when the command registry or the cases may have changed (COVERAGE below),
|
||||
so a new command without a case fails on its own pull request.
|
||||
|
||||
Prints and optionally appends to $GITHUB_OUTPUT:
|
||||
shards=["booleans", ..., "ladder"] the shard matrix, JSON (empty list: no shard runs)
|
||||
build=true|false the build job (and the coverage check) runs
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
CASES = "tests/kernel-cases"
|
||||
LADDER = "ladder"
|
||||
# Changes here may add or remove a registry command, or the case that covers one: the coverage
|
||||
# check must see them even when no area runs.
|
||||
COVERAGE = (
|
||||
"crates/engine/src/cmd/**",
|
||||
"tests/kernel-cases/EXEMPT",
|
||||
"tests/kernel-cases/**/recipe.json",
|
||||
"xtask/src/kernel_cases.rs",
|
||||
)
|
||||
|
||||
|
||||
def glob_regex(glob):
|
||||
"""A git pathspec glob (`:(glob)` magic) as a regular expression over a whole path: `**/`
|
||||
matches any folders (none too), a trailing `/**` everything below, `*` and `?` stay within
|
||||
one folder."""
|
||||
out, i = [], 0
|
||||
while i < len(glob):
|
||||
if glob.startswith("**/", i):
|
||||
out.append("(?:.*/)?")
|
||||
i += 3
|
||||
elif glob.startswith("**", i):
|
||||
out.append(".*")
|
||||
i += 2
|
||||
elif glob[i] == "*":
|
||||
out.append("[^/]*")
|
||||
i += 1
|
||||
elif glob[i] == "?":
|
||||
out.append("[^/]")
|
||||
i += 1
|
||||
else:
|
||||
out.append(re.escape(glob[i]))
|
||||
i += 1
|
||||
return re.compile("".join(out) + r"\Z")
|
||||
|
||||
|
||||
def read_globs(path):
|
||||
"""The globs of a PATHS file: one a line, blank lines and `#` comments skipped."""
|
||||
with open(path, encoding="utf-8") as f:
|
||||
lines = [line.strip() for line in f]
|
||||
globs = [line for line in lines if line and not line.startswith("#")]
|
||||
for g in globs:
|
||||
if g.startswith("/") or g.startswith(":") or "\\" in g:
|
||||
raise SystemExit(f"{path}: `{g}`: a glob is relative to the repository root, with / and no pathspec magic")
|
||||
return globs
|
||||
|
||||
|
||||
def areas(root):
|
||||
"""Every area (a folder of tests/kernel-cases/ but the ladder) with its PATHS globs."""
|
||||
base = os.path.join(root, CASES)
|
||||
out = {}
|
||||
for name in sorted(os.listdir(base)):
|
||||
if name == LADDER or not os.path.isdir(os.path.join(base, name)):
|
||||
continue
|
||||
paths = os.path.join(base, name, "PATHS")
|
||||
if not os.path.isfile(paths):
|
||||
raise SystemExit(f"{CASES}/{name}/PATHS is missing: every area lists the source files whose change reruns it")
|
||||
out[name] = read_globs(paths)
|
||||
return out
|
||||
|
||||
|
||||
def matches(globs, path):
|
||||
return any(glob_regex(g).match(path) for g in globs)
|
||||
|
||||
|
||||
def select(root, paths, everything=False):
|
||||
"""Return (shards, build): the areas to run, in order and the ladder last, and whether the
|
||||
build job runs."""
|
||||
by_area = areas(root)
|
||||
if everything:
|
||||
return [*by_area, LADDER], True
|
||||
shared = read_globs(os.path.join(root, CASES, "PATHS-ALL"))
|
||||
hit, ladder = set(), False
|
||||
for path in paths:
|
||||
if matches(shared, path):
|
||||
return [*by_area, LADDER], True
|
||||
if path.startswith(f"{CASES}/{LADDER}/"):
|
||||
ladder = True
|
||||
for area, globs in by_area.items():
|
||||
if path.startswith(f"{CASES}/{area}/") or matches(globs, path):
|
||||
hit.add(area)
|
||||
shards = [a for a in by_area if a in hit]
|
||||
if shards or ladder:
|
||||
shards.append(LADDER)
|
||||
return shards, bool(shards) or any(matches(COVERAGE, p) for p in paths)
|
||||
|
||||
|
||||
def read_paths(src):
|
||||
data = sys.stdin.buffer.read() if src == "-" else open(src, "rb").read()
|
||||
return [p.decode() for p in data.split(b"\0") if p]
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
group = ap.add_mutually_exclusive_group(required=True)
|
||||
group.add_argument("--paths")
|
||||
group.add_argument("--all", action="store_true")
|
||||
ap.add_argument("--root", default=os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
ap.add_argument("--github-output")
|
||||
a = ap.parse_args()
|
||||
shards, build = select(a.root, [] if a.all else read_paths(a.paths), everything=a.all)
|
||||
lines = [f"shards={json.dumps(shards)}", f"build={'true' if build else 'false'}"]
|
||||
print("\n".join(lines))
|
||||
if a.github_output:
|
||||
with open(a.github_output, "a", encoding="utf-8") as f:
|
||||
f.write("\n".join(lines) + "\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,156 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Self-test for kernel_rig_plan.py. Run: python3 -I scripts/ci/test_kernel_rig_plan.py (from the
|
||||
repo root).
|
||||
|
||||
Uses a small synthetic tests/kernel-cases/, then the real one: every area has its PATHS and
|
||||
KNOWN_FAILING, every glob names files that exist, and the workflow's trigger paths cover them all.
|
||||
"""
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
import kernel_rig_plan as plan # noqa: E402
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
WORKFLOW = os.path.join(ROOT, ".github", "workflows", "kernel-rig.yml")
|
||||
|
||||
|
||||
def write(root, rel, text=""):
|
||||
path = os.path.join(root, rel)
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
f.write(text)
|
||||
|
||||
|
||||
class Glob(unittest.TestCase):
|
||||
def test_star_stays_in_a_folder_and_double_star_crosses(self):
|
||||
self.assertTrue(plan.glob_regex("crates/kernel/src/blend*.rs").match("crates/kernel/src/blend_smlib.rs"))
|
||||
self.assertFalse(plan.glob_regex("crates/kernel/src/*.rs").match("crates/kernel/src/tess/grid.rs"))
|
||||
self.assertTrue(plan.glob_regex("crates/kernel/src/tess/**").match("crates/kernel/src/tess/a/b.rs"))
|
||||
self.assertFalse(plan.glob_regex("crates/kernel/src/tess/**").match("crates/kernel/src/tess.rs"))
|
||||
self.assertTrue(plan.glob_regex("tests/**/recipe.json").match("tests/recipe.json"))
|
||||
self.assertTrue(plan.glob_regex("tests/**/recipe.json").match("tests/a/b/recipe.json"))
|
||||
self.assertFalse(plan.glob_regex("a.rs").match("a.rsx"))
|
||||
self.assertFalse(plan.glob_regex("a.rs").match("b/a.rs"))
|
||||
|
||||
|
||||
class Synthetic(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
r = self.root = self.tmp.name
|
||||
write(r, "tests/kernel-cases/PATHS-ALL", "# shared\ncrates/geom/**\n\n.github/workflows/kernel-rig.yml\n")
|
||||
write(r, "tests/kernel-cases/fillets/PATHS", "# blends\ncrates/kernel/src/blend*.rs\n")
|
||||
write(r, "tests/kernel-cases/shell/PATHS", "crates/kernel/src/offset*.rs\ncrates/kernel/src/blend_smlib.rs\n")
|
||||
write(r, "tests/kernel-cases/ladder/ladder.json", "{}")
|
||||
write(r, "tests/kernel-cases/README.md")
|
||||
|
||||
def tearDown(self):
|
||||
self.tmp.cleanup()
|
||||
|
||||
def select(self, *paths):
|
||||
return plan.select(self.root, list(paths))
|
||||
|
||||
def test_an_area_runs_on_its_paths_with_the_ladder(self):
|
||||
self.assertEqual(self.select("crates/kernel/src/blend.rs"), (["fillets", "ladder"], True))
|
||||
self.assertEqual(self.select("crates/kernel/src/blend_smlib.rs"), (["fillets", "shell", "ladder"], True))
|
||||
|
||||
def test_a_change_to_an_areas_cases_runs_it(self):
|
||||
self.assertEqual(self.select("tests/kernel-cases/shell/1-x/expected.json"), (["shell", "ladder"], True))
|
||||
self.assertEqual(self.select("tests/kernel-cases/shell/PATHS"), (["shell", "ladder"], True))
|
||||
self.assertEqual(self.select("tests/kernel-cases/ladder/golden.json"), (["ladder"], True))
|
||||
|
||||
def test_the_shared_core_runs_every_area(self):
|
||||
for p in ["crates/geom/src/brep.rs", ".github/workflows/kernel-rig.yml"]:
|
||||
self.assertEqual(self.select("docs/a.md", p), (["fillets", "shell", "ladder"], True), p)
|
||||
|
||||
def test_unrelated_changes_run_nothing(self):
|
||||
self.assertEqual(self.select("crates/ui-egui/src/app.rs", "docs/a.md", "tests/kernel-cases/README.md"), ([], False))
|
||||
|
||||
def test_a_registry_or_case_change_builds_for_the_coverage_check(self):
|
||||
self.assertEqual(self.select("crates/engine/src/cmd/view_navigation.rs"), ([], True))
|
||||
self.assertEqual(self.select("tests/kernel-cases/EXEMPT"), ([], True))
|
||||
|
||||
def test_everything_runs_every_area(self):
|
||||
self.assertEqual(plan.select(self.root, [], everything=True), (["fillets", "shell", "ladder"], True))
|
||||
|
||||
def test_a_new_area_joins_from_its_folder(self):
|
||||
write(self.root, "tests/kernel-cases/surfaces/PATHS", "crates/kernel/src/surface_*.rs\n")
|
||||
self.assertEqual(self.select("crates/kernel/src/surface_trim.rs"), (["surfaces", "ladder"], True))
|
||||
self.assertEqual(plan.select(self.root, [], everything=True)[0], ["fillets", "shell", "surfaces", "ladder"])
|
||||
|
||||
def test_an_area_without_paths_is_an_error(self):
|
||||
os.makedirs(os.path.join(self.root, "tests/kernel-cases/plastic"))
|
||||
with self.assertRaises(SystemExit) as e:
|
||||
self.select("crates/geom/src/brep.rs")
|
||||
self.assertIn("plastic/PATHS is missing", str(e.exception))
|
||||
|
||||
|
||||
def trigger_globs():
|
||||
"""The pull_request trigger paths of the workflow (the push trigger lists the same)."""
|
||||
with open(WORKFLOW, encoding="utf-8") as f:
|
||||
text = f.read()
|
||||
events = {}
|
||||
for event in ("pull_request", "push"):
|
||||
block = re.search(rf"^ {event}:\n((?: .*\n|\s*\n)+)", text, re.M)
|
||||
assert block, event
|
||||
paths = block.group(1).split("paths:", 1)[1]
|
||||
events[event] = re.findall(r'^\s+- "([^"]+)"', paths, re.M)
|
||||
return events
|
||||
|
||||
|
||||
def sample(glob):
|
||||
"""A path the glob matches: each wildcard filled in."""
|
||||
return glob.replace("**", "x/y").replace("*", "x").replace("?", "x")
|
||||
|
||||
|
||||
class Real(unittest.TestCase):
|
||||
def test_every_area_has_its_lists_and_every_glob_names_files(self):
|
||||
# Tracked files and new ones not yet committed (not the ignored ones).
|
||||
ls = ["git", "ls-files", "--cached", "--others", "--exclude-standard"]
|
||||
tracked = subprocess.run(ls, cwd=ROOT, check=True, capture_output=True, text=True).stdout.split("\n")
|
||||
shared = plan.read_globs(os.path.join(ROOT, plan.CASES, "PATHS-ALL"))
|
||||
areas = plan.areas(ROOT)
|
||||
self.assertTrue(areas)
|
||||
for area, globs in {"PATHS-ALL": shared, **areas}.items():
|
||||
if area != "PATHS-ALL":
|
||||
self.assertTrue(os.path.isfile(os.path.join(ROOT, plan.CASES, area, "KNOWN_FAILING")), area)
|
||||
for g in globs:
|
||||
with self.subTest(area=area, glob=g):
|
||||
rx = plan.glob_regex(g)
|
||||
self.assertTrue(any(rx.match(t) for t in tracked), f"{area}: {g} matches no file")
|
||||
|
||||
def test_an_area_names_only_its_own_files(self):
|
||||
# The shared core is in PATHS-ALL; an area's PATHS repeating it (or a whole crate's
|
||||
# folder) would only blur what the area is about.
|
||||
ls = ["git", "ls-files", "--cached", "--others", "--exclude-standard"]
|
||||
files = subprocess.run(ls, cwd=ROOT, check=True, capture_output=True, text=True).stdout.split("\n")
|
||||
shared = plan.read_globs(os.path.join(ROOT, plan.CASES, "PATHS-ALL"))
|
||||
core = {f for f in files if plan.matches(shared, f)}
|
||||
for area, globs in plan.areas(ROOT).items():
|
||||
for g in globs:
|
||||
rx = plan.glob_regex(g)
|
||||
overlap = sorted(f for f in core if rx.match(f))
|
||||
with self.subTest(area=area, glob=g):
|
||||
self.assertFalse(overlap, f"{area}/PATHS: {g} names shared-core files (PATHS-ALL): {overlap[:3]}")
|
||||
|
||||
def test_the_workflow_triggers_on_every_glob(self):
|
||||
events = trigger_globs()
|
||||
self.assertEqual(events["pull_request"], events["push"])
|
||||
triggers = [plan.glob_regex(g) for g in events["pull_request"]]
|
||||
globs = [*plan.read_globs(os.path.join(ROOT, plan.CASES, "PATHS-ALL")), *plan.COVERAGE]
|
||||
for area, own in plan.areas(ROOT).items():
|
||||
globs += [*own, f"{plan.CASES}/{area}/**"]
|
||||
for g in globs:
|
||||
with self.subTest(glob=g):
|
||||
self.assertTrue(any(t.match(sample(g)) for t in triggers), f"{g}: not under the workflow's trigger paths")
|
||||
|
||||
def test_a_ui_change_runs_nothing(self):
|
||||
self.assertEqual(plan.select(ROOT, ["crates/ui-egui/src/app.rs", "docs/kernel-cases.md", "README.md"]), ([], False))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user