Add the project setting merge = "human", under which chunk-pr merge leaves the pull request ready and a do-epic chunk parks on your merge

This commit is contained in:
Lilleman
2026-11-19 15:51:44 +01:00
parent 017cf33611
commit e2f5661760
15 changed files with 240 additions and 58 deletions
+4 -2
View File
@@ -18,8 +18,10 @@ DRIVER = DO_EPIC
def shipping(config: ProjectConfig) -> str:
"""How a chunk in the repo that config names ships its change. An epic's issue isn't an item of the repo's plan, so `chunk-pr merge --finish`, which adds a finished item's changelog bullet, doesn't apply: the worker writes the bullet itself, where the repo publishes something others consume (docs/decisions.md → `do-epic: drives one Jira epic …`, "Releases")."""
"""How a chunk in the repo that config names ships its change. An epic's issue isn't an item of the repo's plan, so `chunk-pr merge --finish`, which adds a finished item's changelog bullet, doesn't apply: the worker writes the bullet itself, where the repo publishes something others consume. Where the repo's config leaves the merge to a person, the worker is told that its run ends with the pull request ready (docs/decisions.md → `do-epic: drives one Jira epic …`, "Releases")."""
opening = "The issue isn't an item of the repo's plan, so run `chunk-pr merge` without `--finish`."
if config.merge == "human":
opening += " The repo's team merges its pull requests itself, so `chunk-pr merge` leaves the pull request ready for a person to merge: end your run when it says so."
if config.epic:
return f"{opening} The repo's plan is a Jira epic, and the history of its issues serves as the changelog, so leave the changelog as it is."
changelog = config.paths["changelog"].relative_to(config.top) if "changelog" in config.paths and config.paths["changelog"].is_relative_to(config.top) else config.places["changelog"]
@@ -53,7 +55,7 @@ NAMES = prompts.RecordNames(
WORKER = prompts.worker_prompts(DRIVER, fresh, "", NAMES)
# What a worker does with a reviewer's feedback, as the decision asks (docs/decisions.md → `do-epic: drives one Jira epic …`, "Long gates").
FEEDBACK = 'Where a reviewer left feedback, fix what is reasonable and push the fix, answer each other comment in its thread, and change the setup so our review would have caught the finding (~/.claude/CLAUDE.md → "Working loop", "Change the setup for each finding an outside reviewer raises on a PR, deterministic tools first"). Then merge the pull request as before.'
FEEDBACK = 'Where a reviewer left feedback, fix what is reasonable and push the fix, answer each other comment in its thread, and change the setup so our review would have caught the finding (~/.claude/CLAUDE.md → "Working loop", "Change the setup for each finding an outside reviewer raises on a PR, deterministic tools first"). Then merge the pull request as before. Where the pull request is merged, run `chunk-pr clean` in its worktree and finish the chunk.'
def unparked(news: str) -> WorkerPrompts:
+8 -5
View File
@@ -1,6 +1,6 @@
"""What the pull request of a do-epic chunk waits on, read once from its forge (docs/decisions.md → `do-epic: drives one Jira epic …`, "Long gates").
A gate is something only time or a person passes: checks that still run, an approval a reviewer has yet to give, or the new review of a reviewer whose requested changes the worker answered. While a chunk's pull request waits on one, its worker doesn't have anything to do, so do-epic parks the chunk (`parked`). Anything else is news the worker acts on."""
A gate is something only time or a person passes: checks that still run, an approval a reviewer has yet to give, the new review of a reviewer whose requested changes the worker answered, or the merge itself, where the project's config leaves it to a person. While a chunk's pull request waits on one, its worker doesn't have anything to do, so do-epic parks the chunk (`parked`). Anything else is news the worker acts on."""
import dataclasses
import typing
@@ -13,6 +13,7 @@ from forge.commands import forge_of, Run
CHECKS = "its checks"
APPROVAL = "an approval"
NEW_REVIEW = "a new review"
MERGE = "your merge"
@dataclasses.dataclass(frozen=True)
@@ -22,7 +23,7 @@ class Gate:
worktree: str
# The pull request's number.
pull: str
# CHECKS, APPROVAL or NEW_REVIEW.
# CHECKS, APPROVAL, NEW_REVIEW or MERGE.
on: str
@@ -33,10 +34,10 @@ class Open:
news: str
def gate(branch: str, checkout: str, at: typing.Callable[[str], Run]) -> Gate | Open | Failure:
"""What the chunk on branch waits on, in a worktree of checkout; at gives the runner of commands in a folder. A Failure where git or the forge cannot be read, which doesn't say anything about the gate.
def gate(branch: str, checkout: str, at: typing.Callable[[str], Run], by_human: bool = False) -> Gate | Open | Failure:
"""What the chunk on branch waits on, in a worktree of checkout; at gives the runner of commands in a folder, and by_human says the project's config leaves the merge to a person (`merge = "human"`). A Failure where git or the forge cannot be read, which doesn't say anything about the gate.
A draft never waits on a gate: its worker still works on it. On Gitea only the checks are a gate, since chunk-pr reads approvals on GitHub only (`chunk_pr.approval.awaited`). Changes a reviewer requested are news until the worker answered them (`forge.reviews.answered`); from then on the pull request awaits a new review.
A draft never waits on a gate: its worker still works on it. On Gitea only the checks are a gate, since chunk-pr reads approvals on GitHub only (`chunk_pr.approval.awaited`). Changes a reviewer requested are news until the worker answered them (`forge.reviews.answered`); from then on the pull request awaits a new review. Where a person merges, a pull request with nothing else to wait on awaits that merge, and the chunk has news again once it is merged, when it is no longer open.
"""
worktree = worktree_of(branch, at(checkout))
if not isinstance(worktree, str):
@@ -71,6 +72,8 @@ def gate(branch: str, checkout: str, at: typing.Callable[[str], Run]) -> Gate |
return Open(f"a reviewer's comment on pull request #{pull} waits for an answer, in {', '.join(waiting)}")
if decision in (ANSWERED, REVIEW_REQUIRED):
return Gate(worktree, pull, NEW_REVIEW if decision == ANSWERED else APPROVAL)
if by_human:
return Gate(worktree, pull, MERGE)
return Open(f"pull request #{pull} passed its checks" + (" and is approved" if decision == APPROVED else ""))
+63 -32
View File
@@ -56,7 +56,8 @@ A repo takes as many chunks as its `parallel-chunks`, and none while loop-claude
A chunk whose worker ends its run while its pull request waits on its checks, an approval or a new review
is parked: it frees its place for the next chunk, and its window's name says what it awaits. Its worker goes
on once the checks pass, the approval comes or a reviewer comments, and a place is free; until one is, the
name says `awaits a slot`. A parked chunk survives a reboot: run do-epic <EPIC-KEY> again to go on polling.
name says `awaits a slot`. In a repo whose config sets `merge = "human"`, a chunk never merges: its pull
request, ready and with nothing else to wait on, `awaits your merge`, and its worker goes on once you merged it. A parked chunk survives a reboot: run do-epic <EPIC-KEY> again to go on polling.
--status doesn't start any chunk. It prints a line per open issue: that its chunk runs or asks you a question, or why it waits, such as the gate a parked chunk awaits and since when.
{findings.HELP}
{finish.HELP}
@@ -290,46 +291,76 @@ def parked_through(repo: str, loop: Loop, epic: EpicRun, starts: bool) -> str |
recorded_as = parked.parked_chunks(folder)
if isinstance(recorded_as, Failure):
return Stop(1, recorded_as.reason)
if starts and key not in recorded_as:
config = config_located(loop.root, epic.paths.home)
if isinstance(config, Failure):
return Stop(1, config.reason)
poll = Poll(key, repo, config.merge == "human", folder, loop, epic)
if key in recorded_as:
return resumed_news(poll, recorded_as[key].on, shown=None)
entered = None if starts else poll.gate()
if not isinstance(entered, gates.Gate):
if isinstance(entered, Failure):
say(f"cannot tell whether the chunk's pull request waits on a gate, so the chunk isn't parked: {entered.reason}", epic.effects, stdout=True)
return ""
# What the chunk is parked on; None while it isn't parked.
on = recorded_as[key].on if key in recorded_as else None
# What the window's name says the chunk awaits, and the last reason said for staying parked.
shown = said = None
if failed := poll.record(entered):
return Stop(1, failed.reason)
tmux.park(loop.window, entered.on, epic.effects.tmux)
# Once the window shows as parked, so the chunks that start count it as running nowhere.
issues = epic.store.read()
started = issues if isinstance(issues, Failure) else chunks_started(epic, issues, key)
say(f"could not start the next chunk: {started.reason}" if isinstance(started, Failure) else f"started {', '.join(started.started) or 'no other'}", epic.effects, stdout=True)
epic.effects.sleep(POLL_SECONDS)
return resumed_news(poll, entered.on, shown=entered.on)
@dataclasses.dataclass(frozen=True)
class Poll:
"""What each read of a parked chunk's gate needs: the chunk's key, the repo its work is in, whether a person merges its pull request, the folder of the epic's parked chunks, and the chunk's loop and epic."""
key: str
repo: str
by_human: bool
folder: pathlib.Path
loop: Loop
epic: EpicRun
def gate(self) -> gates.Gate | gates.Open | Failure:
return gates.gate(self.key, self.loop.root, self.epic.forge_at, self.by_human)
def record(self, gate: gates.Gate) -> Failure | None:
"""Writes the chunk's file as parked on gate, and says so."""
failed = parked.park(self.folder, parked.Parked(self.key, gate.worktree, gate.pull, str(self.loop.session.id), gate.on))
if failed is None:
say(f"parked: pull request #{gate.pull} awaits {gate.on}", self.epic.effects, stdout=True)
return failed
def resumed_news(poll: Poll, on: str, shown: str | None) -> str | Stop:
"""Reads the gate of the parked chunk every POLL_SECONDS until its worker has something to do and a running slot is free, then removes its file: what to tell that worker. on is the gate the chunk's file says it is parked on, and shown what its window's name says it awaits, None where nothing yet. A gate that cannot be read leaves the chunk parked as it was."""
# The last reason said for staying parked.
said = None
while True:
now = gates.gate(key, loop.root, epic.forge_at)
if on is None and not isinstance(now, gates.Gate):
if isinstance(now, Failure):
say(f"cannot tell whether the chunk's pull request waits on a gate, so the chunk isn't parked: {now.reason}", epic.effects, stdout=True)
return ""
stays = None
if isinstance(now, gates.Gate) and now.on != on:
failed = parked.park(folder, parked.Parked(key, now.worktree, now.pull, str(loop.session.id), now.on))
if failed is not None:
return Stop(1, failed.reason)
say(f"parked: pull request #{now.pull} awaits {now.on}", epic.effects, stdout=True)
now = poll.gate()
if isinstance(now, gates.Gate):
awaits, stays = now.on, None
if now.on != on:
if failed := poll.record(now):
return Stop(1, failed.reason)
on = now.on
elif isinstance(now, Failure):
stays = f"cannot read the gate: {now.reason}"
elif isinstance(now, gates.Open):
full = resumed(repo, loop, epic)
awaits, stays = shown or on, f"cannot read the gate: {now.reason}"
else:
full = resumed(poll.repo, poll.loop, poll.epic)
if full is None:
failed = parked.unpark(folder, key)
failed = parked.unpark(poll.folder, poll.key)
return Stop(1, failed.reason) if failed is not None else f"{DRIVER_PARKED} its pull request awaited {on}. It resumes now: {now.news}."
stays = f"{now.news}, but the chunk stays parked: {full}"
awaits = parked.SLOT if isinstance(now, gates.Open) else now.on if isinstance(now, gates.Gate) else shown or on
awaits, stays = parked.SLOT, f"{now.news}, but the chunk stays parked: {full}"
if awaits != shown:
tmux.park(loop.window, str(awaits), epic.effects.tmux)
tmux.park(poll.loop.window, awaits, poll.epic.effects.tmux)
shown = awaits
if on is None:
# Once the window shows as parked, so the chunks that start count it as running nowhere.
issues = epic.store.read()
started = issues if isinstance(issues, Failure) else chunks_started(epic, issues, key)
say(f"could not start the next chunk: {started.reason}" if isinstance(started, Failure) else f"started {', '.join(started.started) or 'no other'}", epic.effects, stdout=True)
on = now.on if isinstance(now, gates.Gate) else on
if stays is not None and stays != said:
say(f"{stays}; trying again every {POLL_SECONDS} seconds", epic.effects, stdout=True)
say(f"{stays}; trying again every {POLL_SECONDS} seconds", poll.epic.effects, stdout=True)
said = stays
epic.effects.sleep(POLL_SECONDS)
poll.epic.effects.sleep(POLL_SECONDS)
# How the news for a chunk that was parked opens.
+1 -1
View File
@@ -24,7 +24,7 @@ class Parked:
pull: str
# The ID of its worker's claude session, which `claude --resume` takes.
session: str
# What its pull request waits on, as the words after "awaits" (`gates.CHECKS`, `gates.APPROVAL`, `gates.NEW_REVIEW`).
# What its pull request waits on, as the words after "awaits" (`gates.CHECKS`, `gates.APPROVAL`, `gates.NEW_REVIEW`, `gates.MERGE`).
on: str
def waits(self, shown: str) -> str:
+16 -3
View File
@@ -5,7 +5,7 @@ import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from do_epic.gates import APPROVAL, CHECKS, gate, Gate, NEW_REVIEW, Open
from do_epic.gates import APPROVAL, CHECKS, gate, Gate, MERGE, NEW_REVIEW, Open
from failure import Failure
WORKTREES = "worktree /code/billing\nHEAD aaa\nbranch refs/heads/main\n\nworktree /code/billing-ABC-21\nHEAD abc\nbranch refs/heads/ABC-21\n"
@@ -58,8 +58,8 @@ class Forge:
return Failure(f"{key} failed") if self.fails and key.startswith(self.fails) else self.answers[key]
return run
def gate(self) -> Gate | Open | Failure:
return gate("ABC-21", "/code/billing", self.at)
def gate(self, by_human: bool = False) -> Gate | Open | Failure:
return gate("ABC-21", "/code/billing", self.at, by_human)
class GateOfAChunk(unittest.TestCase):
@@ -72,6 +72,19 @@ class GateOfAChunk(unittest.TestCase):
self.assertIn(["gh", "pr", "checks", "7", "--json", "bucket"], forge.commands)
self.assertTrue(any(command[:3] == ["gh", "api", "graphql"] and "number=7" in command for command in forge.commands), forge.commands)
def test_where_a_person_merges_a_pull_request_with_nothing_else_to_wait_on_awaits_that_merge(self):
gitea = "https://gitea.larvit.se/o/billing.git"
for forge in (Forge(decision="APPROVED"), Forge(decision=""), Forge(gitea)):
with self.subTest(forge.answers["git remote"]):
self.assertEqual(forge.gate(by_human=True), Gate("/code/billing-ABC-21", "7", MERGE))
self.assertEqual(MERGE, "your merge")
def test_where_a_person_merges_every_other_gate_and_all_news_stay_as_they_are(self):
same = (Forge(), Forge(buckets=("pending",)), Forge(draft=True), Forge(decision="CHANGES_REQUESTED"), Forge(threads=(thread(False, "reviewer"),)), Forge(buckets=("fail",)), Forge(pulls=""))
for forge in same:
with self.subTest(forge.answers):
self.assertEqual(forge.gate(by_human=True), forge.gate())
def test_a_review_decision_gh_prints_in_no_known_shape_is_a_failure(self):
forge = Forge()
forge.answers["gh pr view"] = "[1]"
+21 -1
View File
@@ -655,7 +655,7 @@ def awaiting(**answers: object) -> Forge:
class AChunkWhosePullRequestWaitsOnAGate(DoEpicTest):
def setUp(self) -> None:
super().setUp()
self.repo_chunk()
self.repo_config = self.repo_chunk() / "config.toml"
self.issues.append(issue("ABC-22", "do-epic-claude"))
self.file = parked.file_of(self.home / ".local/state/do-epic/ABC-12", "ABC-20")
self.session = main.session_named("A ABC-12 do-epic ABC-20", paths_from(self.home).claude_code_transcripts)
@@ -684,6 +684,26 @@ class AChunkWhosePullRequestWaitsOnAGate(DoEpicTest):
shown = [args for args in fake.tmux_calls if args[0] == "rename-window" or tmux.ROOT in args and "%1" in args]
self.assertEqual(shown, [["rename-window", "-t", "%1", "ABC-20 awaits an approval"], ["rename-window", "-t", "%1", "ABC-20"], ["set-option", "-w", "-t", "%1", tmux.ROOT, checkout]])
def test_where_a_person_merges_it_parks_on_that_merge_and_works_again_once_merged(self) -> None:
self.repo_config.write_text(self.repo_config.read_text(encoding="utf-8") + 'merge = "human"\n', encoding="utf-8")
self.forges = [awaiting(), awaiting(decision="APPROVED"), awaiting(pulls="")]
fake = FakeEffects(streams=[self.worker(), self.worker()], claude=[judged("unfinished"), judged("finished")], git=UNREAD, windows=PARKED_WINDOW)
code, out, err, prompts = self.parked_run(fake)
self.assertEqual(code, 0, err)
self.assertIn("parked: pull request #7 awaits an approval\nstarted ABC-22\nparked: pull request #7 awaits your merge\n", out)
self.assertIn("leaves the pull request ready for a person to merge", prompts[0])
self.assertTrue(prompts[1].startswith(f"do-epic parked the chunk while its pull request awaited your merge. It resumes now: no pull request from origin's ABC-20 is open at abcabcabcabc; it may be merged or closed already. {chunk_prompt.FEEDBACK}\n\n"), prompts[1])
self.assertIn("Where the pull request is merged, run `chunk-pr clean`", chunk_prompt.FEEDBACK)
self.assertEqual([args[3] for args in fake.tmux_calls if args[0] == "rename-window"], ["ABC-20 awaits an approval", "ABC-20 awaits your merge", "ABC-20"])
self.assertFalse(self.file.exists())
def test_where_the_chunk_merges_an_approved_pull_request_parks_no_chunk(self) -> None:
self.forges = [awaiting(decision="APPROVED")]
fake = FakeEffects(streams=[self.worker()], claude=[judged("finished")], git=UNREAD, windows=PARKED_WINDOW)
code, out, err, prompts = self.parked_run(fake)
self.assertEqual(code, 0, err)
self.assertNotIn("parked: ", out)
def test_an_unfinished_chunk_parks_too_and_a_reviewers_comment_resumes_it(self) -> None:
self.forges = [awaiting(buckets=("pending",)), awaiting(), awaiting(threads=({"isResolved": False, "path": "a.py", "comments": {"nodes": [{"author": {"login": "reviewer"}}]}},))]
fake = FakeEffects(streams=[self.worker(), self.worker()], claude=[judged("unfinished"), judged("finished")], git=UNREAD, windows=PARKED_WINDOW + "ABC-22\t1\t@10\n")