diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index 55781ea2cc..dcef4f7480 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -44,6 +44,7 @@ jobs: GH_TOKEN: ${{ github.token }} BODY: ${{ github.event.pull_request.body }} HEAD: ${{ github.event.pull_request.head.sha }} + BASE: ${{ github.event.pull_request.base.sha }} BASE_REF: ${{ github.event.pull_request.base.ref }} NUMBER: ${{ github.event.pull_request.number }} REPO: ${{ github.repository }} @@ -65,4 +66,4 @@ jobs: approval_args=(--test-only) fi fi - python3 scripts/pr_check.py --body body.md --head "$HEAD" --files files.tsv --repo "$REPO" "${approval_args[@]}" + python3 scripts/pr_check.py --body body.md --head "$HEAD" --base "$BASE" --files files.tsv --repo "$REPO" "${approval_args[@]}" diff --git a/scripts/pr_check.py b/scripts/pr_check.py index cb0ef3ef9e..b5cbb2505d 100644 --- a/scripts/pr_check.py +++ b/scripts/pr_check.py @@ -2,10 +2,10 @@ merged"), checked from its description and its files – run by .github/workflows/pr-check.yml (`pull_request_target`: this file as it is on main, never the PR's copy) and locally: - python scripts/pr_check.py --body body.md --head --files files.tsv [--local] [--test-only] + python scripts/pr_check.py --body body.md --head --base --files files.tsv [--local] [--test-only] files.tsv: one line per file of the PR – path, status, binary (true/false), blob sha. --local -asks git instead of the GitHub API (whether a commit is on main, a blob's size). +asks git instead of the GitHub API for a blob's size. --test-only skips only the smoke check. GitHub Actions passes it after a test-only label approval. """ import argparse, os, re, subprocess, sys @@ -33,10 +33,11 @@ MAX_BINARY = 1 << 20 FRAMES_KEPT = 0.8 # the PR's frames at least this share of main's COMMIT_GROWTH = 1.25 # the PR's peak commit at most this times main's -# the lines a smoke block needs to be compared with the other (the crash and device-removal -# counts are checked where they are used), and those of them that are numbers +# the lines both smoke blocks need for comparison, the additional safety lines the PR's run needs, +# and the comparison fields whose values begin with numbers SMOKE_FIELDS = ("title", "seconds", "frames shown") SMOKE_NUMBERS = ("seconds", "frames shown") +PR_SMOKE_FIELDS = SMOKE_FIELDS + ("device removals", "crashes", "exit", "peak commit") def smoke_blocks(body): @@ -58,11 +59,11 @@ def number(s): return int(m.group(1)) if m else None -def field_problems(block, run): +def field_problems(block, run, fields=SMOKE_FIELDS): """What `block` (the smoke block of `run`) lacks for the comparison: a missing line or a number that is not one.""" name = f"{run} regression run (`xcl-smoke` block of commit {(block.get('commit') or '?').split()[0]})" - out = [f"{name} has no `{k}` line" for k in SMOKE_FIELDS if not block.get(k)] + out = [f"{name} has no `{k}` line" for k in fields if not block.get(k)] out += [f"{name} has a non-numeric `{k}`: {block[k]}" for k in SMOKE_NUMBERS if block.get(k) and number(block[k]) is None] return out @@ -100,10 +101,10 @@ def section_has_content(body, section): return False -def check(body, head, files, is_main_ancestor, *, test_only=False): +def check(body, head, base, files, *, test_only=False): """The problems (empty: mergeable as far as this check goes). `files`: (path, status, binary, - size) with `size` a function giving the file's bytes; `is_main_ancestor(sha)`: whether the - commit is on main.""" + size) with `size` a function giving the file's bytes. `head` and `base` are the pull request's + exact head and base commit IDs.""" body = body.replace("\r\n", "\n") problems = [] for section in SECTIONS: @@ -131,29 +132,46 @@ def check(body, head, files, is_main_ancestor, *, test_only=False): blocks = smoke_blocks(body) mine = [b for b in blocks if len(b.get("commit", "")) >= 7 and head.startswith(b["commit"].split()[0])] clean = [b for b in mine if "uncommitted" not in b.get("commit", "")] - base = [b for b in blocks if b not in mine and len(b.get("commit", "").split()[0] if b.get("commit") else "") >= 7] + baseline = [b for b in blocks if b not in mine and len(b.get("commit", "").split()[0] if b.get("commit") else "") >= 7] if not clean: problems.append(f"the PR changes the runtime: **Regression run** needs an `xcl-smoke` block made on this PR's " f"head commit {head[:12]} with no uncommitted changes (rerun scripts/smoke.ps1 after every push)") - main = [b for b in base if is_main_ancestor(b["commit"].split()[0])] + on_base = [b for b in baseline if base.startswith(b["commit"].split()[0])] + dirty_base = [b for b in on_base if "uncommitted" in b.get("commit", "")] + main = [b for b in on_base if b not in dirty_base] + if dirty_base: + problems.append("the main regression run was made with uncommitted changes; rerun scripts/smoke.ps1 on a clean checkout") if not main: - problems.append("**Regression run** needs an `xcl-smoke` block made on a commit of main (not the PR's), " + problems.append(f"**Regression run** needs an `xcl-smoke` block made on the PR's base commit {base[:12]} " "with the same title and time as the PR's") if clean: pr = clean[-1] - problems += field_problems(pr, "the PR's") + problems += field_problems(pr, "the PR's", PR_SMOKE_FIELDS) for key in ("device removals", "crashes"): - if (number(pr.get(key)) or 0) > 0: + value = pr.get(key) + if value and not re.fullmatch(r"\d+", value): + problems.append(f"the PR's regression run has a non-numeric `{key}`: {value}") + elif value and int(value) > 0: problems.append(f"the PR's regression run shows {key}: {pr.get(key)}") - if pr.get("exit", "").startswith("killed"): - problems.append("the PR's regression run did not end by itself (killed)") + exit_code = pr.get("exit") + if exit_code and exit_code != "0x0": + if exit_code.startswith("killed"): + problems.append("the PR's regression run did not end by itself (killed)") + else: + problems.append(f"the PR's regression run exited with {exit_code}, not 0x0") + peak_commit = pr.get("peak commit") + if peak_commit and not re.fullmatch(r"\d+\s+MB", peak_commit): + problems.append(f"the PR's regression run has a non-numeric `peak commit`: {peak_commit}") if main: - m = next((b for b in main if b.get("title") == pr.get("title") and b.get("seconds") == pr.get("seconds")), None) - for b in [m] if m else main: + matches = [b for b in main if b.get("title") == pr.get("title") and b.get("seconds") == pr.get("seconds")] + for b in matches if matches else main: problems += field_problems(b, "the main") - if m is None: + if not matches: problems.append("the main and PR regression runs are not of the same title for the same time") + elif len(matches) > 1: + problems.append("**Regression run** has several main blocks for the same title and time; keep exactly one baseline") else: + m = matches[0] pf, mf = number(pr.get("frames shown")), number(m.get("frames shown")) if mf and (pf is None or pf < FRAMES_KEPT * mf): problems.append(f"the PR's run shows fewer frames than main's ({pf} against {mf})") @@ -167,6 +185,7 @@ def main(): a = argparse.ArgumentParser() a.add_argument("--body", required=True) a.add_argument("--head", required=True) + a.add_argument("--base", required=True) a.add_argument("--files", required=True) a.add_argument("--repo", default=os.environ.get("REPO", "")) a.add_argument("--local", action="store_true") @@ -187,13 +206,7 @@ def blob_size(sha): sha = parts[3] files.append((parts[0], parts[1], parts[2] == "true", lambda sha=sha: blob_size(sha))) - def is_main_ancestor(sha): - if o.local: - return subprocess.run(["git", "merge-base", "--is-ancestor", sha, "origin/main"], capture_output=True).returncode == 0 - r = subprocess.run(["gh", "api", f"repos/{o.repo}/compare/{sha}...main", "--jq", ".status"], capture_output=True, text=True) - return r.returncode == 0 and r.stdout.strip() in ("ahead", "identical") - - problems = check(body, o.head, files, is_main_ancestor, test_only=o.test_only) + problems = check(body, o.head, o.base, files, test_only=o.test_only) if problems: print("This PR cannot be merged yet (see CONTRIBUTING.md, 'What a PR needs to be merged'):") for p in problems: diff --git a/scripts/test_pr_check.py b/scripts/test_pr_check.py index 8cc45d5d06..6dd51879b0 100644 --- a/scripts/test_pr_check.py +++ b/scripts/test_pr_check.py @@ -5,6 +5,7 @@ HEAD = "1234567890abcdef" +BASE = "1111111aaaabcdef" BODY = "**Tested**\nSynthetic fixtures\n**Not tested**\nRetail titles\n**AI use**\nNone\n" @@ -15,10 +16,10 @@ def changed(path, binary=False): class TestOnlyApprovalTests(unittest.TestCase): def test_runtime_path_requires_smoke_until_approved(self): files = [changed("crates/xcl/src/hle/d3d12x/vram.rs")] - problems = check(BODY, HEAD, files, lambda _: False) + problems = check(BODY, HEAD, BASE, files) self.assertEqual(2, len(problems)) self.assertTrue(all("Regression run" in problem for problem in problems)) - self.assertEqual([], check(BODY, HEAD, files, lambda _: False, test_only=True)) + self.assertEqual([], check(BODY, HEAD, BASE, files, test_only=True)) def test_approval_keeps_description_and_asset_checks(self): files = [ @@ -27,7 +28,7 @@ def test_approval_keeps_description_and_asset_checks(self): changed("notes/attachment.txt", binary=True), ] body = "**Tested**\nSynthetic fixtures\n**AI use**\nQwen\n" - problems = check(body, HEAD, files, lambda _: False, test_only=True) + problems = check(body, HEAD, BASE, files, test_only=True) self.assertEqual(4, len(problems)) for reason in ("Not tested", "AI use", "content.rpf", "attachment.txt"): self.assertTrue(any(reason in problem for problem in problems), problems) @@ -36,23 +37,23 @@ def test_runtime_build_inputs_require_smoke_until_approved(self): for path in ("crates/xcl/data/stubdb.toml", "crates/xcl/build.rs", "crates/xcl/Cargo.toml", "Cargo.toml", "Cargo.lock"): with self.subTest(path=path): - problems = check(BODY, HEAD, [changed(path)], lambda _: False) + problems = check(BODY, HEAD, BASE, [changed(path)]) self.assertEqual(2, len(problems)) self.assertTrue(all("Regression run" in problem for problem in problems)) - self.assertEqual([], check(BODY, HEAD, [changed(path)], lambda _: False, test_only=True)) + self.assertEqual([], check(BODY, HEAD, BASE, [changed(path)], test_only=True)) for path in ("tests/testtitle/Cargo.toml", "tests/testgamedll/build.rs", "crates/xcl/tests/data/x.toml"): with self.subTest(path=path): - self.assertEqual([], check(BODY, HEAD, [changed(path)], lambda _: False)) + self.assertEqual([], check(BODY, HEAD, BASE, [changed(path)])) def test_test_paths_do_not_need_approval(self): files = [changed("crates/xcl/tests/integration.rs"), changed("crates/xcl/src/gpu/recomp/tests.rs")] - self.assertEqual([], check(BODY, HEAD, files, lambda _: False)) + self.assertEqual([], check(BODY, HEAD, BASE, files)) class BannedModelTests(unittest.TestCase): def ai_use(self, line): body = BODY.replace("**AI use**\nNone\n", "**AI use**\n" + line + "\n") - return check(body, HEAD, [changed("README.md")], lambda _: True) + return check(body, HEAD, BASE, [changed("README.md")]) def test_family_names_of_small_and_open_weight_models_are_refused(self): for model in ("CodeLlama 34B", "TinyLlama", "Codestral 22B", "Devstral Small", "QwQ-32B", @@ -70,25 +71,27 @@ def test_frontier_models_and_ordinary_words_pass(self): class SmokeBlockTests(unittest.TestCase): + RUN = ("title: G\nseconds: 60\nexit: 0x0\nframes shown: 100\n" + "device removals: 0\ncrashes: 0\npeak commit: 1000 MB\n") + def test_two_blocks_in_one_fence_are_two_blocks(self): - run = "title: G\nseconds: 60\nframes shown: 100\ndevice removals: 0\ncrashes: 0\n" - body = ("```\nxcl-smoke v1\ncommit: 1111111aaaa\n" + run + "\n" - "xcl-smoke v1\ncommit: 1234567890\n" + run + "```\n") + body = ("```\nxcl-smoke v1\ncommit: 1111111aaaa\n" + self.RUN + "\n" + "xcl-smoke v1\ncommit: 1234567890\n" + self.RUN + "```\n") blocks = smoke_blocks(body) self.assertEqual(["1111111aaaa", "1234567890"], [b["commit"] for b in blocks]) files = [changed("crates/xcl/src/hle/d3d12x/vram.rs")] - self.assertEqual([], check(BODY + body, HEAD, files, lambda sha: sha == "1111111aaaa")) + self.assertEqual([], check(BODY + body, HEAD, BASE, files)) def test_blocks_in_separate_fences_stay_separate(self): body = ("```\nxcl-smoke v1\ncommit: 1111111aaaa\ntitle: G\n```\ntext\n" "```\nxcl-smoke v1\ncommit: 1234567890\ntitle: G\n```\n") self.assertEqual(["1111111aaaa", "1234567890"], [b["commit"] for b in smoke_blocks(body)]) - def smoke_problems(self, main_run, pr_run): - body = ("```\nxcl-smoke v1\ncommit: 1111111aaaa\n" + main_run + "```\n" + def smoke_problems(self, main_run, pr_run, main_commit="1111111aaaa"): + body = ("```\nxcl-smoke v1\ncommit: " + main_commit + "\n" + main_run + "```\n" "```\nxcl-smoke v1\ncommit: 1234567890\n" + pr_run + "```\n") files = [changed("crates/xcl/src/hle/d3d12x/vram.rs")] - return check(BODY + body, HEAD, files, lambda sha: sha == "1111111aaaa") + return check(BODY + body, HEAD, BASE, files) def test_blocks_with_only_commit_lines_fail(self): problems = self.smoke_problems("", "") @@ -97,17 +100,58 @@ def test_blocks_with_only_commit_lines_fail(self): self.assertTrue(any(p.startswith(run) and f"no `{field}` line" in p for p in problems), problems) def test_non_numeric_frames_shown_fails(self): - run = "title: G\nseconds: 60\ndevice removals: 0\ncrashes: 0\n" + run = self.RUN.replace("frames shown: 100\n", "") problems = self.smoke_problems(run + "frames shown: 100\n", run + "frames shown: many\n") self.assertIn("the PR's regression run (`xcl-smoke` block of commit 1234567890) " "has a non-numeric `frames shown`: many", problems) + def test_pr_block_requires_safety_fields(self): + for field in ("device removals", "crashes", "exit", "peak commit"): + with self.subTest(field=field): + pr_run = "\n".join(line for line in self.RUN.splitlines() + if not line.startswith(field + ":")) + "\n" + problems = self.smoke_problems(self.RUN, pr_run) + self.assertTrue(any(p.startswith("the PR's") and f"no `{field}` line" in p + for p in problems), problems) + + def test_pr_block_rejects_invalid_safety_values(self): + cases = { + "device removals: none": "non-numeric `device removals`", + "crashes: none": "non-numeric `crashes`", + "exit: 0xc0000005": "exited with 0xc0000005, not 0x0", + "peak commit: n/a MB": "non-numeric `peak commit`", + } + for replacement, expected in cases.items(): + with self.subTest(replacement=replacement): + field = replacement.split(":", 1)[0] + pr_run = "\n".join(replacement if line.startswith(field + ":") else line + for line in self.RUN.splitlines()) + "\n" + problems = self.smoke_problems(self.RUN, pr_run) + self.assertTrue(any(expected in p for p in problems), problems) + + def test_main_block_must_be_the_exact_pr_base(self): + problems = self.smoke_problems(self.RUN, self.RUN, "0000000aaaa") + self.assertTrue(any(f"base commit {BASE[:12]}" in p for p in problems), problems) + + def test_main_block_must_come_from_a_clean_checkout(self): + problems = self.smoke_problems(self.RUN, self.RUN, "1111111aaaa (uncommitted changes)") + self.assertTrue(any("main regression run was made with uncommitted changes" in p + for p in problems), problems) + + def test_several_matching_main_blocks_are_ambiguous(self): + main = "xcl-smoke v1\ncommit: 1111111aaaa\n" + self.RUN + body = ("```\n" + main + main + "```\n" + "```\nxcl-smoke v1\ncommit: 1234567890\n" + self.RUN + "```\n") + files = [changed("crates/xcl/src/hle/d3d12x/vram.rs")] + problems = check(BODY + body, HEAD, BASE, files) + self.assertTrue(any("several main blocks" in p for p in problems), problems) + class TemplatePlaceholderTests(unittest.TestCase): def test_untouched_pr_template_fails_all_required_sections(self): template = Path(__file__).resolve().parent.parent / ".github" / "pull_request_template.md" body = template.read_text(encoding="utf-8") - problems = check(body, HEAD, [changed("README.md")], lambda _: True) + problems = check(body, HEAD, BASE, [changed("README.md")]) self.assertEqual( [ "the **Tested** section is missing or empty", @@ -128,11 +172,11 @@ def test_filled_template_with_comments_and_real_lines_passes(self): "**AI use**\n" "- none\n" ) - self.assertEqual([], check(body, HEAD, [changed("README.md")], lambda _: True)) + self.assertEqual([], check(body, HEAD, BASE, [changed("README.md")])) def test_bare_bullets_and_html_comments_only_fail(self): body = "**Tested**\n\n-\n**Not tested**\n*\n**AI use**\n - \n" - problems = check(body, HEAD, [changed("README.md")], lambda _: True) + problems = check(body, HEAD, BASE, [changed("README.md")]) self.assertEqual(3, len(problems))