diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a3268e1c..78a314d30 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -444,6 +444,9 @@ add_python_test(pr_carry_check_self ${CMAKE_SOURCE_DIR}/tools/ci/pr_carry_check. # branch that deletes a reviewed class passes it -- measured on #2021, where 41 classes became 40 and --check-gaps # still printed "none of the 41 reviewed significant classes is mapped". A test row for the three arms that do see it. add_python_test(registry_loss_check_test ${CMAKE_SOURCE_DIR}/tests/registry_loss_check_test.py) +# engine#2398: the stale-branch test, which `tests_referenced` refuses to leave unregistered -- a test no +# runner names is a test nobody runs. +add_python_test(branch_stale_check_test ${CMAKE_SOURCE_DIR}/tests/branch_stale_check_test.py) add_python_test(hyperloom_benchmark_test ${CMAKE_SOURCE_DIR}/tests/hyperloom_benchmark_test.py) # engine#1820: the other half of ci_targets_check -- a standalone shell test that nothing references cannot fail. diff --git a/tests/branch_stale_check_test.py b/tests/branch_stale_check_test.py new file mode 100644 index 000000000..e49d94e11 --- /dev/null +++ b/tests/branch_stale_check_test.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python3 +# Copyright 2026 bong-water-water-bong +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""branch_stale_check_test.py -- the stale-branch shape, built in throwaway repositories. + +Each case is the thing the check exists for: a file the base advances after the branch was cut, which the branch +never touched. The check must name it, must stay quiet when the branch does touch the file, and must refuse rather +than pass when a ref cannot be read. +""" + +import os +import shutil +import subprocess +import sys +import tempfile +import unittest + +CHECK = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "tools", "ci", + "branch_stale_check.py") + + +def git(cwd, *args): + return subprocess.run(("git",) + args, cwd=cwd, capture_output=True, text=True, + env={**os.environ, "GIT_AUTHOR_NAME": "t", "GIT_AUTHOR_EMAIL": "t@t", + "GIT_COMMITTER_NAME": "t", "GIT_COMMITTER_EMAIL": "t@t"}) + + +class BranchStaleCheckTest(unittest.TestCase): + def setUp(self): + self.repo = tempfile.mkdtemp(prefix="stale-") + git(self.repo, "init", "-q", "-b", "main") + self.write("shared.txt", "v1\n") + self.write("other.txt", "other v1\n") + commit(self.repo, "base") + git(self.repo, "checkout", "-q", "-b", "feature") + self.write("feature.txt", "the branch's own work\n") + commit(self.repo, "the branch does its own thing") + self.feature_before_base_moves = git(self.repo, "rev-parse", "HEAD").stdout.strip() + git(self.repo, "checkout", "-q", "main") + self.write("shared.txt", "v2 - merged while the branch was open\n") + commit(self.repo, "main advances a file the branch never touched") + self.main = git(self.repo, "rev-parse", "HEAD").stdout.strip() + + def tearDown(self): + shutil.rmtree(self.repo, ignore_errors=True) + + def write(self, rel, text): + with open(os.path.join(self.repo, rel), "w", encoding="utf-8") as handle: + handle.write(text) + + def run_check(self, base, head): + return subprocess.run((sys.executable, CHECK, "--base", base, "--head", head), cwd=self.repo, + capture_output=True, text=True) + + def test_a_file_the_base_advanced_is_named(self): + result = self.run_check(self.main, self.feature_before_base_moves) + self.assertEqual(result.returncode, 1, result.stdout + result.stderr) + self.assertIn("shared.txt - the branch carries an older copy and never changed it", result.stdout) + self.assertIn("rebase onto the base", result.stderr) + + def test_the_branchs_own_file_is_not_reported(self): + # `feature.txt` does not exist on the base at all: the branch adds it and that must never be a failure. + result = self.run_check(self.main, self.feature_before_base_moves) + self.assertNotIn("feature.txt", result.stdout) + + def test_a_branch_that_changed_the_file_itself_is_not_reported(self): + # The branch edits the same file, so a difference there is its intent, not staleness. + git(self.repo, "checkout", "-q", "feature") + self.write("shared.txt", "v2 - merged while the branch was open\n") + commit(self.repo, "the branch rebases its own copy") + head = git(self.repo, "rev-parse", "HEAD").stdout.strip() + result = self.run_check(self.main, head) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + self.assertNotIn("FAIL", result.stdout) + + def test_a_file_the_base_added_and_the_branch_predates_is_not_a_failure(self): + # The branch cannot have removed a file it never saw: a merge supplies it. This is the arm that must NOT + # fire, because a check that fails on the harmless case teaches readers to ignore it. + git(self.repo, "checkout", "-q", "main") + self.write("added-after-the-fork.txt", "new on the base\n") + commit(self.repo, "the base adds a file") + base = git(self.repo, "rev-parse", "HEAD").stdout.strip() + result = self.run_check(base, self.feature_before_base_moves) + self.assertIn("the base also added", result.stdout) + self.assertNotIn("added-after-the-fork.txt - the branch carries", result.stdout) + + def test_an_unreadable_ref_refuses_instead_of_passing(self): + result = self.run_check(self.main, "no-such-ref") + self.assertEqual(result.returncode, 2, result.stdout + result.stderr) + self.assertIn("cannot be read - cannot tell, and that is not a pass", result.stderr) + + +def commit(cwd, message): + git(cwd, "add", "-A") + assert git(cwd, "commit", "-q", "-m", message).returncode == 0 + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/ci/branch_stale_check.py b/tools/ci/branch_stale_check.py new file mode 100755 index 000000000..dd3b39111 --- /dev/null +++ b/tools/ci/branch_stale_check.py @@ -0,0 +1,139 @@ +#!/usr/bin/env python3 +# Copyright 2026 bong-water-water-bong +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""branch_stale_check.py -- a branch that is behind carries older copies of files it never touched. + +The fleet squash-merges, so the branch's tree becomes main's tree for every path in the diff. On a branch cut +before another PR landed, the endpoint diff lists that PR's files as *modified* -- the branch's copy is the older +one -- and the squash reverts them. Five PRs on 2026-11-13/14 had exactly that shape: #358 would have removed +llama.cpp#357's alignment refusal and its case, #361 would have removed #355, #2021 would have removed #2387's +ctest row, #2387 would have removed #2366's arms, #2397 would have removed #2021's single-offset guard. + +`git diff base...branch` (three dots) answers "what did the branch change", which is why it does not show this: the +files it misses are the ones the branch never changed. This check asks the other question directly. + + for every path the base has that the branch does NOT change: + is the branch's copy byte-identical to the base's? + +A difference there cannot be the branch's intent -- it did not touch the file -- so it is a stale copy, and merging +it would write the old content over the base's. Files the branch does change are excluded, so a legitimate +divergence is never reported. + +Exit 0 quiet, 1 stale files found, 2 a ref could not be read (which is not a pass). +""" + +import argparse +import subprocess +import sys + + +def git(*args, cwd=None): + return subprocess.run(("git",) + args, capture_output=True, text=True, cwd=cwd) + + +def ref_exists(rev): + return git("rev-parse", "--verify", "--quiet", f"{rev}^{{commit}}").returncode == 0 + + +def blob_sha(rev, path): + """The blob id at rev:path, or None when the path does not exist there.""" + result = git("rev-parse", "--verify", "--quiet", f"{rev}:{path}") + return result.stdout.strip() if result.returncode == 0 else None + + +def paths_at(rev): + listed = git("ls-tree", "-r", "--name-only", "-z", rev) + if listed.returncode != 0: + return None + return [p for p in listed.stdout.split("\0") if p] + + +def touched_by_branch(base, head, merge_base): + """Paths the branch itself changed, relative to where it diverged.""" + result = git("diff", "--name-only", "-z", f"{merge_base}...{head}") + if result.returncode != 0: + return None + return {p for p in result.stdout.split("\0") if p} + + +def main(): + parser = argparse.ArgumentParser(description="Report files where the branch carries an older copy.") + parser.add_argument("--base", default="origin/main", help="base ref (default: origin/main)") + parser.add_argument("--head", default="HEAD", help="head ref (default: HEAD)") + parser.add_argument("--quiet", action="store_true", help="only print failures") + parser.add_argument("--limit", type=int, default=25, help="how many files to name (default: 25)") + args = parser.parse_args() + + for label, rev in (("base", args.base), ("head", args.head)): + if not ref_exists(rev): + print(f"branch_stale_check: {label} ref '{rev}' cannot be read - cannot tell, and that is not a pass", + file=sys.stderr) + return 2 + + merge_base = git("merge-base", args.base, args.head) + if merge_base.returncode != 0: + print(f"branch_stale_check: no merge base for {args.base} and {args.head} ({merge_base.stderr.strip()})", + file=sys.stderr) + return 2 + merge_base = merge_base.stdout.strip() + + base_paths = paths_at(args.base) + head_paths = paths_at(args.head) + if base_paths is None or head_paths is None: + print("branch_stale_check: could not list one side - cannot tell, and that is not a pass", file=sys.stderr) + return 2 + + touched = touched_by_branch(args.base, args.head, merge_base) + if touched is None: + print("branch_stale_check: could not diff the branch against its merge base", file=sys.stderr) + return 2 + + stale = [] + added_by_base = [] + for path in base_paths: + base_blob = blob_sha(args.base, path) + head_blob = blob_sha(args.head, path) + if path in touched: + # The branch's own change. A file it deleted on purpose lands here too, which is why a divergence on + # a touched path is never reported. + continue + if head_blob is None: + # Present on the base, absent at the head, and the branch never touched it: the file arrived on the + # base after the fork, and a merge supplies it. Harmless, so it is counted and not failed -- an arm + # that can only fire on the harmless case is an arm that would teach readers to ignore the check. + added_by_base.append(path) + elif base_blob != head_blob: + stale.append(path) + + if not args.quiet: + print(f"branch_stale_check: base {args.base} {len(base_paths)} path(s), branch touches {len(touched)} of them; " + f"{len(stale)} older cop(ies) (the base also added {len(added_by_base)} path(s) the branch predates, " + f"which a merge supplies)") + + for path in stale[: args.limit]: + print(f"FAIL branch_stale_check: {path} - the branch carries an older copy and never changed it") + if len(stale) > args.limit: + print(f"FAIL branch_stale_check: … and {len(stale) - args.limit} more older cop(ies)") + if stale: + print("branch_stale_check: rebase onto the base - a squash merge of this branch would write these older " + "copies over it", file=sys.stderr) + return 1 + if not args.quiet: + print("branch_stale_check: no file the branch left alone differs from the base") + return 0 + + +if __name__ == "__main__": + sys.exit(main())