Repository navigation
feat: leave the repo clean — delete merged heads at finish, sweep at close - #130
Conversation
There was a problem hiding this comment.
Reviewed as the reviewer role holder (radiusred-checky), diff first, then #129, #109 (M6-R8), #128 and its sweep log. Build, go vet, gofmt -l, go test ./... all clean; status executed against the hub with the dev build prints the note as documented. The shape of the design is right — branchAction is the correct pure core, ancestry-is-not-the-test is well reasoned and matches the 43/50 evidence, deleteHead is on both merge paths and can never mask a merge, slug() is genuinely shared with the --name task start passes, and TestSweepBranches really does exercise the union (task 1 reachable only through its PR head, task 2 only through the convention). Requesting changes on three blocking points and three smaller ones.
Blocking
1. The sweep runs before the DOC_MISSING gate, so on the documented path a refused close does the deleting and the successful close records nothing.
milestoneClose sweeps at milestone.go:167, then gatherRecords, then the hasDoc gate refuses. The quickstart teaches exactly this sequence: step 7 runs milestone close 1, expects refused[DOC_MISSING], and the operator then writes the document; step 8 runs it again and now claims "Any task branch still lying around is swept first ... the output says what went and what stayed". By step 8 there is nothing left — the branches went during step 7's refusal, candidates are skipped silently, swept is empty, and the closing comment carries no Swept N task branch(es). M6-R8 asks for the removal to be printed "as part of the record", and the record here is the closing comment on the milestone issue, not the scrollback of a command that exited nonzero. The plan's "idempotent: a refused close that already swept prints nothing the next time" reads as a property; it is actually the defect.
Nothing forces the current position — swept is only consumed when building comment. Move the sweep to just above that line (after the hasDoc gate). That fixes the record, makes step 8 true, stops a refused verb from performing an irreversible act, and stops burning the per-task API walk on every refused close (my live dry run over three tasks took 9s).
2. sweepBranches can delete unmerged work — two paths, both reproduced.
M6-R8's words are "never deleting unmerged work". Probes against the fake in this PR:
- An open PR loses its branch, order-dependent.
byHead[pr.HeadRef] = pris last-write-wins, so when one head carries two closing PRs (branch reused for a follow-up; a closed attempt plus a merged one) the decision depends on the orderclosedByPullRequestsReferenceshappens to return, which is not a documented ordering. WithClosingPRs→[open, merged]the branch is kept; with[merged, open]the same branch — open PR, 3 commits not on main — isdeleted (PR merged). The per-head facts must be aggregated, not overwritten: keep if any PR on that head is open. - Commits pushed after the merge.
{merged: true}deletes regardless ofahead, so a branch merged at task finish and then pushed to again is deleted at close, carrying 7 unmerged commits with it. The reasoning for ignoringaheadon merged branches is correct, but there is a precise guard available in the call you already make:gh pr view --json headRefOidis frozen at the merge for a merged PR, and onegh api repos/{r}/git/ref/heads/{b}gives the current tip. I verified both for PR #127 —headRefOidand the live ref are the same SHA, so the untouched case is a clean equality. Where they differ, keep and report ("merged, but N commit(s) pushed since"). At minimum the open-PR aggregation must land.
3. The linked-branch premise does not reproduce, and it is now in the permanent record.
Deviation 2 on #129, the PR body, and the comment on sweepBranches all state that GitHub drops the relation when the issue closes, evidenced by "LinkedBranches(#114) empty ... (#129) (open) lists its branch". I ran the same GraphQL myself, just now, under both this App's token and the operator's gh auth: linkedBranches is empty for #129 — open, task/129-leave-the-repo-clean-delete-merged-task alive on the remote, PR #130 attached to it. It is also empty for every issue I sampled (#72, #97, #98, #104, #106, #114, #128 here; #41, #45, #46, #47 on radiusred/www). #129's timeline shows the ConnectedEvent for PR #130 at 21:31:29 and the deviation posted at 21:32:49, so the likeliest true rule is that the relation is dropped once the branch has a PR — which would mean the relation leg is empty for every task the sweep can ever see, not just closed ones.
This does not break the sweep (the union is a superset and my live dry run over #114/#72/#129 produced exactly the right answers — task/114-...: deleted (PR merged) via the PR head, #72 silent, task/129-...: kept (open PR)). It does mean the stated rule, the code comment that encodes it, and the deviation that will be gathered into the M6 document are all wrong about why. Re-verify and correct the deviation and the comment; and decide deliberately whether LinkedBranches earns a per-task GraphQL call on the close path when it is empty by construction — if it stays, say what it is for.
Should fix
4. No default-branch guard. Nothing excludes the default branch from candidacy, and both routes reach it: a linked ref, or a merged PR whose head is main (a PR opened from main into a release branch that closes the task). Probed: candidate main, merged PR, ahead 0 → branch main: deleted (PR merged), and the API call is issued. GitHub will very likely refuse it, but "the platform saves us" is not the guard — if name == info.DefaultBranch { continue } is, and RepoInfo is already fetched inside BranchAhead.
5. No cross-repo guard. headRefName for a fork PR is the branch name in the fork, and both deleteHead and the sweep pass it to DeleteBranch(baseRepo, name). Usually a harmless 404 note; where the base repo happens to have a branch of that name, it deletes the wrong branch, and BranchAhead was measuring that wrong branch too. isCrossRepository comes back from the same gh pr view --json call (verified on #127) — skip cross-repository heads.
6. A cleanup failure can refuse a close that passed every gate. LinkedBranches, ClosingPRs and PRInfo errors return from sweepBranches and abort milestoneClose, while BranchAhead and DeleteBranch failures are tolerated — and deleteHead states the opposite principle explicitly ("a failure here is a note, never an error"). A transient GraphQL failure should not stand between a fully-satisfied milestone and its close. Report and carry on, as the delete path does.
Minor
- A branch whose PR was closed unmerged and which is zero ahead is deleted with the reason
no PR, nothing beyond the default branch. The deletion is safe; the sentence is false, and this feature's whole claim is that the output says what went and why. Distinguish the closed-PR case. - SPEC §6 says
milestone close"sweeps the tasks' linked branches" — after the correction in commit dfe99a4, candidates are a three-source union, and "linked branches" is the one source that yields nothing. The normative row should say what the code does. - Deviation 1 (no
PRInfodecode test) is honest, reasoned and captured, and I accept it — though "no seam without restructuringinternal/tracker/github.go" is a little generous to itself, since a package-levelvarindirection overgh.JSONis a one-line seam. Fine as a backlog item, not a finding.
Checked and clean
deleteHead on both merge paths, after the merge is reported, returning nothing — a deletion failure cannot mask a merge (TestDeleteHeadIsANoteOnFailure covers the three cases). BranchAhead on a branch behind main returns ahead 0, which is the safe answer, and its error path skips rather than deletes. tracker.Tracker gains four methods: tracker.GitHub and the new test fake are the only implementers in the tree, the fake embeds the interface, and everything builds. status's note is advisory and silently skipped when RepoInfo fails — executed live, it prints verbatim what the docs promise.
…the default branch (#129)
There was a problem hiding this comment.
Re-reviewed at 52b2c1d as radiusred-checky. Every round-1 finding is fixed, and I re-ran each of my probes against the new code rather than reading the diff for them. Approving. Two non-blocking items below — one is a real (if narrow) hole I would like closed, one is a test-coverage gap I proved by mutation.
The blocking findings, re-verified
1. Sweep position. sweepBranches now sits at milestone.go:191, after the DOC_MISSING refusal and immediately before the closing comment is built. A refused close no longer deletes anything, and the successful close is the one that prints and records. Quickstart step 8's "swept first, then the milestone issue closes" is now true as written, with no doc change needed — the code moved to meet the sentence, which is the right direction.
2a. Shared heads. prByHead ranks open > merged > closed. Probed both API orderings — [merged, open] and [open, merged] — on one head with 3 unmerged commits: kept (open PR) both times. Deletion is no longer a function of what order the GraphQL connection happened to return.
2b. Commits after the merge. Probed a merged PR (HeadSHA abc) whose branch tip moved to later with 7 commits ahead: kept (commits pushed after the PR merged). The headRefOid-vs-live-tip test is the right instrument — BranchAhead returning the tip alongside ahead_by costs one extra call and buys the guarantee M6-R8 actually asked for. Live check: PRInfo(#127).HeadSHA = c1154223… equals the ref's current SHA, and BranchAhead("task/129-…") returns ahead 5, tip 52b2c1d… — the git/ref/heads/<name> path handles the slash in a task/… name correctly.
3. The premise. The superseding Deviation on #129 states the corrected rule — the relation drops when a PR attaches, not when the issue closes — and credits where the reproduction came from. I can independently confirm the half that is still observable (empty for #129 with the branch alive and PR #130 attached, under both my token and the operator's); the "listed before PR #130 existed" half is inside a window that has closed, so I take it on the implementer's record, which is what a record is for. It is plausible against #129's timeline (ConnectedEvent for #130 at 21:31:29, the original observation minutes earlier), it is self-consistent, and — importantly — the sweep no longer depends on it being exactly right, since the relation is now the last-consulted, failure-tolerated leg of the union.
Should-fix items. Default branch excluded from candidates: probed via both remaining sources (a linked ref, and a merged PR whose head is main) — silent, no delete call issued. Fork heads excluded: probed a merged cross-repo PR with head develop against a base repo whose own develop carries 12 commits — silent, untouched; deleteHead is likewise silent for a fork. Sweep failures are notes: RepoInfo/ClosingPRs/PRInfo errors print note: branch sweep skipped … and the close proceeds, and the partial-PRInfo case correctly discards the whole task's PR set rather than deciding on a half-built byHead — that detail matters, and it is right.
Live dry run over #114, #72, #129 with DeleteBranch stubbed, new code: task/114-…: deleted (PR merged), task/129-…: kept (open PR), #72 silent. CI green on 52b2c1d (Go build and test, Lint commit messages). go build, go vet ./..., gofmt -l ., go test ./... all clean locally.
Non-blocking
A. tip == pr.HeadSHA is true when both are empty. With HeadSHA: "" and a tip of "", a merged PR's branch carrying 5 unmerged commits is deleted as PR merged — I reproduced it. I could not reach it through tracker.GitHub: BranchAhead errors (and the candidate is skipped) before it can return an empty tip, and headRefOid is always populated for a same-repo PR. So this is defence in depth, not a live defect — but it is the exact failure shape round 1 was about, and pr.HeadSHA != "" && tip == pr.HeadSHA closes it for the cost of one clause. Worth taking now rather than trusting a future PRInfo to keep the field non-empty.
B. The default-branch guard is the one new guard the tests do not exercise. I mutation-tested all four: dropping the headRefOid comparison, the fork exclusion, or the open-over-merged ranking each turns the suite red — good, those are genuinely covered. Deleting the n != defaults[task.Repo] clause outright leaves TestSweepBranches green, because nothing in the eleven-task fake ever offers main as a candidate: ahead["main"] = 0 is a decoy no source reaches, and task/10-main is a different string that is (correctly) swept. The "branch main:" and ft.deleted != "main" assertions therefore assert nothing today. One line — linked for some task including "main", or a PR head of "main" — makes them real, and SPEC §6 now promises this behaviour in normative prose.
C. Record tidy-ups before task finish. The PR body's "Executed (dev build, live)" block still carries the round-1 reading — LinkedBranches(#114) → empty "(closed — the relation is gone)" — which the bullet above it now supersedes; the "Tracker seams" bullet does not mention PR.HeadSHA, PR.CrossRepo, or BranchAhead's changed signature; and "all fixed in the fix commit" never got its SHA substituted (52b2c1d). The PR description is the task's summary artifact under SPEC §4, so it is worth a minute.
None of A–C blocks the merge. Approved.
There was a problem hiding this comment.
Re-reviewed at 51ef28c as radiusred-checky. Both code changes confirmed by execution, and I re-ran the mutation experiment that produced finding B rather than trusting the report of it. Approving — this supersedes my round-2 approval, which GitHub dismissed when the branch moved (reviewDecision is back to REVIEW_REQUIRED, so this review is also what re-satisfies the platform's own rule).
(A) Empty SHA no longer reads as "untouched". branchAction now requires pr.HeadSHA != "" before comparing. My exact round-2 probe — merged PR, no HeadSHA, tip "", 5 commits ahead — now returns kept (commits pushed after the PR merged) instead of deleting. I also probed the mirror case (SHA known, tip empty): likewise kept. The rule now fails closed in both directions, which is the property I wanted: an unknown merge SHA is treated as "I cannot prove this branch is spent", not as agreement.
(B) The default-branch guard is now pinned by a test. Task #12's merged PR with head main (tip mm) makes main a real candidate for the first time, so the "branch main:" and ft.deleted != "main" assertions finally have something to catch. I re-ran my mutation harness over a clean export of 51ef28c against all five guards, each deleted in isolation:
- drop the default-branch clause → red (was green in round 2 — the gap is closed)
- drop the
pr.HeadSHA != ""clause → red (new) - drop the tip-vs-
headRefOidcomparison → red - drop the fork exclusion in
prByHead→ red - drop the open-over-merged ranking → red
Every guard that stands between this verb and someone's unmerged work is now load-bearing in the suite. That is the right end state for an irreversible operation, and it is worth more than the line count suggests: a guard no test can kill is a guard the next refactor deletes silently.
(C) The record. The stale round-1 readings are gone, the tracker-seams list carries HeadSHA/CrossRepo and BranchAhead's new signature, and the SHA is substituted. Two counts in the "Tests" paragraph drifted with the last commit — the fake is twelve tasks now, not eleven, and it defines seven methods, not five. Not worth a commit on its own; fold it in if anything else touches the body before task finish.
Re-verified at this SHA: live dry sweep over #114, #72, #129 with DeleteBranch stubbed — task/114-…: deleted (PR merged), task/129-…: kept (open PR), #72 silent. go build, go vet ./..., gofmt -l ., go test ./... clean. CI green on 51ef28c (Go build and test; Lint commit messages).
One observation, explicitly not a request: on the no-SHA path the reason prints commits pushed after the PR merged, when what is actually known is that the merge commit is unknown. It is unreachable through tracker.GitHub — BranchAhead errors before it can hand back an empty tip, and headRefOid is always populated for a same-repo PR — so it costs nothing today and I would not spend a round on it.
Nothing outstanding. Approved.
Closes #129 — M6-R8 (adopts #128).
What
branchAction(pr, hasPR, ahead, tip)— the only grounds for deleting a task branch: its PR merged and the branch still sits at the PR's frozenheadRefOid(commits pushed after the merge are kept), or no PR is open and nothing beyond the default branch. Tip ancestry is never the test (rebase-merge rewrites commits; 43 of the 50 merged branches swept by hand on 2026-08-26 had non-ancestor tips). Several PRs on one head: open wins over merged over closed. Forks' heads and the default branch are never candidates.task finishdeletes the head branch after a successful merge on both paths; a deletion failure is a note (note: could not delete branch …) — the merge stands and is reported first.milestone closesweeps the tasks' branches after every gate has passed, immediately before closing the issue: one line per branch —deleted (PR merged),deleted (no PR, nothing beyond the default branch),deleted (PR closed unmerged, nothing beyond the default branch), orkept (…why…)— and the closing comment carries what was swept. Sweep failures are notes, never an abort.statusnotes when the repo'sdelete_branch_on_mergesetting is off (advisory; skipped if unreadable).PR.HeadRef,PR.HeadSHA(headRefOid, frozen at merge),PR.CrossRepo(isCrossRepository),PR.Merged;LinkedBranches(GraphQLlinkedBranches);BranchAhead(repo, branch) (ahead, tip, err)(compare API against the default branch, plus the live ref);DeleteBranch;RepoInfo.LinkedBranches(#129)listed its branch before PR feat: leave the repo clean — delete merged heads at finish, sweep at close #130 existed, empty after; checky's reproduction on feat: leave the repo clean — delete merged heads at finish, sweep at close #130). So candidates are the union of the relation, the head refs of the task's PRs, and the conventionaltask/<n>-<slug>nametask startused; candidates that no longer exist are skipped silently. Deviation on Leave the repo clean: delete merged task branches at finish and sweep at close #129.task finish,milestone close,status; quickstart steps 5 and 8; the introduction'sstatusline.Executed (dev build, live)
statusin the hub →note: radiusred/gh-codecrew does not delete branches on merge …. Live tracker calls:RepoInfo→{main false};LinkedBranches(#114),(#72)→ empty (PRs attached / branch gone),(#129)→ empty once PR #130 attached;BranchAheadof #114's merged branch → 4 ahead, tip = PR #127'sheadRefOid(c115422); of a deleted branch → error;PRInfo(#127)→head=task/114-…, merged=true, cross=false. Dry sweep over #114, #72, #129 with deletion stubbed:task/114-…: deleted (PR merged); #72 silent;task/129-…: kept (open PR). The real first uses aretask finish 129(its own head) andmilestone close 6.Tests
TestBranchAction(seven cases incl. merged-but-moved),TestPRByHeadOpenWins(both orderings; fork excluded),TestSweepBranches(twelve-task fake: merged, empty, unmerged work, open-over-merged on one head, closed-unmerged empty, gone, deletion refused, moved after merge, relation-only, default-branch candidate, fork),TestSweepBranchesFailuresAreNotes,TestDeleteHeadIsANoteOnFailure(incl. fork silent). The cli package gains its first tracker fake — an embedded-interface stub with the seven methods the sweep uses.go test ./...green.Not done
PRInfodecode test — Deviation on #129 (no seam without restructuring github.go; fields verified live).Review round 1 (checky)
Three blocking findings, all fixed in
52b2c1d: the sweep ran beforeDOC_MISSINGcould refuse (moved after the last gate — the successful close records it); two paths could delete unmerged work — last-write-wins on shared heads (now open > merged > closed) and merged-ignores-ahead (now guarded by the frozenheadRefOid); the premise was wrong — the relation drops when a PR attaches, not at close (record corrected by a superseding Deviation on #129). Should-fix items done: default-branch guard, fork guard (isCrossRepository), sweep errors as notes. Minor: closed-unmerged reason wording; SPEC row says "the tasks' branches" and names the sources.Round 2 (checky, approved) — three non-blocking items taken in
51ef28cEmpty
HeadSHAcan no longer read as "tip untouched" (pr.HeadSHA != ""guard,TestBranchActioncase); the default-branch guard is now reachable inTestSweepBranchesthrough a merged PR whose head ismain— dropping the guard turns the suite red (mutation-checked); this body's stale round-1 readings replaced.🤖 Generated with Claude Code