From 5f04fdab38ca0913fe4e879c6d3a8bfb3e7ff9f6 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Mon, 19 Oct 2026 08:03:40 +0000 Subject: [PATCH 1/3] test: the pre-commit hook registers only the commit's own tests Red: committing tests/test_mine.py also registers the base's unregistered tests/test_base.py, so every pull request that commits a test carries the same registry entries and they conflict (#21946); register_derivable has no way to name the tests. Co-Authored-By: Claude Opus 5.5 --- tests/test_ci_test_execution_registry.py | 14 +++++++++++ tests/test_install_git_hooks.py | 30 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/tests/test_ci_test_execution_registry.py b/tests/test_ci_test_execution_registry.py index f4ed574b6c93..12633a85b60b 100644 --- a/tests/test_ci_test_execution_registry.py +++ b/tests/test_ci_test_execution_registry.py @@ -288,6 +288,20 @@ def test_a_test_a_workflow_runs_through_a_workload_profile_is_live(self) -> None errors, _, _ = validator.validate(root, added=set()) self.assertEqual(errors, []) + def test_write_only_registers_the_named_tests(self) -> None: + """The pre-commit hook passes only the commit's own tests (#21946): registering every + unregistered test the base carries wrote the same entries into every pull request.""" + root = self.make_root(tests=["test_kept.py", "test_base.py", "test_mine.py"], registry=self.kept_registry()) + workflow = root / ".github" / "workflows" / "ci-guards.yml" + workflow.write_text( + GUARD_WORKFLOW + " - run: python3 tests/test_base.py\n - run: python3 tests/test_mine.py\n", + encoding="utf-8", + ) + + self.assertEqual(validator.register_derivable(root, only={"tests/test_mine.py"}), ["tests/test_mine.py"]) + self.assertEqual(validator.register_derivable(root, only=set()), []) + self.assertEqual(validator.register_derivable(root), ["tests/test_base.py"]) # unrestricted, as before + def test_write_leaves_a_test_no_workflow_runs_for_a_person_to_place(self) -> None: root = self.make_root(tests=["test_kept.py", "test_orphan.py"], registry=self.kept_registry()) before = (root / "tests" / "test-execution.toml").read_text(encoding="utf-8") diff --git a/tests/test_install_git_hooks.py b/tests/test_install_git_hooks.py index e5f460c533a3..fd5f7014e43d 100644 --- a/tests/test_install_git_hooks.py +++ b/tests/test_install_git_hooks.py @@ -251,6 +251,36 @@ def run_pre_commit(self, env): hook = Path(self.local_hooks_path()) / "pre-commit" return subprocess.run([str(hook)], cwd=self.repo, env=env, text=True, capture_output=True) + def test_pre_commit_registers_only_the_commits_own_tests(self): + """The base carried an unregistered test; the hook wrote it into whichever pull request + committed a test next, so unrelated pull requests conflicted on the registry (#21946).""" + self.assertEqual(self.install().returncode, 0) + workflow = self.repo / ".github/workflows/ci-guards.yml" + workflow.parent.mkdir(parents=True, exist_ok=True) + workflow.write_text( + "name: CI guards\njobs:\n workflow-guard-tests:\n steps:\n" + " - run: python3 tests/test_kept.py\n" + " - run: python3 tests/test_base.py\n - run: python3 tests/test_mine.py\n", + encoding="utf-8", + ) + registry = self.repo / "tests/test-execution.toml" + registry.parent.mkdir(parents=True, exist_ok=True) + registry.write_text('version = 1\n\n[[test]]\npath = "tests/test_kept.py"\nlane = "linux-guard"\n', + encoding="utf-8") + for name in ("test_kept.py", "test_base.py"): + (self.repo / "tests" / name).write_text("#!/usr/bin/env python3\n", encoding="utf-8") + self.git("add", "-A") + self.git("-c", "user.name=t", "-c", "user.email=t@t", "commit", "--quiet", "--no-verify", "-m", "base") + + (self.repo / "tests/test_mine.py").write_text("#!/usr/bin/env python3\n", encoding="utf-8") + self.git("add", "tests/test_mine.py") + result = subprocess.run([str(Path(self.local_hooks_path()) / "pre-commit")], cwd=self.repo, + env=self.env, text=True, capture_output=True) + self.assertEqual(result.returncode, 0, result.stderr) + text = registry.read_text(encoding="utf-8") + self.assertIn('path = "tests/test_mine.py"', text) + self.assertNotIn("tests/test_base.py", text) + def test_pre_commit_formats_rust_outside_cmux_tui_with_its_edition(self): self.assertEqual(self.install().returncode, 0) source = self.stage_rust_package("Native/OptChat/probe", "2024") From ab47600c3557029e19be5cd3c5021c8126ee0267 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Mon, 19 Oct 2026 08:04:02 +0000 Subject: [PATCH 2/3] pre-commit registers only the commit's own tests (#21946) The hook ran the registry writer over every unregistered test, so whichever pull request committed a test next also carried the base's stragglers, and those identical entries conflicted across pull requests. register_derivable takes --only; the hook passes the tests the commit adds and the ones its workflow changes name. The base's two stragglers are registered here once. Co-Authored-By: Claude Opus 5.5 --- scripts/ci/validate_test_execution_registry.py | 13 +++++++++---- scripts/git-hooks/pre-commit | 16 +++++++++++++++- tests/test-execution.toml | 8 ++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/scripts/ci/validate_test_execution_registry.py b/scripts/ci/validate_test_execution_registry.py index 9d24e9f7e5ee..56857bad241e 100644 --- a/scripts/ci/validate_test_execution_registry.py +++ b/scripts/ci/validate_test_execution_registry.py @@ -245,15 +245,18 @@ def comparison_point(base_sha: str, root: Path = ROOT) -> str: return merge_base(base_sha, root) or base_sha -def register_derivable(root: Path = ROOT) -> list[str]: - """Append an entry for each unregistered test a workflow already runs; return their paths.""" +def register_derivable(root: Path = ROOT, only: set[str] | None = None) -> list[str]: + """Append an entry for each unregistered test a workflow already runs; return their paths. + With `only`, consider just those repository-relative paths: the pre-commit hook passes the + commit's own tests, so it never writes the base's unregistered ones into a pull request.""" manifest = root / "tests" / "test-execution.toml" workflows = root / ".github" / "workflows" registered = {entry.get("path") for entry in load_registry(manifest)} discovered = sorted( path.relative_to(root).as_posix() for path in (root / "tests").glob("test_*.py") if path.is_file() ) - added = [path for path in discovered if path not in registered and workflow_running(path, workflows)] + added = [path for path in discovered + if path not in registered and (only is None or path in only) and workflow_running(path, workflows)] if added: text = manifest.read_text(encoding="utf-8") blocks = "".join(f'\n[[test]]\npath = "{path}"\nlane = "{DIRECT_RUN_LANE}"\n' for path in added) @@ -474,12 +477,14 @@ def main(argv: list[str]) -> int: ) parser.add_argument("--write", action="store_true", help="register unregistered tests a workflow already runs, then validate") + parser.add_argument("--only", action="append", default=None, metavar="PATH", + help="with --write, register only these tests (repeatable)") args = parser.parse_args(argv) root = args.repo_root.resolve() if args.write: try: - for path in register_derivable(root): + for path in register_derivable(root, set(args.only) if args.only is not None else None): print(f"registered {path} on lane {DIRECT_RUN_LANE}") except (OSError, ValueError) as error: print(error, file=sys.stderr) diff --git a/scripts/git-hooks/pre-commit b/scripts/git-hooks/pre-commit index b2e6529972a4..52b5fe0c799d 100755 --- a/scripts/git-hooks/pre-commit +++ b/scripts/git-hooks/pre-commit @@ -26,10 +26,24 @@ cd "$REPO_ROOT" # Register new Python tests that a workflow already runs, so adding a test and # its ci-guards.yml step is all a commit needs. Best effort: CI still validates. +# Only this commit's tests: the ones it adds, and the ones its workflow changes +# name. Registering every unregistered test the base carries wrote the same +# entries into every pull request, which then conflicted (#21946). REGISTRY="tests/test-execution.toml" if git diff --cached --name-only --diff-filter=ACMRT | grep -Eq '^(tests/test_[^/]+\.py|\.github/workflows/.+)$' \ && git diff --quiet -- "$REGISTRY"; then - "$PYTHON3_BIN" -I "$REGISTRY_VALIDATOR" --repo-root "$REPO_ROOT" --write >/dev/null 2>&1 || true + OWN_TESTS=() + while IFS= read -r test_path; do + [[ -n "$test_path" ]] && OWN_TESTS+=(--only "$test_path") + done < <( + { + git diff --cached --name-only --diff-filter=A -- 'tests/test_*.py' + git diff --cached -U0 -- .github/workflows | grep -E '^\+' | grep -oE 'tests/test_[A-Za-z0-9_]+\.py' || true + } | sort -u + ) + if (( ${#OWN_TESTS[@]} > 0 )); then + "$PYTHON3_BIN" -I "$REGISTRY_VALIDATOR" --repo-root "$REPO_ROOT" --write "${OWN_TESTS[@]}" >/dev/null 2>&1 || true + fi if ! git diff --quiet -- "$REGISTRY"; then git add "$REGISTRY" echo "pre-commit: registered new tests in $REGISTRY" diff --git a/tests/test-execution.toml b/tests/test-execution.toml index 9b01a378319b..23ebe4b0e1f3 100644 --- a/tests/test-execution.toml +++ b/tests/test-execution.toml @@ -1403,3 +1403,11 @@ lane = "linux-guard" [[test]] path = "tests/test_idea_ledger.py" lane = "linux-guard" + +[[test]] +path = "tests/test_cmux_next_base_autofix_finishes.py" +lane = "linux-guard" + +[[test]] +path = "tests/test_nightly_next_feed_rollback.py" +lane = "linux-guard" From 543798319b7d9d939e62b029f5023e950234517a Mon Sep 17 00:00:00 2001 From: Leo Li Date: Mon, 19 Oct 2026 21:46:41 +0000 Subject: [PATCH 3/3] test(hooks): cover registering a test named only by a workflow change Greptile on #22056: the hook test added the test file, so the path that collects tests from added workflow lines went untested. Fails on the old hook (it also registers the base's unregistered test). Co-Authored-By: Claude Opus 5.5 --- tests/test_install_git_hooks.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/test_install_git_hooks.py b/tests/test_install_git_hooks.py index fd5f7014e43d..7999a6e08b60 100644 --- a/tests/test_install_git_hooks.py +++ b/tests/test_install_git_hooks.py @@ -281,6 +281,33 @@ def test_pre_commit_registers_only_the_commits_own_tests(self): self.assertIn('path = "tests/test_mine.py"', text) self.assertNotIn("tests/test_base.py", text) + def test_pre_commit_registers_a_test_its_workflow_change_names(self): + """A commit that only adds a workflow line for an existing test registers that test, and + still not the base's other unregistered one (#21946).""" + self.assertEqual(self.install().returncode, 0) + workflow = self.repo / ".github/workflows/ci-guards.yml" + workflow.parent.mkdir(parents=True, exist_ok=True) + base_workflow = ("name: CI guards\njobs:\n workflow-guard-tests:\n steps:\n" + " - run: python3 tests/test_kept.py\n - run: python3 tests/test_base.py\n") + workflow.write_text(base_workflow, encoding="utf-8") + registry = self.repo / "tests/test-execution.toml" + registry.parent.mkdir(parents=True, exist_ok=True) + registry.write_text('version = 1\n\n[[test]]\npath = "tests/test_kept.py"\nlane = "linux-guard"\n', + encoding="utf-8") + for name in ("test_kept.py", "test_base.py", "test_mine.py"): + (self.repo / "tests" / name).write_text("#!/usr/bin/env python3\n", encoding="utf-8") + self.git("add", "-A") + self.git("-c", "user.name=t", "-c", "user.email=t@t", "commit", "--quiet", "--no-verify", "-m", "base") + + workflow.write_text(base_workflow + " - run: python3 tests/test_mine.py\n", encoding="utf-8") + self.git("add", ".github/workflows/ci-guards.yml") + result = subprocess.run([str(Path(self.local_hooks_path()) / "pre-commit")], cwd=self.repo, + env=self.env, text=True, capture_output=True) + self.assertEqual(result.returncode, 0, result.stderr) + text = registry.read_text(encoding="utf-8") + self.assertIn('path = "tests/test_mine.py"', text) + self.assertNotIn("tests/test_base.py", text) + def test_pre_commit_formats_rust_outside_cmux_tui_with_its_edition(self): self.assertEqual(self.install().returncode, 0) source = self.stage_rust_package("Native/OptChat/probe", "2024")