Answer stability round 1: 2 🔴, 8 🟡 (1 misread); the scoring-run gate follows the finish, a rerun commits what a finish left, and the queue wait ends after 30 minutes

This commit is contained in:
2026-11-15 21:56:11 +01:00
parent 0109f97cd7
commit aff46cd64e
2 changed files with 236 additions and 94 deletions
+114 -63
View File
@@ -25,37 +25,43 @@ ready marks the pull request ready for review. On Gitea a draft is a pull req
starts with `WIP: `, so there it drops that prefix. Given <title>, one line, which it holds starts with `WIP: `, so there it drops that prefix. Given <title>, one line, which it holds
to the template as open does, it sets that to the template as open does, it sets that
as the pull request's title first; where that fails, the pull request stays a draft. as the pull request's title first; where that fails, the pull request stays a draft.
merge merges the pull request in its turn: the chunks of one project on this machine merge one merge merges the pull request in its turn: the chunks of one project on this machine take
at a time, each waiting until the one before it has merged or failed. In its turn it turns, one at a time. Run it in the background. In its turn it:
fetches origin and merges its default branch into the branch, as sync does; finishes each 1. fetches origin and merges its default branch into the branch, as sync does;
plan item a --finish names with `todo-item finish`, passing on --changelog or 2. finishes each plan item a --finish names with `todo-item finish`, passing on
--no-changelog, and commits what that changed in the plan and the changelog; and pushes. --changelog or --no-changelog, and commits what that changed in the plan and the
So the plan and the changelog change only at merge time, on top of every chunk merged changelog, so both change only at merge time, on top of every chunk merged before;
before. An item the plan no longer holds was finished by an earlier run, and stays so. 3. pushes, also where step 2 failed after step 1 brought changes in;
On a merge conflict it names the conflicted files and leaves the merge in progress: 4. waits for the pull request's checks, and those origin's default branch requires, to
resolve them, commit, push, and run it again. Where the default branch brought changes in pass on the head; where no check reports within a minute, it takes the pull request
and the branch has recorded a comprehension check, it pushes and stops, since a paired as having none;
ruling read the head before them: run the ruling again, then merge again. 5. merges the pull request, only while its head is still the commit checked out, so a
Then it waits for the pull request's checks, and those origin's default branch requires, to pass push after the gate is never merged. It takes the first merge method the repository
on the head, then merges the pull request, and only while its head is still the commit allows of: a merge commit, fast-forward only, a rebase followed by a merge commit, a
checked out, so a push after the gate is never merged. Where no check reports within a rebase, a squash. GitHub offers only the merge commit, the rebase and the squash.
minute, it takes the pull request as having none. It refuses where a check fails, or, on Its last line names each --finish item the plan did not hold when it began: an earlier
Gitea, where the checks have not all passed within 30 minutes; on GitHub, gh watches them run finished that item, or its ID is mistyped.
until they end. Run it in the background.
It takes the first merge method the repository allows of: a merge commit, fast-forward
only, a rebase followed by a merge commit, a rebase, a squash. GitHub offers only the
merge commit, the rebase and the squash. It also refuses where `larv-review
scoring-run-owed` exits 1, because the chunk owes a scoring run it has not recorded.
Where the project's config sets `wait-for-approval = true` (`project-config --help`), it Where the project's config sets `wait-for-approval = true` (`project-config --help`), it
also waits, once the checks have passed, until GitHub reports the pull request approved merges only once GitHub reports the pull request approved as its branch protection
as its branch protection requires. It waits outside its turn, so other chunks merge requires. It waits for that outside its turn, so other chunks merge meanwhile, and takes
meanwhile, and takes a new turn once approved. It refuses at once where a reviewer requested changes, a new turn once approved.
where the protection requires no review, on Gitea, and where it cannot read the config. It stops, without merging, and says what to do before running it again:
It stops too where a reviewer's comment waits for an answer: a review thread nobody - on a merge conflict in step 1, naming the conflicted files, with the merge in progress;
resolved whose last comment is not the pull request author's. Answer it in its thread, - where `larv-review scoring-run-owed` exits 1 after step 3, because the chunk owes a
push any fix, and run merge again to go on waiting. scoring run it has not recorded;
After 90 minutes without an approval it refuses too; run it again once the pull request - where origin's default branch has moved since the branch last recorded a paired
is approved. ruling, since that ruling read a head that no longer merges;
- where a check fails, or, on Gitea, where the checks have not all passed within 30
minutes; on GitHub, gh watches them until they end;
- where another pull request still holds the turn after 30 minutes, naming it;
- with `wait-for-approval`: where a reviewer requested changes, where the protection
requires no review, on Gitea, where it cannot read the config, and after 90 minutes
without an approval;
- with `wait-for-approval`, approved or not: where a reviewer's comment waits for an
answer, which is a review thread nobody resolved whose last comment is not the pull
request author's. Answer it in its thread and push any fix.
A run can last the 30 minutes in the queue, the checks and the 90 minutes together. One
killed before that left the branch pushed, so run it again.
clean acts once the forge reports the branch's pull request merged at the commit checked out, clean acts once the forge reports the branch's pull request merged at the commit checked out,
by any merge method. It fetches origin, fast-forwards the main checkout to origin's by any merge method. It fetches origin, fast-forwards the main checkout to origin's
default branch, removes the chunk's worktree, then deletes the local branch; origin's default branch, removes the chunk's worktree, then deletes the local branch; origin's
@@ -76,7 +82,7 @@ import sys
import time import time
import typing import typing
from chunk_pr.approval import approval, approved, awaited from chunk_pr.approval import approval, approved, awaited, unanswered
from deploy_release.forge import checks_passed, gitea, merged_pull, open_pull, required_checks, Run, runner from deploy_release.forge import checks_passed, gitea, merged_pull, open_pull, required_checks, Run, runner
from failure import Failure from failure import Failure
from chunk_preflight.gathered import Forge from chunk_preflight.gathered import Forge
@@ -88,6 +94,11 @@ BIN = pathlib.Path(__file__).resolve().parents[1]
NO_CHECKS_SECONDS = 60 NO_CHECKS_SECONDS = 60
# The file, in the git folder that every worktree of the checkout shares, that a merge keeps locked with flock for its turn: so every chunk of the project on this machine queues on it. # The file, in the git folder that every worktree of the checkout shares, that a merge keeps locked with flock for its turn: so every chunk of the project on this machine queues on it.
QUEUE = "chunk-pr-merge-queue" QUEUE = "chunk-pr-merge-queue"
# A turn lasts as long as its checks, which chunk-pr waits half an hour for on Gitea, so a queue still held after that is reported, not waited out.
QUEUE_SECONDS = 30 * 60
QUEUE_POLL_SECONDS = 10
# The line record-ruling writes into the comprehension history, as git's -G reads it.
RULING = "^Ruling: "
USAGE = "usage: chunk-pr open <title> [<body>] | chunk-pr sync | chunk-pr ready [<title>] | chunk-pr merge [--finish <ID> [--changelog <bullet> | --no-changelog]]... | chunk-pr clean" USAGE = "usage: chunk-pr open <title> [<body>] | chunk-pr sync | chunk-pr ready [<title>] | chunk-pr merge [--finish <ID> [--changelog <bullet> | --no-changelog]]... | chunk-pr clean"
# Each forge's merge methods in the order merge prefers them, as the repository setting that allows the method and the method's name in the forge's merge call. A merge commit and a fast-forward keep the branch's commits as they are; a rebase or a squash rewrites them. # Each forge's merge methods in the order merge prefers them, as the repository setting that allows the method and the method's name in the forge's merge call. A merge commit and a fast-forward keep the branch's commits as they are; a rebase or a squash rewrites them.
@@ -231,8 +242,8 @@ def synced(run: Run) -> str | Failure:
return f"{done}\nrank_todo.py --check todo.md passed." return f"{done}\nrank_todo.py --check todo.md passed."
def levelled(run: Run, branch: str, base: str) -> bool | Failure: def levelled(run: Run, branch: str, base: str, resolve: str = "resolve them and commit") -> bool | Failure:
"""Fetch origin and merge its base into branch, the one checked out: whether that brought anything in. On a conflict the merge stays in progress, and the Failure names the conflicted files.""" """Fetch origin and merge its base into branch, the one checked out: whether that brought anything in. On a conflict the merge stays in progress, and the Failure names the conflicted files and ends with resolve, the caller's words for what to do about them."""
fetched = run(["git", "fetch", "--quiet", "origin"]) fetched = run(["git", "fetch", "--quiet", "origin"])
if isinstance(fetched, Failure): if isinstance(fetched, Failure):
return fetched return fetched
@@ -246,7 +257,7 @@ def levelled(run: Run, branch: str, base: str) -> bool | Failure:
conflicted = answers(run, ["diff", "--name-only", "--diff-filter=U"]) conflicted = answers(run, ["diff", "--name-only", "--diff-filter=U"])
if isinstance(conflicted, Failure) or not conflicted[0]: if isinstance(conflicted, Failure) or not conflicted[0]:
return merged return merged
return Failure(f"merging origin/{base} into {branch} conflicts in {', '.join(conflicted[0].splitlines())}; the merge stays in progress: resolve them and commit") return Failure(f"merging origin/{base} into {branch} conflicts in {', '.join(conflicted[0].splitlines())}; the merge stays in progress: {resolve}")
return True return True
@@ -272,11 +283,11 @@ def finishes_of(given: list[str]) -> list[list[str]] | None:
finishes, rest = [], given finishes, rest = [], given
while rest: while rest:
match rest: match rest:
case ["--finish", item, "--changelog", bullet, *rest] if not item.startswith("-"): case ["--finish", item, "--changelog", bullet, *rest] if item.isdigit():
finishes.append([item, "--changelog", bullet]) finishes.append([item, "--changelog", bullet])
case ["--finish", item, "--no-changelog", *rest] if not item.startswith("-"): case ["--finish", item, "--no-changelog", *rest] if item.isdigit():
finishes.append([item, "--no-changelog"]) finishes.append([item, "--no-changelog"])
case ["--finish", item, *rest] if not item.startswith("-"): case ["--finish", item, *rest] if item.isdigit():
finishes.append([item]) finishes.append([item])
case _: case _:
return None return None
@@ -292,9 +303,6 @@ def merged(run: Run, sleep: typing.Callable[[float], None] = time.sleep, finishe
waits = awaited(forge, run) waits = awaited(forge, run)
if isinstance(waits, Failure): if isinstance(waits, Failure):
return waits return waits
owed = run([str(BIN / "larv-review"), "scoring-run-owed"])
if isinstance(owed, Failure):
return owed
method = merge_method(forge, run) method = merge_method(forge, run)
if isinstance(method, Failure): if isinstance(method, Failure):
return method return method
@@ -305,63 +313,102 @@ def merged(run: Run, sleep: typing.Callable[[float], None] = time.sleep, finishe
queue = answers(run, ["rev-parse", "--path-format=absolute", "--git-common-dir"]) queue = answers(run, ["rev-parse", "--path-format=absolute", "--git-common-dir"])
if isinstance(queue, Failure): if isinstance(queue, Failure):
return queue return queue
absent = unplanned(run, finishes)
if isinstance(absent, Failure):
return absent
file = pathlib.Path(queue[0]) / QUEUE
while True: while True:
try: try:
with turn(pathlib.Path(queue[0]) / QUEUE): with turn(file, number, sleep):
head = levelled_and_finished(run, str(base), finishes) head = levelled_and_finished(run, str(base), finishes)
checked = head if isinstance(head, Failure) else checks_passed(forge, number, head, required, run, sleep, NO_CHECKS_SECONDS) checked = head if isinstance(head, Failure) else checks_passed(forge, number, head, required, run, sleep, NO_CHECKS_SECONDS)
if isinstance(checked, Failure): if isinstance(checked, Failure):
return checked return checked
ready = approval(number, run) if waits else True ready = approval(number, run) if waits else True
asked = unanswered(number, run) if waits and ready is True else None
if isinstance(ready, Failure): if isinstance(ready, Failure):
return ready return ready
if asked is not None:
return asked
if ready and forge == "gh": if ready and forge == "gh":
done = run(["gh", "pr", "merge", number, method, "--match-head-commit", str(head)]) done = run(["gh", "pr", "merge", number, method, "--match-head-commit", str(head)])
elif ready: elif ready:
done = gitea(run, f"repos/{{owner}}/{{repo}}/pulls/{number}/merge", f"Do={method}", f"head_commit_id={head}") done = gitea(run, f"repos/{{owner}}/{{repo}}/pulls/{number}/merge", f"Do={method}", f"head_commit_id={head}")
if ready: if ready:
return done if isinstance(done, Failure) else f"Merged pull request #{number} ({method.removeprefix('--')})." return done if isinstance(done, Failure) else f"Merged pull request #{number} ({method.removeprefix('--')}).{absent}"
except Held as held:
return Failure(f"pull request #{held.args[0] or '?'} has held the merge queue for {QUEUE_SECONDS // 60} minutes, so this one took no turn; run chunk-pr merge again")
except OSError as failure: except OSError as failure:
return Failure(f"cannot take a turn in the merge queue, {failure.filename}: {failure.strerror}") return Failure(f"cannot take a turn in the merge queue, {file}: {failure.strerror}")
waited = approved(number, run, sleep) waited = approved(number, run, sleep)
if isinstance(waited, Failure): if isinstance(waited, Failure):
return waited return waited
def unplanned(run: Run, finishes: typing.Sequence[list[str]]) -> str | Failure:
"""A sentence for merge's last line, naming each item of finishes the plan doesn't hold as merge begins: an earlier run finished it, or its ID is mistyped, and only the reader can tell which. Empty where the plan holds them all."""
absent = []
for finish in finishes:
shown = run([str(BIN / "todo-item"), "show", finish[0]])
if isinstance(shown, Failure) and f"holds no item {finish[0]}" not in shown.reason:
return shown
absent += [finish[0]] if isinstance(shown, Failure) else []
return f" The plan held no item {', '.join(absent)} when merge began, so merge left the plan as it was there." if absent else ""
class Held(Exception):
"""Another pull request, whose number is the argument, kept the merge queue for QUEUE_SECONDS."""
@contextlib.contextmanager @contextlib.contextmanager
def turn(queue: pathlib.Path) -> typing.Iterator[None]: def turn(queue: pathlib.Path, number: str, sleep: typing.Callable[[float], None]) -> typing.Iterator[None]:
"""Hold the project's merge queue: wait until no other chunk-pr merge on this machine holds it, and free it on the way out, however that is. Raises OSError where the file cannot be opened or locked.""" """Hold the project's merge queue as pull request number: wait until no other chunk-pr merge on this machine holds it, and free it on the way out, however that is. Raises Held where another one still holds it after QUEUE_SECONDS, and OSError where the file cannot be opened or locked."""
fd = os.open(queue, os.O_RDWR | os.O_CREAT, 0o644) fd = os.open(queue, os.O_RDWR | os.O_CREAT, 0o644)
try: try:
fcntl.flock(fd, fcntl.LOCK_EX) for waited in range(0, QUEUE_SECONDS + 1, QUEUE_POLL_SECONDS):
try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
break
except BlockingIOError:
if waited == QUEUE_SECONDS:
raise Held(os.pread(fd, 20, 0).decode(errors="replace").strip()) from None
sleep(QUEUE_POLL_SECONDS)
os.ftruncate(fd, 0)
os.pwrite(fd, number.encode(), 0)
yield yield
finally: finally:
os.close(fd) os.close(fd)
def levelled_and_finished(run: Run, base: str, finishes: typing.Sequence[list[str]]) -> str | Failure: def levelled_and_finished(run: Run, base: str, finishes: typing.Sequence[list[str]]) -> str | Failure:
"""What merge does to the branch inside its turn, before the checks: merge origin's base into it, finish the plan items, and push. The head to merge, or why the worker must act first.""" """What merge does to the branch inside its turn, before the checks: merge origin's base into it, finish the plan items, and push. The head to merge, or why the worker must act first. Whatever it committed is pushed before it fails, so the pull request's head stays the commit checked out."""
found = checkout(run) found = checkout(run)
if isinstance(found, Failure): if isinstance(found, Failure):
return found return found
_, branch, before = found _, branch, before = found
moved = levelled(run, branch, base) moved = levelled(run, branch, base, "resolve them, commit, push, and run chunk-pr merge again")
failed = moved if isinstance(moved, Failure) else next((failure for finish in finishes if (failure := finished(run, branch, finish))), None) if isinstance(moved, Failure):
head = failed or answers(run, ["rev-parse", "HEAD"]) return moved
failed = next((failure for finish in finishes if (failure := finished(run, branch, finish))), None)
head = answers(run, ["rev-parse", "HEAD"])
if isinstance(head, Failure): if isinstance(head, Failure):
return head return head
pushed = run(["git", "push", "--quiet", "origin", branch]) if head[0] != before else "" pushed = run(["git", "push", "--quiet", "origin", branch]) if head[0] != before else ""
if isinstance(pushed, Failure): if isinstance(pushed, Failure):
return pushed return pushed
return (remeasure(run, base) if moved else None) or head[0] # The scoring run is asked for only now: scoring-run-owed counts as shipped the items the committed plan no longer holds.
owed = failed or run([str(BIN / "larv-review"), "scoring-run-owed"])
if isinstance(owed, Failure):
return owed
return remeasure(run, base) or head[0]
def finished(run: Run, branch: str, finish: list[str]) -> Failure | None: def finished(run: Run, branch: str, finish: list[str]) -> Failure | None:
"""Finish one plan item with `todo-item finish` and commit what that changed. An item the plan no longer holds was finished by an earlier run of merge, and stays as it is.""" """Finish one plan item with `todo-item finish` and commit what that changed. An item the plan no longer holds was finished by an earlier run of merge, which may have stopped before its commit: so the plan and the changelog are committed either way."""
shown = run([str(BIN / "todo-item"), "show", finish[0]]) shown = run([str(BIN / "todo-item"), "show", finish[0]])
if isinstance(shown, Failure): if isinstance(shown, Failure) and f"holds no item {finish[0]}" not in shown.reason:
return None if f"holds no item {finish[0]}" in shown.reason else shown return shown
read = [run([str(BIN / "todo-item"), "finish", *finish]), run([str(BIN / "project-config"), "commit-title"]), run([str(BIN / "project-config"), "branch"]), run([str(BIN / "project-config"), "plan"]), run([str(BIN / "project-config"), "changelog"])] read = ["" if isinstance(shown, Failure) else run([str(BIN / "todo-item"), "finish", *finish]), run([str(BIN / "project-config"), "commit-title"]), run([str(BIN / "project-config"), "branch"]), run([str(BIN / "project-config"), "plan"]), run([str(BIN / "project-config"), "changelog"])]
if failed := next((answer for answer in read if isinstance(answer, Failure)), None): if failed := next((answer for answer in read if isinstance(answer, Failure)), None):
return failed return failed
_, template, branch_template, *records = (str(answer).strip() for answer in read) _, template, branch_template, *records = (str(answer).strip() for answer in read)
@@ -372,7 +419,7 @@ def finished(run: Run, branch: str, finish: list[str]) -> Failure | None:
def committed(run: Run, title: str, files: list[pathlib.Path]) -> Failure | None: def committed(run: Run, title: str, files: list[pathlib.Path]) -> Failure | None:
"""Commit what changed in files under title, each in the git repository that holds it, and only those files: a record the config keeps in its own folder is committed there, where another chunk may commit at once.""" """Commit what changed in files under title, each in the git repository that holds it, and only those files: a record the config keeps in its own folder is committed there, where another chunk may commit at the same time."""
held: dict[str, list[str]] = {} held: dict[str, list[str]] = {}
for file in files: for file in files:
top = run(["git", "-C", str(file.parent), "rev-parse", "--show-toplevel"]) top = run(["git", "-C", str(file.parent), "rev-parse", "--show-toplevel"])
@@ -388,16 +435,20 @@ def committed(run: Run, title: str, files: list[pathlib.Path]) -> Failure | None
def remeasure(run: Run, base: str) -> Failure | None: def remeasure(run: Run, base: str) -> Failure | None:
"""A Failure where the branch recorded a comprehension check before origin's base, just merged in, brought in another chunk's change: the check read a head that no longer merges. None where the branch recorded none, or the history is no file in the checkout.""" """A Failure where origin's base has moved since the branch last recorded a paired ruling: that ruling read a head that no longer merges. None where the branch recorded none, or the project keeps its history outside the checkout."""
history = run([str(BIN / "project-config"), "comprehension-history"]) history = run([str(BIN / "project-config"), "comprehension-history"])
if isinstance(history, Failure): if isinstance(history, Failure):
return history return history
if not pathlib.Path(history.strip()).is_file(): top = answers(run, ["rev-parse", "--show-toplevel"])
return None if isinstance(top, Failure) or not pathlib.Path(history.strip()).resolve().is_relative_to(top[0]):
recorded = answers(run, ["diff", "--name-only", f"origin/{base}...HEAD", "--", history.strip()]) return top if isinstance(top, Failure) else None
if isinstance(recorded, Failure) or not recorded[0]: ruled = answers(run, ["log", "-1", "--format=%H", "-G", RULING, f"origin/{base}..HEAD", "--", history.strip()])
return recorded if isinstance(recorded, Failure) else None if isinstance(ruled, Failure) or not ruled[0].strip():
return Failure(f"origin/{base} moved after the branch recorded its comprehension check, and its changes are merged in and pushed; where the chunk recorded a paired ruling, run it again at the new head, record it, and run chunk-pr merge again") return ruled if isinstance(ruled, Failure) else None
since = answers(run, ["rev-list", "--count", f"{ruled[0].strip()}..origin/{base}"])
if isinstance(since, Failure) or since[0].strip() == "0":
return since if isinstance(since, Failure) else None
return Failure(f"origin/{base} moved after the branch recorded its paired ruling, and its changes are merged in and pushed; run the paired ruling again at the new head, record it, push, and run chunk-pr merge again")
def merge_method(forge: Forge, run: Run) -> str | Failure: def merge_method(forge: Forge, run: Run) -> str | Failure:
+122 -31
View File
@@ -1,3 +1,4 @@
import contextlib
import fcntl import fcntl
import json import json
import os import os
@@ -67,6 +68,7 @@ class Checkout:
tuple(BRANCH): "item-{id}\n", tuple(BRANCH): "item-{id}\n",
tuple(BEHIND): "0\n", tuple(BEHIND): "0\n",
tuple(COMMON): f"{GIT_FOLDER.name}\n", tuple(COMMON): f"{GIT_FOLDER.name}\n",
("git", "rev-parse", "--show-toplevel"): "/checkout\n",
} }
def __call__(self, command: list[str]) -> str | Failure: def __call__(self, command: list[str]) -> str | Failure:
@@ -297,7 +299,7 @@ class Sync(unittest.TestCase):
def changes(checkout: Checkout) -> list[list[str]]: def changes(checkout: Checkout) -> list[list[str]]:
"""What a merge ran beyond its reads, of the project's config too, and the fetch its turn opens with.""" """What a merge ran, leaving out what it read, the project's config included, and the fetch each turn opens with."""
return [command for command in checkout.acted() if command != FETCH and not command[0].endswith("/project-config")] return [command for command in checkout.acted() if command != FETCH and not command[0].endswith("/project-config")]
@@ -355,7 +357,7 @@ class Merge(unittest.TestCase):
def test_an_approved_pull_request_merges_after_the_checks_and_the_approval(self): def test_an_approved_pull_request_merges_after_the_checks_and_the_approval(self):
checkout = self.requiring_approval() checkout = self.requiring_approval()
self.assertEqual(step(["merge"], checkout), "Merged pull request #12 (merge).") self.assertEqual(step(["merge"], checkout), "Merged pull request #12 (merge).")
self.assertEqual(checkout.ran[-3:], [GH_CHECKS, GH_DECISION, ["gh", "pr", "merge", "12", "--merge", "--match-head-commit", HEAD]]) self.assertEqual(checkout.ran[-4:], [GH_CHECKS, GH_DECISION, GH_THREADS, ["gh", "pr", "merge", "12", "--merge", "--match-head-commit", HEAD]])
def test_it_waits_a_minute_at_a_time_while_the_branch_protection_still_wants_a_review(self): def test_it_waits_a_minute_at_a_time_while_the_branch_protection_still_wants_a_review(self):
checkout = self.requiring_approval(GITHUB, "REVIEW_REQUIRED", "REVIEW_REQUIRED", "APPROVED") checkout = self.requiring_approval(GITHUB, "REVIEW_REQUIRED", "REVIEW_REQUIRED", "APPROVED")
@@ -558,8 +560,12 @@ class MergeQueue(unittest.TestCase):
checkout.__call__ = committing # type: ignore[method-assign] checkout.__call__ = committing # type: ignore[method-assign]
return checkout, folder return checkout, folder
def test_malformed_finish_flags_run_nothing(self): @staticmethod
for flags in (["--finish"], ["--changelog", "Bullet."], ["12"], ["--finish", "--no-changelog"], ["--finish", "7", "--changelog"], ["--finish", "7", "--no-changelog", "--changelog", "Bullet."]): def thread(path: str, last: str, resolved: bool = False) -> dict[str, object]:
return {"isResolved": resolved, "path": path, "comments": {"nodes": [{"author": {"login": last}}]}}
def test_malformed_finish_flags_dont_run_anything(self):
for flags in (["--finish"], ["--finish", "abc"], ["--changelog", "Bullet."], ["12"], ["--finish", "--no-changelog"], ["--finish", "7", "--changelog"], ["--finish", "7", "--no-changelog", "--changelog", "Bullet."]):
with self.subTest(flags): with self.subTest(flags):
checkout = Checkout(GITHUB, open=True) checkout = Checkout(GITHUB, open=True)
self.assertEqual(step(["merge", *flags], checkout), Failure(USAGE)) self.assertEqual(step(["merge", *flags], checkout), Failure(USAGE))
@@ -577,7 +583,7 @@ class MergeQueue(unittest.TestCase):
checkout.answers[("git", "diff", "--name-only", "--diff-filter=U")] = "bin/tool.py\n" checkout.answers[("git", "diff", "--name-only", "--diff-filter=U")] = "bin/tool.py\n"
failed = merged(checkout.__call__) failed = merged(checkout.__call__)
self.assertIsInstance(failed, Failure) self.assertIsInstance(failed, Failure)
self.assertIn("conflicts in bin/tool.py", failed.reason) self.assertIn("conflicts in bin/tool.py; the merge stays in progress: resolve them, commit, push, and run chunk-pr merge again", failed.reason)
self.assertEqual((changes(checkout), MERGE in checkout.ran), ([], True)) self.assertEqual((changes(checkout), MERGE in checkout.ran), ([], True))
self.assertTrue(queue_is_free()) self.assertTrue(queue_is_free())
@@ -595,14 +601,33 @@ class MergeQueue(unittest.TestCase):
def test_the_flags_reach_todo_item_as_given(self): def test_the_flags_reach_todo_item_as_given(self):
checkout, _ = self.with_plan(Checkout(GITHUB, open=True)) checkout, _ = self.with_plan(Checkout(GITHUB, open=True))
checkout.answers[(self.TODO_ITEM, "show", "8")] = "From todo.md:\n" checkout.answers.update({(self.TODO_ITEM, "show", "8"): "From todo.md:\n", (self.TODO_ITEM, "show", "9"): "From todo.md:\n"})
self.assertEqual(step(["merge", "--finish", "7", "--no-changelog", "--finish", "8"], checkout.__call__), "Merged pull request #12 (merge).") self.assertEqual(step(["merge", "--finish", "7", "--no-changelog", "--finish", "8", "--finish", "9", "--changelog", "Exports keep their order."], checkout.__call__), "Merged pull request #12 (merge).")
self.assertEqual([command[1:] for command in checkout.ran if command[:2] == [self.TODO_ITEM, "finish"]], [["finish", "7", "--no-changelog"], ["finish", "8"]]) self.assertEqual([command[1:] for command in checkout.ran if command[:2] == [self.TODO_ITEM, "finish"]], [["finish", "7", "--no-changelog"], ["finish", "8"], ["finish", "9", "--changelog", "Exports keep their order."]])
def test_an_item_an_earlier_run_finished_stays_as_it_is(self): def test_an_item_the_plan_no_longer_holds_is_named_on_the_last_line_and_what_an_earlier_run_left_uncommitted_is_committed(self):
checkout, _ = self.with_plan(Checkout(GITHUB, open=True), held=False) checkout, folder = self.with_plan(Checkout(GITHUB, open=True), held=False)
self.assertEqual(merged(checkout.__call__, finishes=[["7"]]), "Merged pull request #12 (merge).") self.assertEqual(merged(checkout.__call__, finishes=[["7"]]), "Merged pull request #12 (merge). The plan held no item 7 when merge began, so merge left the plan as it was there.")
self.assertEqual(changes(checkout), [["gh", "pr", "merge", "12", "--merge", "--match-head-commit", HEAD]]) self.assertNotIn([self.TODO_ITEM, "finish", "7"], checkout.ran)
self.assertEqual([command[3] for command in changes(checkout)[:-1]], ["add", "commit", "origin"])
self.assertEqual(changes(checkout)[-1], ["gh", "pr", "merge", "12", "--merge", "--match-head-commit", "f00d"])
def test_a_finish_that_changes_no_file_commits_and_pushes_nothing(self):
for clean in ("status", "records"):
with self.subTest(clean):
checkout, folder = self.with_plan(Checkout(GITHUB, open=True))
if clean == "status":
checkout.answers[("git", "-C", folder, "status", "--porcelain", "--", f"{folder}/todo.md", f"{folder}/CHANGELOG.md")] = "\n"
else:
checkout.answers.update({(self.CONFIG, "plan"): "PAY-1\n", (self.CONFIG, "changelog"): f"{folder}/none.md\n"})
self.assertEqual(merged(checkout.__call__, finishes=[["7", "--no-changelog"]]), "Merged pull request #12 (merge).")
self.assertEqual(changes(checkout), [[self.TODO_ITEM, "finish", "7", "--no-changelog"], ["gh", "pr", "merge", "12", "--merge", "--match-head-commit", HEAD]])
def test_a_branch_outside_the_branch_template_titles_the_finish_with_the_summary_alone(self):
checkout, folder = self.with_plan(Checkout(GITHUB, open=True))
checkout.answers[tuple(BRANCH)] = "feature/PAY-{id}\n"
merged(checkout.__call__, finishes=[["7"]])
self.assertIn("Finish item 7 in the plan", [command[6] for command in checkout.ran if command[3:4] == ["commit"]])
def test_a_plan_it_cannot_read_or_an_item_it_cannot_finish_leaves_it_unmerged(self): def test_a_plan_it_cannot_read_or_an_item_it_cannot_finish_leaves_it_unmerged(self):
for failing in ("show", "finish"): for failing in ("show", "finish"):
@@ -612,6 +637,34 @@ class MergeQueue(unittest.TestCase):
self.assertEqual(merged(checkout.__call__, finishes=[["7"]]), Failure(f"todo-item {failing} 7: no config names the checkout")) self.assertEqual(merged(checkout.__call__, finishes=[["7"]]), Failure(f"todo-item {failing} 7: no config names the checkout"))
self.assertNotIn(self.PUSHED, checkout.ran) self.assertNotIn(self.PUSHED, checkout.ran)
def test_a_step_of_the_turn_that_fails_leaves_it_unmerged_with_that_failure(self):
_, folder = self.with_plan(Checkout(GITHUB, open=True))
for failing in (FETCH, BEHIND, COMMON, self.PUSHED, ["git", "-C", "<plan>", "rev-parse", "--show-toplevel"], ["git", "-C", "<plan>", "add"], ["git", "-C", "<plan>", "commit"], [self.CONFIG, "comprehension-history"]):
with self.subTest(failing):
checkout, folder = self.with_plan(Checkout(GITHUB, open=True))
run = checkout.__call__
def failing_run(command: list[str]) -> str | Failure:
asked = ["<plan>" if part == folder else part for part in command]
return Failure("that step failed") if tuple(asked[:len(failing)]) == tuple(failing) else run(command)
self.assertEqual(merged(failing_run, finishes=[["7"]]), Failure("that step failed"))
self.assertNotIn("merge", [command[2] for command in checkout.ran if command[0] == "gh"])
def test_a_finish_that_fails_after_the_default_branch_was_merged_in_still_pushes_the_branch(self):
checkout, _ = self.with_plan(self.moved(Checkout(GITHUB, open=True)))
checkout.answers[(self.TODO_ITEM, "finish", "7")] = Failure("todo-item finish 7: a decision cites item 7")
self.assertEqual(merged(checkout.__call__, finishes=[["7"]]), Failure("todo-item finish 7: a decision cites item 7"))
self.assertEqual(changes(checkout), [MERGE, self.PUSHED])
def test_the_scoring_run_is_asked_for_once_the_finish_is_committed_and_one_owed_leaves_it_unmerged(self):
checkout, _ = self.with_plan(Checkout(GITHUB, open=True))
commit = lambda: next(index for index, command in enumerate(checkout.ran) if command[3:4] == ["commit"])
self.assertEqual(merged(checkout.__call__, finishes=[["7"]]), "Merged pull request #12 (merge).")
self.assertLess(commit(), checkout.ran.index(OWED))
checkout, _ = self.with_plan(Checkout(GITHUB, open=True))
checkout.answers[tuple(OWED)] = Failure("the chunk owes 1 scoring run")
self.assertEqual(merged(checkout.__call__, finishes=[["7"]]), Failure("the chunk owes 1 scoring run"))
self.assertEqual(changes(checkout)[-1], self.PUSHED)
def test_a_merge_holds_the_queue_through_the_checks_and_frees_it_while_it_waits_for_an_approval(self): def test_a_merge_holds_the_queue_through_the_checks_and_frees_it_while_it_waits_for_an_approval(self):
checkout = Checkout(GITHUB, open=True) checkout = Checkout(GITHUB, open=True)
checkout.answers[tuple(WAITS)] = "true\n" checkout.answers[tuple(WAITS)] = "true\n"
@@ -626,12 +679,10 @@ class MergeQueue(unittest.TestCase):
self.assertEqual(checkout.ran.count(FETCH), 2) self.assertEqual(checkout.ran.count(FETCH), 2)
def test_a_reviewers_comment_ends_the_wait_naming_its_file_until_the_author_answers_or_it_is_resolved(self): def test_a_reviewers_comment_ends_the_wait_naming_its_file_until_the_author_answers_or_it_is_resolved(self):
def thread(path: str, last: str, resolved: bool = False) -> dict[str, object]:
return {"isResolved": resolved, "path": path, "comments": {"nodes": [{"author": {"login": last}}]}}
checkout = Checkout(GITHUB, open=True) checkout = Checkout(GITHUB, open=True)
checkout.answers[tuple(WAITS)] = "true\n" checkout.answers[tuple(WAITS)] = "true\n"
checkout.decisions = ["REVIEW_REQUIRED"] checkout.decisions = ["REVIEW_REQUIRED"]
checkout.threads = [thread("bin/asked.py", "reviewer"), thread("bin/answered.py", "worker"), thread("bin/resolved.py", "reviewer", resolved=True)] checkout.threads = [self.thread("bin/asked.py", "reviewer"), self.thread("bin/answered.py", "worker"), self.thread("bin/resolved.py", "reviewer", resolved=True)]
slept: list[float] = [] slept: list[float] = []
failed = merged(checkout, slept.append) failed = merged(checkout, slept.append)
self.assertIsInstance(failed, Failure) self.assertIsInstance(failed, Failure)
@@ -641,42 +692,82 @@ class MergeQueue(unittest.TestCase):
checkout.decisions = ["REVIEW_REQUIRED", "REVIEW_REQUIRED", "APPROVED"] checkout.decisions = ["REVIEW_REQUIRED", "REVIEW_REQUIRED", "APPROVED"]
self.assertEqual(merged(checkout, slept.append), "Merged pull request #12 (merge).") self.assertEqual(merged(checkout, slept.append), "Merged pull request #12 (merge).")
def test_an_approved_pull_request_with_a_comment_that_waits_for_an_answer_stays_unmerged(self):
checkout = Checkout(GITHUB, open=True)
checkout.answers[tuple(WAITS)] = "true\n"
checkout.threads = [self.thread("bin/asked.py", "reviewer")]
failed = merged(checkout)
self.assertIsInstance(failed, Failure)
self.assertIn("waits for an answer, in bin/asked.py;", failed.reason)
self.assertEqual(changes(checkout), [])
def test_review_threads_it_cannot_read_leave_it_unmerged(self): def test_review_threads_it_cannot_read_leave_it_unmerged(self):
for answer in (Failure("gh api graphql failed"), "not json", "{}"): for answer, reason in ((Failure("gh api graphql failed"), "gh api graphql failed"), ("not json", "gh api graphql printed no review threads: not json"), ("{}", "gh api graphql printed no review threads: {}")):
with self.subTest(answer): with self.subTest(answer):
checkout = Checkout(GITHUB, open=True) checkout = Checkout(GITHUB, open=True)
checkout.answers.update({tuple(WAITS): "true\n", tuple(GH_THREADS): answer}) checkout.answers.update({tuple(WAITS): "true\n", tuple(GH_THREADS): answer})
checkout.decisions = ["REVIEW_REQUIRED"] checkout.decisions = ["REVIEW_REQUIRED"]
self.assertIsInstance(merged(checkout, lambda seconds: None), Failure) self.assertEqual(merged(checkout, lambda seconds: None), Failure(reason))
self.assertEqual(changes(checkout), []) self.assertEqual(changes(checkout), [])
def test_a_default_branch_that_moved_after_the_branch_recorded_its_comprehension_check_stops_for_a_new_ruling(self): def test_a_default_branch_that_moved_after_the_branchs_last_paired_ruling_stops_for_a_new_one(self):
with tempfile.NamedTemporaryFile() as history: history = "/checkout/docs/comprehension-history.md"
for recorded, expected in (("docs/comprehension-history.md\n", [MERGE, self.PUSHED]), ("", [MERGE, self.PUSHED, ["gh", "pr", "merge", "12", "--merge", "--match-head-commit", "decaf"]])): ruled = ["git", "log", "-1", "--format=%H", "-G", "^Ruling: ", "origin/main..HEAD", "--", history]
with self.subTest(recorded): since = ["git", "rev-list", "--count", "bead..origin/main"]
checkout = self.moved(Checkout(GITHUB, open=True)) for name, answers, stops in (
checkout.answers[(self.CONFIG, "comprehension-history")] = f"{history.name}\n" ("moved since the ruling", {tuple(ruled): "bead\n", tuple(since): "2\n"}, True),
checkout.answers[("git", "diff", "--name-only", "origin/main...HEAD", "--", history.name)] = recorded ("ruled at this head", {tuple(ruled): "bead\n", tuple(since): "0\n"}, False),
done = merged(checkout.__call__) ("no ruling", {tuple(ruled): "\n"}, False),
self.assertEqual(changes(checkout), expected) ):
self.assertEqual(isinstance(done, Failure) and "paired ruling" in done.reason, bool(recorded)) with self.subTest(name):
checkout = Checkout(GITHUB, open=True)
checkout.answers.update({(self.CONFIG, "comprehension-history"): f"{history}\n", **answers})
done = merged(checkout.__call__)
self.assertEqual(isinstance(done, Failure) and "run the paired ruling again" in done.reason, stops, done)
self.assertEqual(changes(checkout), [] if stops else [["gh", "pr", "merge", "12", "--merge", "--match-head-commit", HEAD]])
def test_a_history_outside_the_checkout_or_one_git_cannot_read_never_stops_or_fails_the_merge_wrongly(self):
ruled = ("git", "log", "-1", "--format=%H", "-G", "^Ruling: ", "origin/main..HEAD", "--", "/checkout/history.md")
checkout = Checkout(GITHUB, open=True)
checkout.answers[(self.CONFIG, "comprehension-history")] = "/elsewhere/history.md\n"
self.assertEqual(merged(checkout.__call__), "Merged pull request #12 (merge).")
self.assertNotIn("log", [command[1] for command in checkout.ran])
for failing in (ruled, ("git", "rev-list", "--count", "bead..origin/main")):
with self.subTest(failing):
checkout = Checkout(GITHUB, open=True)
checkout.answers.update({(self.CONFIG, "comprehension-history"): "/checkout/history.md\n", ruled: "bead\n"})
checkout.answers[failing] = Failure("git failed")
self.assertEqual(merged(checkout.__call__), Failure("git failed"))
def test_a_turn_keeps_every_other_merge_out_until_it_ends_even_by_an_error(self): def test_a_turn_keeps_every_other_merge_out_until_it_ends_even_by_an_error(self):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
with turn(pathlib.Path(GIT_FOLDER.name) / QUEUE): with turn(pathlib.Path(GIT_FOLDER.name) / QUEUE, "12", lambda seconds: None):
self.assertFalse(queue_is_free()) self.assertFalse(queue_is_free())
raise ValueError raise ValueError
self.assertTrue(queue_is_free()) self.assertTrue(queue_is_free())
def test_a_queue_another_pull_request_holds_for_half_an_hour_leaves_it_unmerged_naming_the_holder(self):
checkout = Checkout(GITHUB, open=True)
slept: list[float] = []
with turn(pathlib.Path(GIT_FOLDER.name) / QUEUE, "31", lambda seconds: None):
failed = merged(checkout, slept.append)
self.assertEqual(failed, Failure("pull request #31 has held the merge queue for 30 minutes, so this one took no turn; run chunk-pr merge again"))
self.assertEqual((sum(slept), set(slept), changes(checkout)), (30 * 60, {10}, []))
def test_a_queue_freed_while_it_waits_gives_it_the_turn(self):
checkout = Checkout(GITHUB, open=True)
holder = contextlib.ExitStack()
holder.enter_context(turn(pathlib.Path(GIT_FOLDER.name) / QUEUE, "31", lambda seconds: None))
self.assertEqual(merged(checkout, lambda seconds: holder.close()), "Merged pull request #12 (merge).")
def test_a_queue_it_cannot_open_leaves_it_unmerged(self): def test_a_queue_it_cannot_open_leaves_it_unmerged(self):
checkout = Checkout(GITHUB, open=True) checkout = Checkout(GITHUB, open=True)
checkout.answers[tuple(COMMON)] = "/nonexistent/folder\n" checkout.answers[tuple(COMMON)] = "/nonexistent/folder\n"
failed = merged(checkout) self.assertEqual(merged(checkout), Failure("cannot take a turn in the merge queue, /nonexistent/folder/chunk-pr-merge-queue: No such file or directory"))
self.assertIsInstance(failed, Failure)
self.assertIn("merge queue", failed.reason)
self.assertEqual(checkout.acted(), []) self.assertEqual(checkout.acted(), [])
class Clean(unittest.TestCase): class Clean(unittest.TestCase):
"""A real bare repository origin whose default branch is main; the clone project of it; and the worktree project-item-7 on branch item-7, one commit ahead of main and pushed. """A real bare repository origin whose default branch is main; the clone project of it; and the worktree project-item-7 on branch item-7, one commit ahead of main and pushed.