diff --git a/scripts/pr_check.py b/scripts/pr_check.py index cb0ef3ef9e..5ac9ea1c33 100644 --- a/scripts/pr_check.py +++ b/scripts/pr_check.py @@ -100,10 +100,25 @@ def section_has_content(body, section): return False -def check(body, head, files, is_main_ancestor, *, test_only=False): +def main_baseline(candidates, main_behind): + """The main block the PR's run is compared with, of those with its title and time: the one of + the newest main commit when `main_behind(sha)` (commits main is ahead of `sha`) is given, so + pasting an older block first does not choose it (#2226); else, or when the distance of any of + them is unknown (a failed API call must not make an older block win), the first.""" + if not candidates: + return None + if main_behind is None: + return candidates[0] + behind = [main_behind(b["commit"].split()[0]) for b in candidates] + if None in behind: + return candidates[0] + return candidates[behind.index(min(behind))] + + +def check(body, head, files, is_main_ancestor, *, test_only=False, main_behind=None): """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.""" + commit is on main; `main_behind(sha)`: how many commits main is ahead of it (None: unknown).""" body = body.replace("\r\n", "\n") problems = [] for section in SECTIONS: @@ -135,8 +150,14 @@ def check(body, head, files, is_main_ancestor, *, test_only=False): 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])] - if not main: + on_main = [b for b in base if is_main_ancestor(b["commit"].split()[0])] + # a run with local changes is not main, for the baseline as for the PR's run (#2226) + main = [b for b in on_main if "uncommitted" not in b.get("commit", "")] + if not main and on_main: + problems.append("the main regression run (`xcl-smoke` block of commit " + f"{on_main[0]['commit'].split()[0]}) has uncommitted changes: rerun scripts/smoke.ps1 " + "on a clean checkout of main") + elif not main: problems.append("**Regression run** needs an `xcl-smoke` block made on a commit of main (not the PR's), " "with the same title and time as the PR's") if clean: @@ -148,18 +169,22 @@ def check(body, head, files, is_main_ancestor, *, test_only=False): if pr.get("exit", "").startswith("killed"): problems.append("the PR's regression run did not end by itself (killed)") if main: - m = next((b for b in main if b.get("title") == pr.get("title") and b.get("seconds") == pr.get("seconds")), None) + m = main_baseline([b for b in main if b.get("title") == pr.get("title") and b.get("seconds") == pr.get("seconds")], main_behind) for b in [m] if m else main: problems += field_problems(b, "the main") if m is None: problems.append("the main and PR regression runs are not of the same title for the same time") else: + # an old baseline is named where the comparison fails, not only in the log + n = main_behind(m["commit"].split()[0]) if main_behind else None + old = (f"; the main block is {n} commit(s) behind main: rerun it before comparing" + if n else "") 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})") + problems.append(f"the PR's run shows fewer frames than main's ({pf} against {mf}){old}") pc, mc = number(pr.get("peak commit")), number(m.get("peak commit")) if pc and mc and pc > COMMIT_GROWTH * mc: - problems.append(f"the PR's run commits much more memory than main's ({pc} MB against {mc} MB)") + problems.append(f"the PR's run commits much more memory than main's ({pc} MB against {mc} MB){old}") return problems @@ -193,7 +218,21 @@ def is_main_ancestor(sha): 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) + def main_behind(sha): + if o.local: + r = subprocess.run(["git", "rev-list", "--count", f"{sha}..origin/main"], capture_output=True, text=True) + else: + r = subprocess.run(["gh", "api", f"repos/{o.repo}/compare/{sha}...main", "--jq", ".ahead_by"], capture_output=True, text=True) + return int(r.stdout.strip()) if r.returncode == 0 and r.stdout.strip().isdigit() else None + + problems = check(body, o.head, files, is_main_ancestor, test_only=o.test_only, main_behind=main_behind) + # how old the baseline is: shown, not judged (the limit is the maintainer's call, #2226) + for b in smoke_blocks(body.replace("\r\n", "\n")): + sha = (b.get("commit") or "").split()[0] if b.get("commit") else "" + if len(sha) >= 7 and not o.head.startswith(sha) and is_main_ancestor(sha): + n = main_behind(sha) + if n is not None: + print(f"main regression run {sha[:12]}: {n} commit(s) behind main") 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..e809f45c6c 100644 --- a/scripts/test_pr_check.py +++ b/scripts/test_pr_check.py @@ -96,6 +96,36 @@ def test_blocks_with_only_commit_lines_fail(self): for run in ("the PR's", "the main"): self.assertTrue(any(p.startswith(run) and f"no `{field}` line" in p for p in problems), problems) + RUN = "title: G\nseconds: 60\ndevice removals: 0\ncrashes: 0\npeak commit: {} MB\nframes shown: {}\n" + + def test_a_main_run_with_uncommitted_changes_is_not_a_baseline(self): + body = ("```\nxcl-smoke v1\ncommit: 1111111aaaa (uncommitted changes)\n" + self.RUN.format(100, 100) + "```\n" + "```\nxcl-smoke v1\ncommit: 1234567890\n" + self.RUN.format(100, 100) + "```\n") + problems = check(BODY + body, HEAD, [changed("crates/xcl/src/hle/d3d12x/vram.rs")], lambda sha: sha == "1111111aaaa") + self.assertEqual(["the main regression run (`xcl-smoke` block of commit 1111111aaaa) has uncommitted " + "changes: rerun scripts/smoke.ps1 on a clean checkout of main"], problems) + + def test_the_newest_main_block_is_the_baseline_whatever_its_place(self): + # an old main run with a low peak pasted first, the current main run after it + old = "```\nxcl-smoke v1\ncommit: 1111111aaaa\n" + self.RUN.format(1000, 100) + "```\n" + new = "```\nxcl-smoke v1\ncommit: 2222222bbbb\n" + self.RUN.format(4000, 100) + "```\n" + pr = "```\nxcl-smoke v1\ncommit: 1234567890\n" + self.RUN.format(4000, 100) + "```\n" + files = [changed("crates/xcl/src/hle/d3d12x/vram.rs")] + on_main = lambda sha: sha in ("1111111aaaa", "2222222bbbb") + behind = {"1111111aaaa": 40, "2222222bbbb": 2}.get + for order in (old + new, new + old): + self.assertEqual([], check(BODY + order + pr, HEAD, files, on_main, main_behind=behind)) + # without the distance the first block is taken, as before + self.assertIn("the PR's run commits much more memory than main's (4000 MB against 1000 MB)", + check(BODY + old + new + pr, HEAD, files, on_main)) + # an unknown distance (a failed API call) does not reorder: the first block, not the older one + flaky = {"1111111aaaa": 40, "2222222bbbb": None}.get + self.assertEqual([], check(BODY + new + old + pr, HEAD, files, on_main, main_behind=flaky)) + # a failing comparison with an old baseline says how old it is + self.assertIn("the PR's run commits much more memory than main's (4000 MB against 1000 MB); " + "the main block is 40 commit(s) behind main: rerun it before comparing", + check(BODY + old + pr, HEAD, files, on_main, main_behind=behind)) + def test_non_numeric_frames_shown_fails(self): run = "title: G\nseconds: 60\ndevice removals: 0\ncrashes: 0\n" problems = self.smoke_problems(run + "frames shown: 100\n", run + "frames shown: many\n")