diff --git a/tests/test_supervise.py b/tests/test_supervise.py new file mode 100644 index 000000000..c9757bb41 --- /dev/null +++ b/tests/test_supervise.py @@ -0,0 +1,46 @@ +from pathlib import Path +import subprocess +import tempfile +import unittest +from unittest import mock + +from tools import supervise + + +class TrialMergeTests(unittest.TestCase): + def test_the_largest_compatible_set_avoids_every_conflicting_pair(self): + conflicts = {frozenset(("a", "b")), frozenset(("b", "c"))} + self.assertEqual(supervise.largest_compatible(["a", "b", "c", "d"], conflicts), ["a", "c", "d"]) + self.assertEqual(supervise.largest_compatible(["b", "a"], {frozenset(("a", "b"))}), ["b"]) + + def test_plan_keeps_branches_that_merge_cleanly_together(self): + with tempfile.TemporaryDirectory() as temp, mock.patch.object(supervise, "ROOT", Path(temp)): + def run(*args): + subprocess.run(["git", "-c", "user.name=t", "-c", "user.email=t@t", *args], cwd=temp, check=True, + capture_output=True) + + def commit(branch, name, text, start="base"): + run("checkout", "-q", "-B", branch, start) + Path(temp, name).write_text(text) + run("add", name) + run("commit", "-q", "-m", f"{branch} {name}") + + run("init", "-q", "-b", "base") + Path(temp, "shared.txt").write_text("one\n") + run("add", "shared.txt") + run("commit", "-q", "-m", "start") + commit("parity/a", "a.txt", "a\n") + commit("parity/b", "shared.txt", "b\n") + commit("parity/c", "shared.txt", "c\n") # conflicts with parity/b only + commit("parity/d", "d.txt", "d\n") + commit("parity/e", "e.txt", "e\n", start="base") + run("checkout", "-q", "base") + commit("base", "shared.txt", "base\n") # parity/b and parity/c now conflict with the base too + chosen, head = supervise.plan("base", ["parity/a", "parity/b", "parity/c", "parity/d"], verbose=False) + self.assertEqual(chosen, ["parity/a", "parity/d"]) + files = subprocess.run(["git", "ls-tree", "--name-only", head], cwd=temp, capture_output=True, text=True).stdout + self.assertEqual(files.split(), ["a.txt", "d.txt", "shared.txt"]) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/supervise.py b/tools/supervise.py index 49b00d803..1f608e175 100644 --- a/tools/supervise.py +++ b/tools/supervise.py @@ -4,14 +4,23 @@ python3 tools/supervise.py status # every worktree: branch, progress, last gate python3 tools/supervise.py new dispensers # worktree ../dispensers on parity/dispensers, from the integration branch python3 tools/supervise.py save [AREA ...] # commit and push unfinished work (before stopping agents) + python3 tools/supervise.py check [BRANCH ...] # which branches merge cleanly, alone and together (seconds) python3 tools/supervise.py integrate parity/dispensers # merge into the integration branch if the gate passes - python3 tools/supervise.py integrate parity/a parity/b # several branches, one gate + python3 tools/supervise.py integrate parity/a parity/b # the largest set that merges cleanly, one gate + python3 tools/supervise.py integrate --ready # every parity/* branch with new commits The integration branch (`parity-base` by default) is checked out in its own worktree; `integrate` merges there, runs tools/gate.py and pushes the integration branch only when the gate passes, allowing only the harnesses that already fail on the integration branch to keep failing. Otherwise it undoes the merge -and leaves the branch for its owner to fix. Worktrees live next to the +and leaves the branch for its owner to fix. + +`check` and `integrate` first trial-merge the candidates in the object database +(git merge-tree; no worktree is touched): each alone, every pair, then the +largest set without conflicts, merged in turn. `integrate` merges that set and +gates it once (formatting, lints and tests come before the long harness run). +If the gate fails on several branches, it tries each half in turn, so the +branches that pass still land. Worktrees live next to the integration worktree and share its vanilla data: their `.local/` entries link to `shared-local/` in the same directory. """ @@ -21,11 +30,13 @@ import json from pathlib import Path import subprocess import sys +import time ROOT = Path(__file__).resolve().parents[1] -def git(*args, cwd=ROOT, check=True): +def git(*args, cwd=None, check=True): + cwd = cwd or ROOT result = subprocess.run(["git", *args], cwd=cwd, capture_output=True, text=True) if check and result.returncode: raise SystemExit(f"git {' '.join(args)} failed in {cwd}:\n{result.stderr.strip()}") @@ -112,7 +123,118 @@ def save(args): print(f"{path.name}: {branch} pushed at {git('rev-parse', '--short', 'HEAD', cwd=path)}") +def trial_merge(onto, branch): + """Merge branch onto the commit onto without touching any worktree: (merged commit or None, conflicting paths).""" + result = subprocess.run(["git", "merge-tree", "--write-tree", "--name-only", "--no-messages", onto, branch], + cwd=ROOT, capture_output=True, text=True) + lines = result.stdout.split() + if result.returncode == 0: + return git("commit-tree", lines[0], "-p", onto, "-p", branch, "-m", f"Trial merge of {branch}"), [] + if result.returncode == 1: + return None, lines[1:] + raise SystemExit(f"git merge-tree {onto} {branch} failed:\n{result.stderr.strip()}") + + +def largest_compatible(branches, conflicts): + """The largest subset of branches with no conflicting pair (earlier branches win ties).""" + best = [] + + def search(chosen, index): + nonlocal best + if len(chosen) + len(branches) - index <= len(best): + return + if index == len(branches): + best = list(chosen) + return + branch = branches[index] + if not any(frozenset((branch, other)) in conflicts for other in chosen): + search(chosen + [branch], index + 1) + search(chosen, index + 1) + + search([], 0) + return best + + +def plan(base, branches, verbose=True): + """Trial-merge the branches into base: the largest set that merges cleanly, in order, and its merged commit.""" + say = print if verbose else (lambda *_: None) + started = time.monotonic() + base_commit = git("rev-parse", base) + candidates, alone = [], {} + for branch in branches: + ahead = count(f"{base_commit}..{branch}", ROOT) + if not ahead: + say(f" {branch:<28} nothing new") + continue + merged, files = trial_merge(base_commit, branch) + alone[branch] = merged + if merged: + candidates.append(branch) + say(f" {branch:<28} {ahead:>3} commits, merges cleanly") + else: + say(f" {branch:<28} {ahead:>3} commits, CONFLICTS with {base}: {', '.join(files)}") + conflicts = {} + for index, first in enumerate(candidates): + for second in candidates[index + 1:]: + merged, files = trial_merge(alone[first], second) + if not merged: + conflicts[frozenset((first, second))] = files + say(f" {first} and {second} conflict: {', '.join(files)}") + chosen, head = [], base_commit + for branch in largest_compatible(candidates, conflicts): + merged, files = trial_merge(head, branch) + if merged: + chosen.append(branch) + head = merged + else: + say(f" {branch} conflicts with {', '.join(chosen)} together: {', '.join(files)}") + say(f"{len(chosen)} of {len(candidates) + sum(1 for b in alone if not alone[b])} branches merge cleanly together" + f"{': ' + ', '.join(chosen) if chosen else ''} (checked in {time.monotonic() - started:.1f}s)") + return chosen, head + + +def ready_branches(base): + """Every parity/* branch with commits the integration branch lacks.""" + names = git("for-each-ref", "--format=%(refname:short)", "refs/heads/parity/").splitlines() + return [name for name in names if name != base and count(f"{base}..{name}", ROOT)] + + +def check(args): + branches = args.branches or ready_branches(args.base) + print(f"Trial merges into {args.base} at {git('rev-parse', '--short', args.base)} (committed work only):") + plan(args.base, branches) + + def integrate(args): + if not args.branches and not args.ready: + raise SystemExit("Name the branches to integrate, or pass --ready for every parity/* branch with new commits.") + print(f"Trial merges into {args.base}:") + chosen, _ = plan(args.base, args.branches or ready_branches(args.base)) + if not chosen: + print("Nothing to integrate.") + return 2 if args.branches else 0 + landed, failed = integrate_set(args, chosen) + print(f"Integrated: {', '.join(landed) or 'nothing'}. Not integrated: {', '.join(failed) or 'none'}.") + return 1 if failed else 0 + + +def integrate_set(args, branches): + """Merge and gate branches together; if the gate fails, try each half. Returns (integrated, rejected).""" + landed = integrate_once(args, branches) + if landed is not None: + return landed, [] + if len(branches) == 1 or args.no_bisect: + return [], branches + half = len(branches) // 2 + print(f"Trying {', '.join(branches[:half])} and {', '.join(branches[half:])} apart.") + first, first_failed = integrate_set(args, branches[:half]) + second, second_failed = integrate_set(args, branches[half:]) + return first + second, first_failed + second_failed + + +def integrate_once(args, branches): + """Merge branches into the integration worktree and gate them: the branches integrated, or None if the gate failed.""" + args = argparse.Namespace(**{**vars(args), "branches": branches}) base_tree = integration(args.base) if git("status", "--porcelain", "--untracked-files=no", cwd=base_tree): raise SystemExit(f"{base_tree} has uncommitted changes; commit or stash them first") @@ -127,22 +249,21 @@ def integrate(args): git("merge", "--abort", cwd=base_tree, check=False) git("reset", "--hard", before, cwd=base_tree) print(f"{branch} does not merge cleanly into {args.base}. Conflicts:\n{conflicts or merge.stdout + merge.stderr}") - print(f"Merge {args.base} into {branch}, resolve them there, and integrate again. Nothing was integrated.") - return 2 + return None names = ", ".join(args.branches) if git("rev-parse", "HEAD", cwd=base_tree) == before: print(f"{names}: nothing new for {args.base}.") - return 0 + return [] gate = [sys.executable, "tools/gate.py", "--known-failures", ",".join(known)] gate += ["-j", str(args.jobs)] if args.jobs is not None else [] gate += ["--no-parity"] if args.no_parity else [] if subprocess.run(gate, cwd=base_tree).returncode: git("reset", "--hard", before, cwd=base_tree) print(f"The gate failed, so {names} were not integrated; {args.base} is back at {before[:9]}.") - return 1 + return None git("push", "-q", "origin", f"{args.base}:{args.push_to}", cwd=base_tree) print(f"Integrated {names}; {args.base} pushed to origin/{args.push_to} at {git('rev-parse', '--short', 'HEAD', cwd=base_tree)}.") - return 0 + return list(args.branches) def main(): @@ -157,8 +278,14 @@ def main(): command = commands.add_parser("save") command.add_argument("areas", nargs="*") command.set_defaults(run=save) + command = commands.add_parser("check") + command.add_argument("branches", nargs="*", help="default: every parity/* branch with new commits") + command.set_defaults(run=check) command = commands.add_parser("integrate") - command.add_argument("branches", nargs="+", help="merged in order, then gated together") + command.add_argument("branches", nargs="*", help="the largest set of them that merges cleanly is merged in order, " + "then gated together") + command.add_argument("--ready", action="store_true", help="every parity/* branch with new commits") + command.add_argument("--no-bisect", action="store_true", help="if the gate fails, don't retry halves of the set") command.add_argument("-j", "--jobs", type=int, help="harnesses at once on this machine (default: tools/parity.py's)") command.add_argument("--no-parity", action="store_true", help="skip the vanilla harnesses in the gate") command.set_defaults(run=integrate)