Add chunk-pr sync and wait-for

This commit is contained in:
2026-11-12 08:43:14 +01:00
parent 0d32cab386
commit 2ab44beca2
7 changed files with 287 additions and 2 deletions
+3
View File
@@ -120,6 +120,9 @@
!bin/todo_item/*.py
!bin/transcript_archive/
!bin/transcript_archive/*.py
!bin/wait-for
!bin/wait_for/
!bin/wait_for/*.py
!docs/
!docs/*.md
!judge-field-trials/
+46 -2
View File
@@ -1,6 +1,7 @@
"""Open the chunk's pull request as a draft, mark it ready, or merge it, with gh where origin is on GitHub and tea elsewhere; once it has merged, remove the chunk's worktree and branch.
"""Open the chunk's pull request as a draft, bring its branch level with origin's default branch, mark it ready, or merge it, with gh where origin is on GitHub and tea elsewhere; once it has merged, remove the chunk's worktree and branch.
usage: chunk-pr open <title> [<body>]
chunk-pr sync
chunk-pr ready [<title>]
chunk-pr merge --gate-passed
chunk-pr clean
@@ -12,6 +13,11 @@ open pushes the branch, setting its upstream, and opens a draft pull request
default branch. <title> is one line, as ~/.claude/CLAUDE.md → "Git, commits, and PRs"
says, and <body> is the pull request's summary. Where the branch's pull request is open
already, it opens no second one. On origin's default branch it refuses, pushing nothing.
sync fetches origin and merges its default branch into the branch checked out, where the
branch does not hold it already, and prints which it did. Then, where the worktree's root
holds a todo.md, it runs `rank_todo.py --check` on it and prints the result, failing where
the check fails. On a merge conflict it names the conflicted files and leaves the merge in
progress, to resolve and commit. On origin's default branch it refuses, fetching nothing.
ready marks the pull request ready for review. On Gitea a draft is a pull request whose title
starts with `WIP: `, so there it drops that prefix. Given <title>, one line, it sets that
as the pull request's title first; where that fails, the pull request stays a draft.
@@ -45,7 +51,7 @@ from chunk_preflight.choices import forge_of
from deploy_release.forge import gitea, merged_pull, open_pull, Run, runner
BIN = pathlib.Path(__file__).resolve().parents[1]
USAGE = "usage: chunk-pr open <title> [<body>] | chunk-pr ready [<title>] | chunk-pr merge --gate-passed | chunk-pr clean"
USAGE = "usage: chunk-pr open <title> [<body>] | chunk-pr sync | chunk-pr ready [<title>] | chunk-pr merge --gate-passed | 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.
METHODS: dict[Forge, tuple[tuple[str, str], ...]] = {
@@ -71,6 +77,8 @@ def step(argv: list[str], run: Run) -> str | Failure:
match argv:
case ["open", title, *body] if len(body) <= 1:
return Failure("give a title of one line") if not one_line(title) else opened(title, body[0] if body else "", run)
case ["sync"]:
return synced(run)
case ["ready", *title] if len(title) <= 1:
return Failure("give a title of one line") if title and not one_line(title[0]) else marked_ready(run, title[0] if title else None)
case ["merge", "--gate-passed"]:
@@ -150,6 +158,42 @@ def opened(title: str, body: str, run: Run) -> str | Failure:
return number if isinstance(number, Failure) else f"Opened draft pull request #{number}."
def synced(run: Run) -> str | Failure:
found = checkout(run)
if isinstance(found, Failure):
return found
_, branch, _ = found
base = default_branch(run)
if isinstance(base, Failure):
return base
if branch == base:
return Failure(f"{branch} is origin's default branch; sync the chunk's branch")
fetched = run(["git", "fetch", "--quiet", "origin"])
if isinstance(fetched, Failure):
return fetched
behind = answers(run, ["rev-list", "--count", f"HEAD..origin/{base}"], ["rev-parse", "--show-toplevel"])
if isinstance(behind, Failure):
return behind
count, root = behind
if count == "0":
done = f"{branch} already holds origin/{base}."
else:
merged = run(["git", "merge", "--quiet", "--no-edit", f"origin/{base}"])
if isinstance(merged, Failure):
conflicted = answers(run, ["diff", "--name-only", "--diff-filter=U"])
if isinstance(conflicted, Failure) or not conflicted[0]:
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")
done = f"Merged origin/{base} into {branch}."
todo = pathlib.Path(root, "todo.md")
if not todo.is_file():
return done
ranked = run([str(BIN / "rank_todo.py"), "--check", str(todo)])
if isinstance(ranked, Failure):
return Failure(f"{done.removesuffix('.')}, but rank_todo.py --check todo.md failed: {ranked.reason}")
return f"{done}\nrank_todo.py --check todo.md passed."
def marked_ready(run: Run, title: str | None) -> str | Failure:
found = pull(run)
if isinstance(found, Failure):
+65
View File
@@ -155,6 +155,71 @@ class Ready(unittest.TestCase):
self.assertEqual(checkout.acted(), [])
FETCH = ["git", "fetch", "--quiet", "origin"]
BEHIND = ("git", "rev-list", "--count", "HEAD..origin/main")
MERGE = ["git", "merge", "--quiet", "--no-edit", "origin/main"]
CONFLICTED = ("git", "diff", "--name-only", "--diff-filter=U")
RANK = str(pathlib.Path(__file__).resolve().parents[1] / "rank_todo.py")
class Sync(unittest.TestCase):
def checkout(self, behind: int, todo: bool = False) -> Checkout:
"""A Checkout behind origin/main by behind commits, at a worktree root holding a todo.md where todo is True."""
checkout = Checkout(GITEA, open=True)
root = self.enterContext(tempfile.TemporaryDirectory())
if todo:
pathlib.Path(root, "todo.md").write_text("")
checkout.answers[("git", "rev-parse", "--show-toplevel")] = f"{root}\n"
checkout.answers[BEHIND] = f"{behind}\n"
self.todo = str(pathlib.Path(root, "todo.md"))
return checkout
def test_a_branch_behind_the_default_branch_merges_it(self):
checkout = self.checkout(behind=3)
self.assertEqual(step(["sync"], checkout), "Merged origin/main into item-7.")
self.assertEqual(checkout.acted(), [FETCH, MERGE])
def test_a_branch_that_holds_the_default_branch_merges_nothing(self):
checkout = self.checkout(behind=0)
self.assertEqual(step(["sync"], checkout), "item-7 already holds origin/main.")
self.assertEqual(checkout.acted(), [FETCH])
def test_a_worktree_with_a_todo_md_runs_the_rank_check_after_the_merge(self):
checkout = self.checkout(behind=1, todo=True)
self.assertEqual(step(["sync"], checkout), f"Merged origin/main into item-7.\nrank_todo.py --check todo.md passed.")
self.assertEqual(checkout.acted(), [FETCH, MERGE, [RANK, "--check", self.todo]])
def test_a_failing_rank_check_fails_naming_what_it_did(self):
checkout = self.checkout(behind=0, todo=True)
checkout.answers[(RANK, "--check", self.todo)] = Failure("rank_todo.py --check: item 3 is out of order")
self.assertEqual(step(["sync"], checkout), Failure("item-7 already holds origin/main, but rank_todo.py --check todo.md failed: rank_todo.py --check: item 3 is out of order"))
def test_a_conflict_leaves_the_merge_in_progress_and_names_the_conflicted_files(self):
checkout = self.checkout(behind=2, todo=True)
checkout.answers[tuple(MERGE)] = Failure("git merge --quiet: CONFLICT")
checkout.answers[CONFLICTED] = "todo.md\nREADME.md\n"
self.assertEqual(step(["sync"], checkout), Failure("merging origin/main into item-7 conflicts in todo.md, README.md; the merge stays in progress: resolve them and commit"))
self.assertNotIn([RANK, "--check", self.todo], checkout.ran)
self.assertNotIn(["git", "merge", "--abort"], checkout.ran)
def test_a_merge_failing_without_a_conflict_is_returned(self):
checkout = self.checkout(behind=2)
checkout.answers[tuple(MERGE)] = Failure("git merge --quiet: untracked files would be overwritten")
checkout.answers[CONFLICTED] = ""
self.assertEqual(step(["sync"], checkout), Failure("git merge --quiet: untracked files would be overwritten"))
def test_on_the_default_branch_it_refuses_and_fetches_nothing(self):
checkout = self.checkout(behind=0)
checkout.answers[("git", "symbolic-ref", "--short", "HEAD")] = "main\n"
self.assertIsInstance(step(["sync"], checkout), Failure)
self.assertNotIn(FETCH, checkout.ran)
def test_a_failed_fetch_is_returned(self):
checkout = self.checkout(behind=0)
checkout.answers[tuple(FETCH)] = Failure("git fetch --quiet: could not read from remote")
self.assertEqual(step(["sync"], checkout), Failure("git fetch --quiet: could not read from remote"))
class Merge(unittest.TestCase):
def test_without_the_gate_passed_it_refuses_before_anything_runs(self):
checkout = Checkout(GITHUB, open=True)
Executable
+9
View File
@@ -0,0 +1,9 @@
#!/usr/bin/env python3
import pathlib
import sys
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
from wait_for import main
sys.exit(main.main(sys.argv[1:]))
View File
+88
View File
@@ -0,0 +1,88 @@
"""Wait until a file a background job writes holds a line matching a pattern, and print that line.
usage: wait-for <file> [<pattern>] [--timeout <seconds>]
<pattern> is a Python regular expression, searched for in each line; it prints the first line that
holds a match. Without <pattern>, it waits until the file holds anything, and prints its last
line. It checks every 2 seconds and gives up after --timeout seconds, 540 by default, so it fits
one foreground call of the Bash tool, whose limit is 600 seconds.
Exit 0 on the line, 1 on the timeout or bad arguments, saying why.
"""
import pathlib
import re
import sys
import time
import typing
from failure import Failure
POLL_SECONDS = 2
TIMEOUT_SECONDS = 540
USAGE = "usage: wait-for <file> [<pattern>] [--timeout <seconds>]"
class Clock(typing.Protocol):
def monotonic(self) -> float: ...
def sleep(self, seconds: float) -> None: ...
def line_in(path: pathlib.Path, pattern: re.Pattern[str] | None) -> str | None:
"""The first line matching pattern, or without one the last line, where the file holds anything; else None."""
try:
lines = path.read_text(errors="replace").splitlines()
except FileNotFoundError:
return None
if pattern is None:
return lines[-1] if lines else None
return next((line for line in lines if pattern.search(line)), None)
def waited(file: str, pattern: str | None, timeout: float, clock: Clock = time) -> str | Failure:
path = pathlib.Path(file)
compiled = re.compile(pattern) if pattern is not None else None
deadline = clock.monotonic() + timeout
while (found := line_in(path, compiled)) is None:
left = deadline - clock.monotonic()
if left <= 0:
if not path.exists():
return Failure(f"{file} did not appear in {timeout:g} s")
last = line_in(path, None)
wanted = f"no line matching `{pattern}`" if pattern is not None else "nothing"
return Failure(f"{file} held {wanted} after {timeout:g} s" + (f"; its last line: {last}" if last is not None else ""))
clock.sleep(min(POLL_SECONDS, left))
return found
def arguments(argv: list[str]) -> tuple[str, str | None, float] | Failure:
operands = list(argv)
timeout = float(TIMEOUT_SECONDS)
if "--timeout" in operands:
at = operands.index("--timeout")
given = operands[at + 1:at + 2]
if not given or not re.fullmatch(r"[0-9]+(\.[0-9]+)?", given[0]) or float(given[0]) <= 0:
return Failure(f"give --timeout a number of seconds above 0; {USAGE}")
timeout = float(given[0])
del operands[at:at + 2]
if not 1 <= len(operands) <= 2:
return Failure(USAGE)
pattern = operands[1] if len(operands) == 2 else None
if pattern is not None:
try:
re.compile(pattern)
except re.error as error:
return Failure(f"cannot read the pattern `{pattern}`: {error}")
return operands[0], pattern, timeout
def main(argv: list[str]) -> int:
if argv[:1] in (["-h"], ["--help"]):
print(__doc__.strip())
return 0
parsed = arguments(argv)
done = parsed if isinstance(parsed, Failure) else waited(*parsed)
if isinstance(done, Failure):
print(f"wait-for: {done.reason}", file=sys.stderr)
return 1
print(done)
return 0
+76
View File
@@ -0,0 +1,76 @@
import pathlib
import subprocess
import sys
import tempfile
import threading
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from failure import Failure
from wait_for import main
TOOL = pathlib.Path(__file__).resolve().parents[1] / "wait-for"
class Clock:
"""A fake clock whose sleep advances time and runs the next of the steps given, one per sleep."""
def __init__(self, *steps):
self.now = 0.0
self.steps = list(steps)
def monotonic(self) -> float:
return self.now
def sleep(self, seconds: float):
self.now += seconds
if self.steps:
self.steps.pop(0)()
class WaitFor(unittest.TestCase):
def setUp(self):
self.file = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory()), "report.md")
def test_prints_the_first_line_matching_the_pattern_once_the_file_holds_it(self):
clock = Clock(lambda: self.file.write_text("running\n"), lambda: self.file.write_text("running\n== 3 passed in 1s ==\nFull log: x\n"))
self.assertEqual(main.waited(str(self.file), r"passed|failed", 60, clock), "== 3 passed in 1s ==")
self.assertEqual(clock.now, 2 * main.POLL_SECONDS)
def test_without_a_pattern_prints_the_last_line_once_the_file_holds_anything(self):
clock = Clock(lambda: self.file.write_text(""), lambda: self.file.write_text("# Report\nNo findings.\n"))
self.assertEqual(main.waited(str(self.file), None, 60, clock), "No findings.")
def test_a_file_already_holding_the_line_returns_at_once(self):
self.file.write_text("done\n")
clock = Clock()
self.assertEqual(main.waited(str(self.file), "done", 60, clock), "done")
self.assertEqual(clock.now, 0)
def test_gives_up_at_the_timeout_naming_the_file_and_its_last_line(self):
self.file.write_text("still running\n")
clock = Clock()
self.assertEqual(main.waited(str(self.file), "passed", 30, clock), Failure(f"{self.file} held no line matching `passed` after 30 s; its last line: still running"))
self.assertLessEqual(clock.now, 30)
def test_gives_up_on_a_file_that_never_appears(self):
self.assertEqual(main.waited(str(self.file), None, 5, Clock()), Failure(f"{self.file} did not appear in 5 s"))
def test_the_command_waits_for_a_file_another_process_writes(self):
writer = threading.Timer(0.5, lambda: self.file.write_text("== 1 passed in 0.1s ==\n"))
writer.start()
self.addCleanup(writer.cancel)
result = subprocess.run([sys.executable, str(TOOL), str(self.file), "passed", "--timeout", "20"], capture_output=True, text=True)
self.assertEqual((result.returncode, result.stdout, result.stderr), (0, "== 1 passed in 0.1s ==\n", ""))
def test_refuses_a_bad_pattern_timeout_or_argument_count_waiting_for_nothing(self):
for argv in ([], [str(self.file), "("], [str(self.file), "--timeout", "0"], [str(self.file), "--timeout", "x"], [str(self.file), "a", "b"]):
with self.subTest(argv=argv):
result = subprocess.run([sys.executable, str(TOOL), *argv], capture_output=True, text=True, timeout=10)
self.assertEqual((result.returncode, result.stdout), (1, ""))
self.assertIn("wait-for: ", result.stderr)
if __name__ == "__main__":
unittest.main()