Skip to content

milestone close sweeps stale task branches from earlier closes #273

Closed

Description

@davison

Goal

M14-R4 in full; same file family as #271 (branches.go), land after it. sweepBranches gains a second pass over the repo's task/- branches: a branch whose task issue is closed and which is merged into or empty against the default branch is deleted and reported in the closing comment; a branch with unmerged commits, or whose task is open, is named and left; --dry-run lists them beside the milestone's own. SPEC §6 milestone close row. Closes #167 with the PR.

Requirements

M14-R4

Plan

The shape. milestone close gains a second branch pass, run straight after the milestone's own sweep and before the close itself, on the same "only ever deletes a merged or empty branch, so a close that fails after it loses nothing" footing (#133).

Repos swept. The hub (where the milestone issue lives) plus every repo the milestone's tasks name, deduped, hub first. A stale branch accumulates wherever tasks are cut, and the hub is the one repo every close can be certain about.

The candidate set. One listing per repo, prefix-filtered at the server: a new Tracker.TaskBranches(repo) reading GraphQL refs(refPrefix: "refs/heads/task/", first: 100), so a repo's other branches never enter the sweep and the ceiling is 100 task branches rather than 100 branches. Names come back with the prefix stripped, so the implementation restores it. The task number is read off task/<n>-<slug>; a name that carries no number is skipped. Skipped too: the default branch, and every branch the milestone's own pass already considered (keyed by repo and name, and by the milestone's own task refs) — so pass one's verdicts are never restated or overridden.

The delete conditions do not change. Each candidate is judged by the same branchAction the milestone's own branches meet: its PR merged and the tip still at the merged commit, or no open PR and nothing beyond the default branch. Anything else is named and left, with the reason. This is what #167 asked for ("the change is the candidate set"), and it is the only test that catches a rebase-merged branch, whose commits are rewritten and so are never ahead == 0.

Bounded API use. One branch listing and one RepoInfo per repo; per candidate, one issue read (Task) — and a candidate whose task is open costs exactly that and stops there. Only a candidate whose task is closed goes on to ClosingPRs + PRInfo + BranchAhead, the lookups the delete conditions need. A branch that vanished between the listing and the check is skipped silently, as pass one does.

Reporting. The live verb prints the stale group under its own heading, each line reading stale branch <name>: deleted (<reason>) / kept (<reason>). The closing comment gains its own sentence, Swept from earlier closes: a, b., beside the existing Swept N task branch(es): …. --dry-run lists them beside the milestone's own as would delete stale branch … / would keep stale branch …, and writes nothing.

Files. internal/tracker/tracker.go (interface), internal/tracker/github.go (TaskBranches), internal/cli/branches.go (taskNumber, planStaleSweep, sweepItem.Stale, executeSweep's prefix), internal/cli/milestone.go (the second pass, the dry-run lines, the closing comment), SPEC.md §6's milestone close row, CHANGELOG.md.

Tests (internal/cli/branches_test.go, internal/cli/dryrun_test.go, fakes extended with TaskBranches and a closed/open Task): a stale merged branch of a closed task is swept and reported; an empty one is swept; one with unmerged commits is kept and named; one whose task is still open is kept and named after a single issue read; the milestone's own branches are untouched by the second pass; a listing failure is a note, not an abort; the dry run names them and writes nothing.

Ask-the-human points

None.

Activity

  1. added
    cc:task
    on Sep 6, 2026
  2. radiusred-cody commented on Sep 6, 2026

    @radiusred-cody
    Contributor

    Started by @radiusred-cody[bot].

  3. radiusred-cody commented on Sep 6, 2026

    @radiusred-cody
    Contributor

    Decision: the second pass lists the hub the milestone issue lives in, plus every repo the milestone's tasks name, deduped and hub first.

    Trade-off: the brief left the choice open between "the hub and each spoke a task of this milestone lives in" and "just the repos the milestone's tasks name". The two differ only for a milestone whose tasks are all in spokes, where the second would never sweep the hub. The hub is where every milestone issue lives and where the record is read, so it is the one repo a close can always be certain about, and it is the repo a solo project's stale branches accumulate in — #167's own evidence is radiusred/numberguess, a solo hub. One extra prefix-filtered listing is the whole cost.

    Rejected: every repo in the org or in .codecrew/config.yml's spoke list — a close would then reach repos this milestone never touched, and the sweep would stop being something the closing operator can predict from the milestone in front of them.

  4. radiusred-cody commented on Sep 6, 2026

    @radiusred-cody
    Contributor

    Decision: the delete conditions are unchanged — every stale candidate meets the same branchAction the milestone's own branches meet — so a candidate whose task is closed costs ClosingPRs + PRInfo + BranchAhead on top of the one issue read.

    Trade-off: the cheaper test the bound suggests, "one branch listing per repo, one issue read per candidate", would have to be ahead == 0 alone. That misses the merged case entirely: this project rebase-merges, so a merged branch's commits are rewritten and it is never ahead == 0 — only "its PR merged and the tip still at the merged commit" catches it, and that needs the PR. #167 asked for exactly this ("the delete conditions do not change... the change is the candidate set"), and reusing branchAction and prByHead means the two passes can never drift apart. The bound is kept where it matters: the listing is one prefix-filtered GraphQL call per repo, and a candidate whose task is still open stops after its single issue read.

    Rejected: reading the PRs out of the branch listing itself via Ref.associatedPullRequests, which would have made the whole pass one call per repo. It would duplicate PRInfo's shape inside the listing and give the Tracker seam a second, richer PR type for one caller's benefit.

  5. radiusred-cody commented on Sep 6, 2026

    @radiusred-cody
    Contributor

    Decision: a task/<n>-… branch is a candidate on its name alone; the sweep does not require issue <n> to carry cc:task, and it names kept branches in the verb's output and the dry run but not in the closing comment.

    Trade-off: the name is what task start writes and what the first pass already reconstructs from the convention, so requiring the label would make the sweep miss a branch whose task was relabelled while gaining nothing — the delete conditions still refuse anything with unmerged work or an open PR, so the worst case for a mislabelled candidate is an empty branch removed. An issue number that resolves to nothing at all is a note: and a skip, never a delete. Naming kept branches in the output only mirrors what the milestone's own pass does; putting every kept stale branch in the closing comment would grow the comment without end on a repo that has one branch nobody will ever sweep.

  6. radiusred-cody commented on Sep 6, 2026

    @radiusred-cody
    Contributor

    Decision: the second pass will not delete a branch that is the head of any open pull request, checked with one OpenPRsForBranch lookup made only for a candidate about to go on the grounds that no PR is open at all.

    Trade-off: branchAction's "no PR, nothing beyond the default branch" arm is only as good as the PR set it is handed, and ClosingPRs finds the PRs that close <n> — so a PR opened on a task branch without a Closes #<n> line is invisible to it, and deleting the branch would close that PR. Pass one's candidates come from the task's own PRs and one conventional name, so its exposure is narrow; pass two makes every task/<n>-… branch in the hub and in each task's repo a candidate, which is wider than the arm was written for (checky's finding 2 on PR #293, taken as required by the coordination layer). The guard costs at most one extra call per candidate, and none at all for a branch already known to have a PR or already being kept. It fails closed: a lookup that errors is a note: and the branch stands.

    Rejected: widening the PR set for every candidate, which would cost the lookup on branches that are being kept anyway; and leaving it to the Decision text, which was the reviewer's non-blocking suggestion — a guard that only exists in prose is the shape finding 1 was about.

  7. radiusred-cody commented on Sep 6, 2026

    @radiusred-cody
    Contributor

    Decision: Tracker.TaskBranches returns a truncated flag read from the listing's pageInfo.hasNextPage, and the sweep emits a note: naming the repo rather than paging.

    Trade-off: one page is a hundred task branches, which no repo in this project approaches, and successive closes do make progress through a longer list — so paging would buy correctness nobody can currently reach, at the cost of an unbounded walk in a verb whose whole design claim is a bounded one. The flag costs one GraphQL field and turns a silent partial sweep into a reported one, which is the property the operator actually needs (checky's finding 4 on PR #293).

  8. 1 remaining item

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    cc:task

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions