diff --git a/changelog.d/fixed-17439-merge-train-same-issue-gate.md b/changelog.d/fixed-17439-merge-train-same-issue-gate.md new file mode 100644 index 0000000000..7391997b45 --- /dev/null +++ b/changelog.d/fixed-17439-merge-train-same-issue-gate.md @@ -0,0 +1 @@ +- The merge train's one-PR-per-issue and same-issue claim-conflict holds now judge only heads at the merge gate ([#17439](https://github.com/hivecommons/hive/issues/17439)). A PR whose CI is still running or whose review is outstanding is evaluated as usual, so its tracker row reads CI / Review instead of "needs human decision · Let hive decide", and it is held at the merge point only if it gets there. The first same-issue PR at the merge point keeps the issue and that pick stands until it closes or is blocked for good, so a check going red mid-run no longer flips the winner and two PRs no longer wear each other's number as `duplicate-of`. diff --git a/src/docs/dashboard-sections/activity-monitor.md b/src/docs/dashboard-sections/activity-monitor.md index e912a6f266..3ce88077a7 100644 --- a/src/docs/dashboard-sections/activity-monitor.md +++ b/src/docs/dashboard-sections/activity-monitor.md @@ -289,7 +289,7 @@ The tooltip lists the PRs ahead and the overlapping files. Waiting siblings are **Queue position, countdown, stall** ([#15395](https://github.com/hivecommons/hive/issues/15395)). The queued chip is its place in the queue and nothing else — **Queued for merge · 2nd of 6 · ~4m** — so it reads the same whether or not a merge sweep is evaluating it: the sweep is the spinning ⬡ glyph and a *Checking now…* line in the hover, never a change of words (one label per chevron), and no state slug (`queued-behind`, `front`) ever reaches the page. The hover is the place sentence — *2nd of 6 in the merge queue · behind #15253 (soaking, 12m left) · ETA ~25m (2 ahead · head soaking 12m · next pass in 40s). Hive merges one at a time, in order.*; the head's reads *1st of 6 in the merge queue · merges next · ETA ~13m.* — or, when the head is on a person or on CI, the blocker instead of a clock: *… behind #15253 (waiting for a maintainer (untrusted author)) · no estimate until #15253 moves.* Every train state — the 1st included — lights the **Merge queue** column, whatever numeric stage the lane holds the head at (the train's facts are mirrored onto a head still at 050/060; hive's own #15400 once lit CI / Review beside its 2nd-place sibling) — a head under repair is no member (below) and lights CI / Review. The server adds to each member's `merge_train`: `length`, `head_number`, `head_state` (`needs-human` / `ci` / `soaking` / `ready` / `merging` / `fixing` / `unknown`), `head_reason` (plain words), `head_since`, `eta_s`, `eta_at`, `eta_basis`, `late`, `stalled`, `stalled_s`. -**One PR per issue** ([#15395](https://github.com/hivecommons/hive/issues/15395)). Two open PRs declaring the same `Fixes #N` never both ride the queue (hive's own #15402 and #15407, both for #15396, once sat side by side as "waiting its turn"). Before the trains are grouped the sweep keeps one PR per closing issue — a green head (not conflicting, not blocked on a red required check) beats a red one; among equals the earlier-opened wins — and holds the other out of every train with the stage reason `merge-train:duplicate-of#:/0:0/0:`. The held row's gate reads `duplicate-of:` (`merge_gate.duplicate_of`, `merge_gate.duplicate_issue`), its chip **Duplicate of #15402 · needs a pick** with no clock, it carries no `merge_train`, and it wears `needs-decision` so both PRs sit under their issue with the hold visible. *Let hive decide* on either row takes the existing supersede path (rule `one-pr-per-issue`): on the held PR it closes it as superseded by the kept one with a credit comment, through the same write gate as every delegate close; on the kept PR it releases it and names the duplicate the queue holds (or, if the kept PR has since gone red, only recommends the fix — the queue re-picks on its next pass, so an earlier red PR yields to a later green one). +**One PR per issue** ([#15395](https://github.com/hivecommons/hive/issues/15395)). Two open PRs declaring the same `Fixes #N` never both ride the queue (hive's own #15402 and #15407, both for #15396, once sat side by side as "waiting its turn"). Before the trains are grouped the sweep keeps one PR per closing issue, judging heads at the merge gate only ([#17439](https://github.com/hivecommons/hive/issues/17439)): a same-issue PR whose CI is still running or whose review is outstanding carries no hold and no `needs-decision` — its row reads CI / Review like any other — and is held at the merge point if it gets there behind the issue's kept PR. The first PR at the merge point keeps the issue; among several already at the gate a green head (not conflicting) beats a conflicting one and then the earlier-opened wins; the pick then stands until the kept PR closes or is blocked for good, so a check that goes red mid-run never flips it. The other PR, once at the gate, is held out of every train with the stage reason `merge-train:duplicate-of#:/0:0/0:`. The held row's gate reads `duplicate-of:` (`merge_gate.duplicate_of`, `merge_gate.duplicate_issue`), its chip **Duplicate of #15402 · needs a pick** with no clock, it carries no `merge_train`, and it wears `needs-decision` so both PRs sit under their issue with the hold visible. *Let hive decide* on either row takes the existing supersede path (rule `one-pr-per-issue`): on the held PR it closes it as superseded by the kept one with a credit comment, through the same write gate as every delegate close; on the kept PR it releases it and names the duplicate the queue holds (or, if the kept PR has since gone red, only recommends the fix — the queue keeps its pick until the kept PR closes or is blocked for good). - **Countdown to auto-merge.** `eta_s` is the member's own remaining soak, plus — for each PR ahead — its remaining soak or, with no clock of its own, one sweep (the lanes' floor cadence), plus the wait for the next sweep tick; `eta_basis` is the short reading ("2 ahead · head soaking 12m · next pass in 40s"). The chip's `· ~4m` counts down in place from `eta_at` on the same tick as the soak clock, and `eta_at` is anchored server-side to the member's first estimate at its position (re-anchored only when a fresh estimate runs more than a sweep longer), so a poll re-render never resets it. Past the estimate it reads *any moment now*; past twice the first estimate `late` is set and the chip reads *running late* with the estimate kept in the hover. Always a `~`: an estimate, not a promise. A head on a person, on CI or being fixed — or a member that is itself a person's — carries no estimate and the blocker instead. - **Merge queue popover.** The sticky bar's **Merge queue N** chip (led by the merge-sign icon) (hidden with no train) opens a popover listing every train in merge order — ordinal, #, title, state in words, ETA — with the head's blocker on the front row; same pattern as the **Merged** popover (click outside / Escape closes). diff --git a/src/docs/hive-merge.md b/src/docs/hive-merge.md index 61f733ea56..9520943bbc 100644 --- a/src/docs/hive-merge.md +++ b/src/docs/hive-merge.md @@ -1293,6 +1293,23 @@ recorded trusted decision on a PR's head (the tracker's Approve, or any what lifts the gate for that PR; closing the other ends it ([#15810](https://github.com/hivecommons/hive/issues/15810)). +Both rules judge **heads at the merge gate only**: a head the lane last saw +green with every required verdict recorded, the readiness a train seat needs +([#17439](https://github.com/hivecommons/hive/issues/17439)). A same-issue +PR whose CI is still running, or whose review is outstanding, is not held: +the lane evaluates it on its normal path, so its row reads CI running / +review pending, never *needs a pick* or *needs decision*. Should that head +reach the merge point it is held there instead of merging — on an undecided +claim conflict, or behind the PR that already holds its issue — and from the +next pass it is held at the gate like any other. Within one lane the first +PR at the merge point takes the issue; when several are already at the gate +the pick is the usual green-first, then earlier-opened. The pick then +**stands** until the kept PR closes or is blocked for good (a person merges +it, not mergeable, a terminal required-check hold, a permanent merge +refusal): a check that goes red mid-run no longer hands the issue to the +other PR, so two PRs never point at each other. The kept PR is remembered in +memory per lane and issue; a restart re-plans. + ### Hive-authored superseding PRs auto-merge Owner rule (@clubanderson): *"if a pr is superseded by a pr that a hive diff --git a/src/docs/operator-reference/README.md b/src/docs/operator-reference/README.md index 6a0c0615c3..90aa45ecdb 100644 --- a/src/docs/operator-reference/README.md +++ b/src/docs/operator-reference/README.md @@ -489,7 +489,7 @@ advanced into the merge path. | `auto_merge.require_current_base_default` | `false` | Hive-wide default of the current-base gate for repos in neither `require_current_base` nor `skip_current_base` (Settings → Features → Auto-merge → "Merge only on current base"). **Off** by default: the merge train already update-branches overlapping siblings, so the gate is being soaked per repo (hive's own hive first, via `require_current_base`) before any hive turns it on everywhere. When `true`, a behind head the App cannot update merges as it was tested, with one WARN per PR (`automerge current-base gate: … merging as tested`), so fork-heavy repos are not parked wholesale. | | `auto_merge.skip_current_base` | unset | Repo list (`owner/repo`) with an explicit **off** override for the current-base gate (Settings → Repos → "Merge only on current base" unchecked): a green head there merges even if the base has moved, whatever `require_current_base_default` says. An explicit `require_current_base` entry wins over this list. | | `auto_merge.merge_forward_trusted_human` | unset (off) | Repo list (`owner/repo`) where a `role/trusted` + `author/human` PR on a same-repo branch that GitHub reports as conflicting (`dirty`) gets the base merged into its head as one App-authored, signed-off merge commit via the GitHub merges API (no human commit is rewritten), then merges once CI is green on the new head. A real conflict pushes nothing and posts one `` note naming the files both sides changed. Fork heads and dependency-bot branches (`renovate/*` head ref, or a Renovate, Dependabot or Mergeraptor author) are never written. Attempted once per head (#13636). | -| `auto_merge.merge_train.enabled` | `true` | **Settings → Auto-merge → Merge train → Conflict-aware merge train.** Conflict-aware merge train in every automerge lane (hive, trusted-author, contributor) ([#15117](https://github.com/hivecommons/hive/issues/15117)). Each sweep groups the open approved / mergeable-pending PRs of one repo+base by overlapping changed files (`GET /pulls/{n}/files`, cached per head SHA) and lets ONE front per group merge — ordered readiness first: a head the lane last saw at the merge gate (green, approved, soaking or waiting on a sweep) leads, one it has not evaluated since its push comes next, one still at CI / Review trails, then clean-mergeable before behind, then oldest; recomputed every pass, so a member that turns green moves up and one that turns red moves back — while the others wait at stage/080 with lane reason `merge-train:queued-behind#:/:…` (tracker pill "In merge queue · 3 of 4 · ~4m", never a human stage, never `needs-rebase` as a human flag). The moment the front merges, every sibling whose branch is merely `behind` gets `PUT /pulls/{n}/update-branch`; a `dirty` one is handed to the fixer's rebase-resolve episode (`requested_by: merge-train`, the shared two-attempt guard of [#15104](https://github.com/hivecommons/hive/pull/15104); the fixer router still requires a head the hive can push to, so a fork without maintainer edits stays on the plain `conflicting` path for a person). A dependency bot's branch (`renovate/*` head ref, or a Renovate, Dependabot or Mergeraptor author) gets neither — behind or dirty, sibling or front, the bot rebases it itself (projectbluefin/utah#1097). A head the lane last saw blocked on its own (red required check, terminal hold, permanent merge refusal) never fronts, and a group whose best member is blocked imposes no ordering (fail-open) — nobody waits behind a PR that is not moving. One INFO line `merge train` per order change, not per cycle. The queue carries one PR per issue: when two open PRs declare the same `Fixes #N`, the sweep keeps one (green first, then the earlier-opened) and holds the other with lane reason `merge-train:duplicate-of#:/0:0/0:` (tracker pill "Duplicate of #15402 · needs a pick"; *Let hive decide* supersedes, [#15395](https://github.com/hivecommons/hive/issues/15395)). That pick sees one lane; when the two claimants sit in different lanes (the hive's own PR and a trusted human's PR for one issue) every lane holds its candidate with lane reason `merge-train:claim-conflict#:/0:0/0:` and the log line `same-issue claim conflict undecided: #A vs #B`, so neither is admitted until a decision is recorded on one head or the other PR closes — the PR parks at 085, the chevron reads "held · duplicate of #B · needs decision", and the row's options name the concrete PRs; the hive recommends and never picks ([#15810](https://github.com/hivecommons/hive/issues/15810)). `false` restores independent per-PR evaluation. A PR GitHub reports conflicting on its own, or one at stage/072 returned-to-contributor (label or stage record), is never a member nor counted in the train's size and draws no train idle comment; a conflicting PR rides only while the train itself is repairing it (its rebase requested behind a merged front, loop guard not spent) ([#15395](https://github.com/hivecommons/hive/issues/15395)). | +| `auto_merge.merge_train.enabled` | `true` | **Settings → Auto-merge → Merge train → Conflict-aware merge train.** Conflict-aware merge train in every automerge lane (hive, trusted-author, contributor) ([#15117](https://github.com/hivecommons/hive/issues/15117)). Each sweep groups the open approved / mergeable-pending PRs of one repo+base by overlapping changed files (`GET /pulls/{n}/files`, cached per head SHA) and lets ONE front per group merge — ordered readiness first: a head the lane last saw at the merge gate (green, approved, soaking or waiting on a sweep) leads, one it has not evaluated since its push comes next, one still at CI / Review trails, then clean-mergeable before behind, then oldest; recomputed every pass, so a member that turns green moves up and one that turns red moves back — while the others wait at stage/080 with lane reason `merge-train:queued-behind#:/:…` (tracker pill "In merge queue · 3 of 4 · ~4m", never a human stage, never `needs-rebase` as a human flag). The moment the front merges, every sibling whose branch is merely `behind` gets `PUT /pulls/{n}/update-branch`; a `dirty` one is handed to the fixer's rebase-resolve episode (`requested_by: merge-train`, the shared two-attempt guard of [#15104](https://github.com/hivecommons/hive/pull/15104); the fixer router still requires a head the hive can push to, so a fork without maintainer edits stays on the plain `conflicting` path for a person). A dependency bot's branch (`renovate/*` head ref, or a Renovate, Dependabot or Mergeraptor author) gets neither — behind or dirty, sibling or front, the bot rebases it itself (projectbluefin/utah#1097). A head the lane last saw blocked on its own (red required check, terminal hold, permanent merge refusal) never fronts, and a group whose best member is blocked imposes no ordering (fail-open) — nobody waits behind a PR that is not moving. One INFO line `merge train` per order change, not per cycle. The queue carries one PR per issue: when two open PRs declare the same `Fixes #N`, the sweep keeps one and holds the other with lane reason `merge-train:duplicate-of#:/0:0/0:` (tracker pill "Duplicate of #15402 · needs a pick"; *Let hive decide* supersedes, [#15395](https://github.com/hivecommons/hive/issues/15395)). That pick sees one lane; when the two claimants sit in different lanes (the hive's own PR and a trusted human's PR for one issue) every lane holds its candidate with lane reason `merge-train:claim-conflict#:/0:0/0:` and the log line `same-issue claim conflict undecided: #A vs #B`, so neither is admitted until a decision is recorded on one head or the other PR closes — the PR parks at 085, the chevron reads "held · duplicate of #B · needs decision", and the row's options name the concrete PRs; the hive recommends and never picks ([#15810](https://github.com/hivecommons/hive/issues/15810)). Both rules hold only heads at the merge gate ([#17439](https://github.com/hivecommons/hive/issues/17439)): a same-issue PR still running CI or awaiting review is evaluated as usual (its row reads CI / Review) and is held at the merge point only if it gets there; the first PR at the merge point keeps the issue (among several already at the gate: green first, then the earlier-opened), and the pick stands until that PR closes or is blocked for good — a check going red mid-run does not flip it. `false` restores independent per-PR evaluation. A PR GitHub reports conflicting on its own, or one at stage/072 returned-to-contributor (label or stage record), is never a member nor counted in the train's size and draws no train idle comment; a conflicting PR rides only while the train itself is repairing it (its rebase requested behind a merged front, loop guard not spent) ([#15395](https://github.com/hivecommons/hive/issues/15395)). | | `auto_merge.merge_train.max_rebase_attempts` | `2`; `0` uses the default | **Settings → Auto-merge → Merge train → Rebase attempts per front.** Loop guard: how many rebase-resolve requests the train makes for one PR behind one front. Past it the PR moves to stage/085 with the pill "Needs human decision · rebase failed twice" and the decision option "Rebase, then merge as maintainer". | | `auto_merge.native_merge_queue.enabled` | `false` | Opt-in: once the lane's gates pass, enqueue the PR into GitHub's native merge queue (App auth only) instead of merging directly, and withdraw only Hive's own entries when the PR loses eligibility ([#15198](https://github.com/hivecommons/hive/issues/15198)). Off keeps the pre-queue behaviour everywhere, including the `merge-queue-required` park. See [native-merge-queue.md](../native-merge-queue.md). | | `auto_merge.native_merge_queue.repos` | unset (every watched repo once enabled) | `owner/repo` allow-list for the hand-off, matched case-insensitively. | diff --git a/src/pkg/delegatedecision/policy.go b/src/pkg/delegatedecision/policy.go index ab1bf0d090..7cb2913fec 100644 --- a/src/pkg/delegatedecision/policy.go +++ b/src/pkg/delegatedecision/policy.go @@ -862,8 +862,9 @@ func decideClaimConflict(in Input, ref string) (Decision, bool) { } // decideTrainDuplicate resolves a same-issue pair the merge queue split -// (hivecommons/hive#15395). The queue's pick stands: a green head beats a -// red one, the earlier-opened wins among equals. Deciding on the held PR +// (hivecommons/hive#15395). The queue's pick stands: it picks between +// heads at the merge gate and keeps the pick until the kept PR closes or +// is blocked for good (hivecommons/hive#17439). Deciding on the held PR // closes it as superseded by the kept one, with a comment; deciding on // the kept PR releases it and names the duplicate the queue holds. A // paused repo only gets the recommendation. @@ -896,7 +897,7 @@ func decideTrainDuplicate(in Input, ref string) (Decision, bool) { held := fmt.Sprintf("#%d", other.Number) if in.Conflicting || in.CIRed() || in.DCOMissing { return recommendOnly(RuleOnePRPerIssue, - fmt.Sprintf("the merge queue kept %s over %s%s, but %s is not mergeable now (%s); the queue re-picks on its next pass", ref, held, issue, ref, mergeBlockers(in)), + fmt.Sprintf("the merge queue kept %s over %s%s, but %s is not mergeable now (%s); the queue keeps it until it closes or is blocked for good", ref, held, issue, ref, mergeBlockers(in)), "fix "+ref+" ("+mergeBlockers(in)+"), or decide on "+held+" to let it supersede this one"), true } return releasePR(RuleOnePRPerIssue, diff --git a/src/pkg/github/automerge/automerge_contributor_lane.go b/src/pkg/github/automerge/automerge_contributor_lane.go index 2e2d23e65e..e8a1f108d7 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane.go +++ b/src/pkg/github/automerge/automerge_contributor_lane.go @@ -470,7 +470,7 @@ func (c *Engine) SweepContributorLaneAutoMerges(ctx context.Context, opts AutoMe } result.Candidates++ c.sweepClock.evaluating(displayRepo, number) - event, reason, err := c.trySweepContributorLanePR(withMergeTrainCandidateHold(ctx, c.mergeTrainCandidateReason(train, number)), displayRepo, owner, repoName, number, policy) + event, reason, err := c.trySweepContributorLanePR(c.mergeTrainEvalContext(ctx, train, number), displayRepo, owner, repoName, number, policy) c.sweepClock.evaluated() if err == nil { c.noteMergeTrainOutcome(stageLaneContributor, owner, repoName, number, pr.GetHead().GetSHA(), reason) diff --git a/src/pkg/github/automerge/automerge_merge_train.go b/src/pkg/github/automerge/automerge_merge_train.go index 1323bf2ecc..b8c1445759 100644 --- a/src/pkg/github/automerge/automerge_merge_train.go +++ b/src/pkg/github/automerge/automerge_merge_train.go @@ -417,11 +417,17 @@ type mergeTrainPlan struct { // on their current head: they hold no seat (hivecommons/hive#15825). candidates map[int]*mergeTrainGroup // duplicates are the PRs held out of every train this pass because - // another open PR fixes the same issue (one PR per issue). + // another open PR fixes the same issue (one PR per issue); only heads + // at the merge gate are held (hivecommons/hive#17439). duplicates map[int]mergeTrainDuplicateHold - // claimConflicts are the candidates held because an open PR outside - // this lane claims the same issue and no decision is recorded. + // claimConflicts are the candidates at the merge gate held because an + // open PR outside this lane claims the same issue and no decision is + // recorded. claimConflicts map[int]mergeTrainClaimConflict + // issueTurns are the same-issue PRs short of the merge gate: the lane + // evaluates them on their normal path and the merge point holds one + // rather than merging past either rule (hivecommons/hive#17439). + issueTurns map[int]mergeTrainIssueTurn } // group returns the train number rides in, if any. @@ -479,11 +485,17 @@ func withMergeTrainCandidateHold(ctx context.Context, reason string) context.Con } // mergeTrainCandidateHold is the hold reason a merge point returns instead -// of merging when the lane is evaluating a train candidate; "" otherwise. +// of merging, "" to go on: a same-issue PR's turn (mergeTrainEvalContext) +// first, then the hold of a train candidate. func mergeTrainCandidateHold(ctx context.Context) string { if ctx == nil { return "" } + if turn, ok := ctx.Value(mergeTrainIssueTurnKey{}).(func() string); ok { + if reason := turn(); reason != "" { + return reason + } + } reason, _ := ctx.Value(mergeTrainCandidateHoldKey{}).(string) return reason } @@ -1007,6 +1019,11 @@ type mergeTrainState struct { // the train (earliest first) and its loss is the ejection the log and // the stage store report. mergeTrainSeats map[string]mergeTrainSeat + // mergeTrainIssueWinners remembers, per lane and closing issue, the PR + // the one-PR-per-issue rule kept (hivecommons/hive#17439): it keeps the + // issue until it closes or its head is blocked for good, so a check + // that is red for a pass never flips the pick. + mergeTrainIssueWinners map[string]int // mergeTrainNow is the planner's clock (tests inject one). mergeTrainNow func() time.Time } @@ -1188,12 +1205,17 @@ func mergeTrainBlockingReason(reason string) bool { // mergeTrainReadinessOf reads a lane verdict as a readiness tier: a merge // (""), a soak, a merge-queue wait or a release hold is ready — the head -// would merge but for its turn; CI pending, no reviewer verdict yet or a +// would merge but for its turn; so is a merge-point hold for the train or +// for a same-issue PR (a lane verdict carries those only from the merge +// point, hivecommons/hive#17439); CI pending, no reviewer verdict yet or a // review gate still waiting is unready; any other reason says nothing // about readiness (unknown). func mergeTrainReadinessOf(reason string) int { - if mt, ok := prstage.ParseMergeTrain(reason); ok && mt.State == prstage.MergeTrainStateAdmitted { - return mergeTrainReadinessReady + if mt, ok := prstage.ParseMergeTrain(reason); ok { + switch mt.State { + case prstage.MergeTrainStateAdmitted, prstage.MergeTrainStateDuplicateOf, prstage.MergeTrainStateClaimConflict: + return mergeTrainReadinessReady + } } switch { case reason == "", strings.HasPrefix(reason, prstage.ContributorLaneReasonSoaking), diff --git a/src/pkg/github/automerge/automerge_merge_train_claim_conflict_test.go b/src/pkg/github/automerge/automerge_merge_train_claim_conflict_test.go index d937ab3010..589ec6f5eb 100644 --- a/src/pkg/github/automerge/automerge_merge_train_claim_conflict_test.go +++ b/src/pkg/github/automerge/automerge_merge_train_claim_conflict_test.go @@ -23,6 +23,14 @@ func claimPR(n int, login, body string, at time.Time) *gh.PullRequest { } } +// claimAtGate records the lane's "would merge" verdict for each PR's head +// on o/r: the merge gate the same-issue rules judge (hivecommons/hive#17439). +func claimAtGate(c *Engine, lane string, prs ...*gh.PullRequest) { + for _, pr := range prs { + c.noteMergeTrainOutcome(lane, "o", "r", pr.GetNumber(), pr.GetHead().GetSHA(), "") + } +} + // hivecommons/hive#15810: the hive's own #15783 (self lane) and a trusted // human's #15793 (trusted lane) both closed #15773 and both marched toward // merge because each lane's one-PR-per-issue rule saw only its own PRs. @@ -47,6 +55,8 @@ func TestPlanClaimConflictsHoldsCrossLanePair(t *testing.T) { } return "" } + claimAtGate(c, stageLaneSelf, hive) + claimAtGate(c, stageLaneTrusted, human) self := &mergeTrainPlan{lane: stageLaneSelf, displayRepo: "o/r"} kept := c.planDuplicateHolds(context.Background(), self, stageLaneSelf, "o", "r", "o/r", prs, selfOnly) @@ -101,6 +111,7 @@ func TestPlanClaimConflictsDecidedOrAdoptionAdmits(t *testing.T) { } c := &Engine{humanChangeRequests: store} prs := []*gh.PullRequest{claimPR(15783, "hive-app[bot]", "Closes #15773", t0), claimPR(15793, "clubanderson", "Fixes #15773", t0.Add(time.Hour))} + claimAtGate(c, stageLaneSelf, prs[0]) trusted := &mergeTrainPlan{lane: stageLaneTrusted} kept := c.planDuplicateHolds(context.Background(), trusted, stageLaneTrusted, "o", "r", "o/r", prs, func(p *gh.PullRequest) string { if p.GetUser().GetLogin() != "clubanderson" { @@ -134,6 +145,7 @@ func TestPlanClaimConflictsDecidedOrAdoptionAdmits(t *testing.T) { // Two candidates of one lane stay with the one-PR-per-issue winner // rule, which already sees them both. + claimAtGate(c, stageLaneSelf, prs...) plan = &mergeTrainPlan{lane: stageLaneSelf} c.planDuplicateHolds(context.Background(), plan, stageLaneSelf, "o", "r", "o/r", prs, all) if plan.claimConflicts != nil { @@ -153,6 +165,7 @@ func TestPlanClaimConflictsSeesClaimantsOutsideNarrowedList(t *testing.T) { hive, human := claimPR(15783, "hive-app[bot]", "Closes #15773", t0), claimPR(15793, "clubanderson", "Fixes #15773", t0.Add(time.Hour)) c := &Engine{} c.rememberClaimants("o/r", []*gh.PullRequest{hive, human}) + claimAtGate(c, stageLaneTrusted, human) plan := &mergeTrainPlan{lane: stageLaneTrusted} kept := c.planDuplicateHolds(context.Background(), plan, stageLaneTrusted, "o", "r", "o/r", []*gh.PullRequest{human}, func(*gh.PullRequest) string { return "" }) if h, held := plan.claimConflictOf(15793); !held || h.Other != 15783 || h.Issue != 15773 || len(kept) != 0 { @@ -165,3 +178,40 @@ func TestPlanClaimConflictsSeesClaimantsOutsideNarrowedList(t *testing.T) { t.Fatalf("other repo held: %+v", plan.claimConflicts) } } + +// hivecommons/hive#17439: a claimant whose CI is still running is not +// held for a decision — the lane evaluates it, so its row reads CI running +// — but the merge point holds it all the same; once its head is at the +// gate it is held for the decision. +func TestPlanClaimConflictsDefersHeadShortOfTheGate(t *testing.T) { + t0 := time.Date(2026, 11, 3, 10, 25, 0, 0, time.UTC) + hive, human := claimPR(15783, "hive-app[bot]", "Closes #15773", t0), claimPR(15793, "clubanderson", "Fixes #15773", t0.Add(time.Hour)) + trustedOnly := func(p *gh.PullRequest) string { + if p.GetUser().GetLogin() != "clubanderson" { + return "author:not-trusted" + } + return "" + } + c := &Engine{} + c.noteMergeTrainOutcome(stageLaneTrusted, "o", "r", 15793, human.GetHead().GetSHA(), "required-check-pending:build") + plan := &mergeTrainPlan{lane: stageLaneTrusted, displayRepo: "o/r", owner: "o", repo: "r"} + kept := c.planDuplicateHolds(context.Background(), plan, stageLaneTrusted, "o", "r", "o/r", []*gh.PullRequest{hive, human}, trustedOnly) + if _, held := plan.claimConflictOf(15793); held { + t.Fatal("held for a decision while its CI runs") + } + if len(kept) != 1 || kept[0].GetNumber() != 15783 { + t.Fatalf("kept %v, want the claimant out of the trains", kept) + } + reason := mergeTrainCandidateHold(c.mergeTrainEvalContext(context.Background(), plan, 15793)) + mt, ok := prstage.ParseMergeTrain(reason) + if !ok || mt.State != prstage.MergeTrainStateClaimConflict || mt.Front != 15783 || mt.Position != 15773 { + t.Fatalf("merge point = %q", reason) + } + + c.noteMergeTrainOutcome(stageLaneTrusted, "o", "r", 15793, human.GetHead().GetSHA(), reason) + plan = &mergeTrainPlan{lane: stageLaneTrusted, displayRepo: "o/r", owner: "o", repo: "r"} + c.planDuplicateHolds(context.Background(), plan, stageLaneTrusted, "o", "r", "o/r", []*gh.PullRequest{hive, human}, trustedOnly) + if h, held := plan.claimConflictOf(15793); !held || h.Other != 15783 || h.Issue != 15773 { + t.Fatalf("at the gate: hold=%+v held=%v", h, held) + } +} diff --git a/src/pkg/github/automerge/automerge_merge_train_duplicate.go b/src/pkg/github/automerge/automerge_merge_train_duplicate.go index 9d24578e2b..1306d2cf49 100644 --- a/src/pkg/github/automerge/automerge_merge_train_duplicate.go +++ b/src/pkg/github/automerge/automerge_merge_train_duplicate.go @@ -5,6 +5,7 @@ import ( "slices" "sort" "strconv" + "strings" "time" gh "github.com/google/go-github/v72/github" @@ -29,6 +30,14 @@ type mergeTrainDuplicateCandidate struct { // Green: the head is not blocked (red required check, terminal hold, // permanent refusal) and GitHub does not report it conflicting. Green bool + // AtGate: the lane last saw this exact head at the merge gate — CI + // green, every required verdict in (mergeTrainAtGate). Only such heads + // are picked between (hivecommons/hive#17439). + AtGate bool + // Terminal: the lane last saw this exact head blocked for good + // (mergeTrainHeadTerminal). A remembered winner in this state lets + // the issue go; a merely red check does not. + Terminal bool } // mergeTrainDuplicateHold is the losing side of a pair: Number is held as @@ -37,10 +46,10 @@ type mergeTrainDuplicateHold struct { Number, Winner, Issue int } -// mergeTrainDuplicateWinner picks the PR the queue keeps for one issue: -// a green head beats a red one; among equals the earlier-opened PR wins -// (lower number on a tie). So two green PRs keep the earlier; an earlier -// red one yields to a later green one. +// mergeTrainDuplicateWinner picks the best of the candidates: a green head +// beats a red one; among equals the earlier-opened PR wins (lower number +// on a tie). mergeTrainIssueWinner asks it about the heads at the merge +// gate only. func mergeTrainDuplicateWinner(cands []mergeTrainDuplicateCandidate) int { if len(cands) == 0 { return 0 @@ -64,11 +73,36 @@ func mergeTrainDuplicateBetter(a, b mergeTrainDuplicateCandidate) bool { return a.Number < b.Number } -// mergeTrainDuplicateHolds groups the candidates by the issue each one -// closes and holds every PR but the winner per issue. A PR closing several -// issues is held on the first issue it loses. Holds come back sorted by -// PR number so the plan is stable across passes. -func mergeTrainDuplicateHolds(byIssue map[int][]mergeTrainDuplicateCandidate) []mergeTrainDuplicateHold { +// mergeTrainIssueWinner is the PR one issue's group yields to +// (hivecommons/hive#17439): the winner remembered from an earlier pass +// while it is still in the group and not blocked for good — a check that +// is red mid-run never hands the issue to the other PR — else the best +// of the members at the merge gate; 0 while no member is at the gate, so +// the first PR to reach the merge point takes the issue. +func mergeTrainIssueWinner(cands []mergeTrainDuplicateCandidate, remembered int) int { + var atGate []mergeTrainDuplicateCandidate + for _, c := range cands { + if remembered > 0 && c.Number == remembered && !c.Terminal { + return remembered + } + if c.AtGate { + atGate = append(atGate, c) + } + } + return mergeTrainDuplicateWinner(atGate) +} + +// mergeTrainDuplicatePicks runs the one-PR-per-issue rule over every issue +// two or more candidates close. remembered is the winner the lane kept per +// issue on an earlier pass. winners names each contested issue's winner +// (0: nobody at the gate yet); holds are the members at the merge gate +// that yield to a winner, sorted by PR number (a PR closing several +// issues is held on the first it loses); turns are, per member short of +// the gate, the issues whose turn it must claim at the merge point before +// it may merge (hivecommons/hive#17439). +func mergeTrainDuplicatePicks(byIssue map[int][]mergeTrainDuplicateCandidate, remembered map[int]int) (winners map[int]int, holds []mergeTrainDuplicateHold, turns map[int][]int) { + winners = map[int]int{} + turns = map[int][]int{} held := map[int]mergeTrainDuplicateHold{} issues := make([]int, 0, len(byIssue)) for issue := range byIssue { @@ -80,22 +114,30 @@ func mergeTrainDuplicateHolds(byIssue map[int][]mergeTrainDuplicateCandidate) [] if len(cands) < 2 { continue } - winner := mergeTrainDuplicateWinner(cands) + winner := mergeTrainIssueWinner(cands, remembered[issue]) + winners[issue] = winner for _, c := range cands { if c.Number == winner { continue } + if !c.AtGate { + turns[c.Number] = append(turns[c.Number], issue) + continue + } if _, done := held[c.Number]; !done { held[c.Number] = mergeTrainDuplicateHold{Number: c.Number, Winner: winner, Issue: issue} } } } - out := make([]mergeTrainDuplicateHold, 0, len(held)) + for n := range held { + delete(turns, n) + } + holds = make([]mergeTrainDuplicateHold, 0, len(held)) for _, h := range held { - out = append(out, h) + holds = append(holds, h) } - sort.Slice(out, func(i, j int) bool { return out[i].Number < out[j].Number }) - return out + sort.Slice(holds, func(i, j int) bool { return holds[i].Number < holds[j].Number }) + return winners, holds, turns } // mergeTrainClosedIssues reads the same-repo issues a PR's title and body @@ -128,25 +170,47 @@ func mergeTrainSameLine(a, b *gh.PullRequest) bool { } // planDuplicateHolds runs the one-PR-per-issue rule over the lane's -// candidates before the trains are grouped: the held PRs leave the -// candidate list and the plan remembers who each one yields to. PRs are -// grouped by base branch first, so siblings on different release lines -// never compete. +// candidates before the trains are grouped (hivecommons/hive#15395). PRs +// are grouped by base branch first, so siblings on different release +// lines never compete (hivecommons/hive#17436). The rule judges heads at +// the merge gate only (hivecommons/hive#17439): a same-issue candidate +// whose CI is still running or whose review is outstanding is evaluated +// on its normal path, so its row reads CI running / review pending rather +// than "needs a pick", and carries a turn the merge point checks, so it +// still cannot merge past the rule. A candidate at the gate behind its +// issue's winner is held: it leaves the candidate list and the plan +// remembers who it yields to. func (c *Engine) planDuplicateHolds(ctx context.Context, plan *mergeTrainPlan, lane, owner, repo, displayRepo string, prs []*gh.PullRequest, prefilter func(*gh.PullRequest) string) []*gh.PullRequest { - prs = c.planClaimConflicts(ctx, plan, owner, repo, displayRepo, prs, prefilter) + candidate := map[int]bool{} + for _, pr := range prs { + if pr != nil && pr.GetNumber() > 0 && mergeTrainCandidate(prefilter(pr)) { + candidate[pr.GetNumber()] = true + } + } + if len(candidate) == 0 { + return prs + } + claimants, whole := c.claimantsForConflicts(ctx, owner, repo, prs) + if whole { + c.forgetClosedMergeTrainWinners(lane, owner, repo, claimants) + } + prs = c.planClaimConflicts(ctx, plan, lane, owner, repo, displayRepo, prs, claimants, candidate) byLine := map[string]map[int][]mergeTrainDuplicateCandidate{} for _, pr := range prs { - if pr == nil || pr.GetNumber() <= 0 || !mergeTrainCandidate(prefilter(pr)) { + if pr == nil || !candidate[pr.GetNumber()] { continue } issues := mergeTrainClosedIssues(pr, displayRepo) if len(issues) == 0 { continue } + n, sha := pr.GetNumber(), pr.GetHead().GetSHA() cand := mergeTrainDuplicateCandidate{ - Number: pr.GetNumber(), + Number: n, CreatedAt: pr.GetCreatedAt().Time, - Green: !mergeTrainDirty(pr.GetMergeableState()) && !c.mergeTrainHeadBlocked(lane, owner, repo, pr.GetNumber(), pr.GetHead().GetSHA()), + Green: !mergeTrainDirty(pr.GetMergeableState()) && !c.mergeTrainHeadBlocked(lane, owner, repo, n, sha), + AtGate: c.mergeTrainAtGate(lane, owner, repo, pr), + Terminal: c.mergeTrainHeadTerminal(lane, owner, repo, n, sha), } line := pr.GetBase().GetRef() byIssue := byLine[line] @@ -159,8 +223,14 @@ func (c *Engine) planDuplicateHolds(ctx context.Context, plan *mergeTrainPlan, l } } var holds []mergeTrainDuplicateHold - for _, byIssue := range byLine { - holds = append(holds, mergeTrainDuplicateHolds(byIssue)...) + for line, byIssue := range byLine { + winners, h, turns := mergeTrainDuplicatePicks(byIssue, c.mergeTrainRememberedWinners(lane, owner, repo, line, byIssue)) + c.rememberMergeTrainWinners(lane, owner, repo, line, displayRepo, winners) + holds = append(holds, h...) + for n, issues := range turns { + plan.deferIssueTurn(n, mergeTrainIssueTurn{Base: line, Issues: issues}) + c.debug("merge train: same-issue PR short of the merge gate; evaluated, held at the merge point", "repo", displayRepo, "pr", n, "issues", issues, "lane", plan.lane) + } } sort.Slice(holds, func(i, j int) bool { return holds[i].Number < holds[j].Number }) if len(holds) == 0 { @@ -253,25 +323,27 @@ func (c *Engine) rememberClaimants(fullRepo string, prs []*gh.PullRequest) { // the forge-state snapshot when one is wired, else the full /pulls list // the lane's openPullRequests just fetched for this repo (remembered // before narrowing to targets); the lane's own list only when neither is -// available (a bare Engine in tests). -func (c *Engine) claimantsForConflicts(ctx context.Context, owner, repo string, prs []*gh.PullRequest) []*gh.PullRequest { +// available (a bare Engine in tests). whole is false when a wired +// forge-state read failed and the lane's own, possibly event-narrowed, +// list stands in: absence from it does not mean a PR closed. +func (c *Engine) claimantsForConflicts(ctx context.Context, owner, repo string, prs []*gh.PullRequest) (claimants []*gh.PullRequest, whole bool) { if c == nil { - return prs + return prs, true } if c.openPRs == nil { c.claimantsMu.Lock() all, ok := c.claimants[owner+"/"+repo] c.claimantsMu.Unlock() if ok && len(all) > 0 { - return all + return all, true } - return prs + return prs, true } all, skip, err := c.openPullRequests(ctx, owner, repo, nil) if err != nil || skip != "" || len(all) == 0 { - return prs + return prs, false } - return all + return all, true } // planClaimConflicts holds every lane candidate whose closing issue another @@ -281,19 +353,14 @@ func (c *Engine) claimantsForConflicts(ctx context.Context, owner, repo string, // them both; the gate exists for the pair the lanes split — the hive's // own PR in the self lane and a trusted human's PR in the trusted lane // both marched toward merge for hivecommons/hive#15773 because each lane -// saw only its own (hivecommons/hive#15810). -func (c *Engine) planClaimConflicts(ctx context.Context, plan *mergeTrainPlan, owner, repo, displayRepo string, prs []*gh.PullRequest, prefilter func(*gh.PullRequest) string) []*gh.PullRequest { - candidate := map[int]bool{} - for _, pr := range prs { - if pr != nil && pr.GetNumber() > 0 && mergeTrainCandidate(prefilter(pr)) { - candidate[pr.GetNumber()] = true - } - } - if len(candidate) == 0 { - return prs - } +// saw only its own (hivecommons/hive#15810). Only a head at the merge gate +// is held for the decision; one whose CI is still running or whose review +// is outstanding is evaluated on its normal path and meets the same hold +// at the merge point (hivecommons/hive#17439). Either way it leaves the +// list the trains and the winner rule are grouped from. +func (c *Engine) planClaimConflicts(ctx context.Context, plan *mergeTrainPlan, lane, owner, repo, displayRepo string, prs, claimants []*gh.PullRequest, candidate map[int]bool) []*gh.PullRequest { byIssue := map[int][]*gh.PullRequest{} - for _, pr := range c.claimantsForConflicts(ctx, owner, repo, prs) { + for _, pr := range claimants { if pr == nil || pr.GetNumber() <= 0 || (pr.State != nil && pr.GetState() != "open") { continue } @@ -329,11 +396,19 @@ func (c *Engine) planClaimConflicts(ctx context.Context, plan *mergeTrainPlan, o if len(held) == 0 { return prs } - plan.claimConflicts = held kept := make([]*gh.PullRequest, 0, len(prs)) for _, pr := range prs { - if _, h := held[pr.GetNumber()]; !h { + h, conflicted := held[pr.GetNumber()] + switch { + case !conflicted: kept = append(kept, pr) + case c.mergeTrainAtGate(lane, owner, repo, pr): + if plan.claimConflicts == nil { + plan.claimConflicts = map[int]mergeTrainClaimConflict{} + } + plan.claimConflicts[h.Number] = h + default: + plan.deferIssueTurn(h.Number, mergeTrainIssueTurn{Conflict: h}) } } return kept @@ -356,3 +431,189 @@ func (c *Engine) mergeTrainClaimConflictSkip(plan *mergeTrainPlan, number int) s c.debug("automerge skip", "repo", plan.displayRepo, "pr", number, "reason", reason, "lane", plan.lane, "other", strconv.Itoa(h.Other)) return reason } + +// mergeTrainAtGate reports whether the lane last saw this exact head at +// the merge gate — CI green, every required verdict recorded, mergeable +// but for its turn — the readiness a train seat needs too. Only such a +// head is judged by the same-issue rules (hivecommons/hive#17439): a PR +// whose CI is still running or whose review is outstanding is not yet a +// pick anyone could make. +func (c *Engine) mergeTrainAtGate(lane, owner, repo string, pr *gh.PullRequest) bool { + n, sha := pr.GetNumber(), pr.GetHead().GetSHA() + return !c.mergeTrainHeadBlocked(lane, owner, repo, n, sha) && c.mergeTrainHeadReadiness(lane, owner, repo, n, sha) == mergeTrainReadinessReady +} + +// mergeTrainHeadTerminal reports whether this exact head was last seen by +// the lane blocked for good: a person merges it, or a hold no CI re-run +// clears (not mergeable, a terminal required-check hold, a permanent merge +// refusal). A red check is not terminal — on a head whose CI is still +// running it is noise, and a re-run or the fixer may clear it — so it +// never costs a remembered winner its issue (hivecommons/hive#17439). +func (c *Engine) mergeTrainHeadTerminal(lane, owner, repo string, number int, sha string) bool { + v, ok := c.mergeTrainVerdictFor(lane, owner, repo, number, sha) + return ok && (v.humanMerge || (v.blocked && classifyCIReason(v.reason) != ciReasonRed)) +} + +// mergeTrainIssueTurn is a same-issue PR the lane evaluates on its normal +// path because its head is not at the merge gate yet +// (hivecommons/hive#17439). Its row shows CI and review as they are; if +// the head reaches the merge point it is held there instead of merging: +// on an undecided claim conflict (Conflict.Other set), or behind the PR +// that already holds the turn of one of Issues on base branch Base. +type mergeTrainIssueTurn struct { + Conflict mergeTrainClaimConflict + Base string + Issues []int +} + +// deferIssueTurn records number as evaluated with a same-issue turn. +func (p *mergeTrainPlan) deferIssueTurn(number int, t mergeTrainIssueTurn) { + if p.issueTurns == nil { + p.issueTurns = map[int]mergeTrainIssueTurn{} + } + p.issueTurns[number] = t +} + +// issueTurnOf reports the same-issue turn number is evaluated with. +func (p *mergeTrainPlan) issueTurnOf(number int) (mergeTrainIssueTurn, bool) { + if p == nil || p.issueTurns == nil { + return mergeTrainIssueTurn{}, false + } + t, ok := p.issueTurns[number] + return t, ok +} + +// mergeTrainIssueTurnKey is the context key under which a lane carries a +// same-issue PR's turn to the merge point. +type mergeTrainIssueTurnKey struct{} + +// mergeTrainEvalContext is the context a lane evaluates number under: its +// same-issue turn, if any, and its train candidate hold. The merge point +// asks mergeTrainCandidateHold, which settles the turn first. +func (c *Engine) mergeTrainEvalContext(ctx context.Context, plan *mergeTrainPlan, number int) context.Context { + if t, ok := plan.issueTurnOf(number); ok { + ctx = context.WithValue(ctx, mergeTrainIssueTurnKey{}, func() string { + return c.mergeTrainIssueTurnHold(plan, number, t) + }) + } + return withMergeTrainCandidateHold(ctx, c.mergeTrainCandidateReason(plan, number)) +} + +// mergeTrainIssueTurnHold is the hold a same-issue PR meets at the merge +// point, "" when it may go on: an undecided claim conflict holds it; +// otherwise it claims each issue's turn in order and yields to the first +// issue another PR already holds — reaching the merge point behind the +// issue's winner is the same verdict as waiting at the gate behind it. +func (c *Engine) mergeTrainIssueTurnHold(plan *mergeTrainPlan, number int, t mergeTrainIssueTurn) string { + if t.Conflict.Other > 0 { + return mergeTrainClaimConflictReason(t.Conflict) + } + for _, issue := range t.Issues { + if w := c.claimMergeTrainIssueTurn(plan.lane, plan.owner, plan.repo, t.Base, plan.displayRepo, issue, number); w != number { + return mergeTrainDuplicateReason(mergeTrainDuplicateHold{Number: number, Winner: w, Issue: issue}) + } + } + return "" +} + +// mergeTrainIssueWinnerKey keys the PR that holds issue's turn on one +// base branch: release-line siblings never share a turn +// (hivecommons/hive#17436). It extends the lane's verdict key, so +// forgetClosedMergeTrainWinners still finds it by prefix. +func mergeTrainIssueWinnerKey(lane, owner, repo, base string, issue int) string { + return mergeTrainVerdictKey(lane, owner, repo, issue) + "@" + base +} + +// claimMergeTrainIssueTurn is the merge point's one-PR-per-issue check: +// the PR that holds issue's turn on base when it is another; otherwise +// number takes the turn — the first of the issue's PRs at the merge point +// keeps the issue until it closes or is blocked for good — and is +// returned. +func (c *Engine) claimMergeTrainIssueTurn(lane, owner, repo, base, displayRepo string, issue, number int) int { + key := mergeTrainIssueWinnerKey(lane, owner, repo, base, issue) + c.mergeTrainMu.Lock() + if w := c.mergeTrainIssueWinners[key]; w > 0 && w != number { + c.mergeTrainMu.Unlock() + return w + } + _, had := c.mergeTrainIssueWinners[key] + if c.mergeTrainIssueWinners == nil { + c.mergeTrainIssueWinners = map[string]int{} + } + c.mergeTrainIssueWinners[key] = number + c.mergeTrainMu.Unlock() + if !had { + c.info("merge train: first at the merge gate keeps the issue", "repo", displayRepo, "pr", number, "issue", issue, "base", base, "lane", lane) + } + return number +} + +// mergeTrainRememberedWinners reads the winner the lane kept on base on +// an earlier pass for each issue two or more candidates close. +func (c *Engine) mergeTrainRememberedWinners(lane, owner, repo, base string, byIssue map[int][]mergeTrainDuplicateCandidate) map[int]int { + out := map[int]int{} + c.mergeTrainMu.Lock() + defer c.mergeTrainMu.Unlock() + for issue, cands := range byIssue { + if len(cands) < 2 { + continue + } + if w, ok := c.mergeTrainIssueWinners[mergeTrainIssueWinnerKey(lane, owner, repo, base, issue)]; ok { + out[issue] = w + } + } + return out +} + +// rememberMergeTrainWinners stores this pass's pick on base per contested +// issue: a winner is kept for the next pass; an issue nobody at the gate +// holds is forgotten, so the first of its PRs at the merge point takes +// it. A new winner is logged once. +func (c *Engine) rememberMergeTrainWinners(lane, owner, repo, base, displayRepo string, winners map[int]int) { + type pick struct{ issue, winner, was int } + var picked []pick + c.mergeTrainMu.Lock() + for issue, w := range winners { + key := mergeTrainIssueWinnerKey(lane, owner, repo, base, issue) + was := c.mergeTrainIssueWinners[key] + if w <= 0 { + delete(c.mergeTrainIssueWinners, key) + continue + } + if c.mergeTrainIssueWinners == nil { + c.mergeTrainIssueWinners = map[string]int{} + } + c.mergeTrainIssueWinners[key] = w + if was != w { + picked = append(picked, pick{issue: issue, winner: w, was: was}) + } + } + c.mergeTrainMu.Unlock() + sort.Slice(picked, func(i, j int) bool { return picked[i].issue < picked[j].issue }) + for _, p := range picked { + c.info("merge train: same-issue winner picked at the merge gate", "repo", displayRepo, "issue", p.issue, "base", base, "winner", p.winner, "previous", p.was, "lane", lane) + } +} + +// forgetClosedMergeTrainWinners drops the lane's remembered winners on +// owner/repo whose PR is no longer among the repository's open PRs. +func (c *Engine) forgetClosedMergeTrainWinners(lane, owner, repo string, open []*gh.PullRequest) { + if len(open) == 0 { + return + } + alive := make(map[int]bool, len(open)) + for _, pr := range open { + if pr != nil && pr.GetNumber() > 0 && (pr.State == nil || pr.GetState() == "open") { + alive[pr.GetNumber()] = true + } + } + prefix := mergeTrainVerdictKey(lane, owner, repo, 0) + prefix = prefix[:strings.LastIndex(prefix, "#")+1] + c.mergeTrainMu.Lock() + defer c.mergeTrainMu.Unlock() + for key, w := range c.mergeTrainIssueWinners { + if strings.HasPrefix(key, prefix) && !alive[w] { + delete(c.mergeTrainIssueWinners, key) + } + } +} diff --git a/src/pkg/github/automerge/automerge_merge_train_duplicate_test.go b/src/pkg/github/automerge/automerge_merge_train_duplicate_test.go index 35f2ed1770..eec863ffad 100644 --- a/src/pkg/github/automerge/automerge_merge_train_duplicate_test.go +++ b/src/pkg/github/automerge/automerge_merge_train_duplicate_test.go @@ -2,6 +2,7 @@ package automerge import ( "context" + "slices" "testing" "time" @@ -40,33 +41,82 @@ func TestMergeTrainDuplicateWinner(t *testing.T) { } } -func TestMergeTrainDuplicateHolds(t *testing.T) { +// The one-PR-per-issue rule picks only between heads at the merge gate +// and keeps its pick until the winner is blocked for good +// (hivecommons/hive#17439): a head still running CI is neither picked nor +// held — it claims the issue's turn at the merge point instead. +func TestMergeTrainDuplicatePicks(t *testing.T) { t0 := time.Date(2026, 10, 29, 10, 0, 0, 0, time.UTC) - byIssue := map[int][]mergeTrainDuplicateCandidate{ - 15396: {{Number: 15402, CreatedAt: t0, Green: true}, {Number: 15407, CreatedAt: t0.Add(time.Hour), Green: true}}, - 15300: {{Number: 15320, CreatedAt: t0, Green: false}, {Number: 15330, CreatedAt: t0.Add(time.Hour), Green: true}}, - 15999: {{Number: 15350, CreatedAt: t0, Green: true}}, - } - got := mergeTrainDuplicateHolds(byIssue) - want := []mergeTrainDuplicateHold{{Number: 15320, Winner: 15330, Issue: 15300}, {Number: 15407, Winner: 15402, Issue: 15396}} - if len(got) != len(want) { - t.Fatalf("holds = %+v, want %+v", got, want) - } - for i := range want { - if got[i] != want[i] { - t.Fatalf("hold[%d] = %+v, want %+v", i, got[i], want[i]) - } + cand := func(n, min int, atGate bool) mergeTrainDuplicateCandidate { + return mergeTrainDuplicateCandidate{Number: n, CreatedAt: t0.Add(time.Duration(min) * time.Minute), Green: atGate, AtGate: atGate} } - reason := mergeTrainDuplicateReason(want[1]) - if reason != "merge-train:duplicate-of#15402:15396/0:0/0:" { - t.Fatalf("reason = %q", reason) + terminal := func(c mergeTrainDuplicateCandidate) mergeTrainDuplicateCandidate { + c.Terminal = true + return c } - m, ok := prstage.ParseMergeTrain(reason) - if !ok || m.State != prstage.MergeTrainStateDuplicateOf || m.Front != 15402 || m.Position != 15396 { - t.Fatalf("parse = %+v ok=%v", m, ok) + for _, tc := range []struct { + name string + cands []mergeTrainDuplicateCandidate + remembered int + winner int + held []int + turns []int + }{ + {"both at the gate: the earlier wins, the later is held", []mergeTrainDuplicateCandidate{cand(15407, 30, true), cand(15402, 0, true)}, 0, 15402, []int{15407}, nil}, + {"CI running on both: nobody is picked or held", []mergeTrainDuplicateCandidate{cand(15402, 0, false), cand(15407, 30, false)}, 0, 0, nil, []int{15402, 15407}}, + {"only the later at the gate: it takes the issue, the earlier is not held", []mergeTrainDuplicateCandidate{cand(15402, 0, false), cand(15407, 30, true)}, 0, 15407, nil, []int{15402}}, + {"remembered winner with a red check keeps the issue", []mergeTrainDuplicateCandidate{cand(15402, 0, false), cand(15407, 30, true)}, 15402, 15402, []int{15407}, nil}, + {"remembered winner blocked for good lets it go", []mergeTrainDuplicateCandidate{terminal(cand(15402, 0, false)), cand(15407, 30, true)}, 15402, 15407, nil, []int{15402}}, + {"remembered winner gone: re-picked at the gate", []mergeTrainDuplicateCandidate{cand(15407, 30, true), cand(15410, 60, true)}, 15402, 15407, []int{15410}, nil}, + } { + t.Run(tc.name, func(t *testing.T) { + winners, holds, turns := mergeTrainDuplicatePicks(map[int][]mergeTrainDuplicateCandidate{15396: tc.cands}, map[int]int{15396: tc.remembered}) + if winners[15396] != tc.winner { + t.Fatalf("winner = #%d, want #%d", winners[15396], tc.winner) + } + var held []int + for _, h := range holds { + if h.Winner != tc.winner || h.Issue != 15396 { + t.Fatalf("hold %+v, want behind #%d for #15396", h, tc.winner) + } + held = append(held, h.Number) + } + if !slices.Equal(held, tc.held) { + t.Fatalf("held = %v, want %v", held, tc.held) + } + var turned []int + for n, issues := range turns { + if !slices.Equal(issues, []int{15396}) { + t.Fatalf("turn of #%d = %v", n, issues) + } + turned = append(turned, n) + } + slices.Sort(turned) + if !slices.Equal(turned, tc.turns) { + t.Fatalf("turns = %v, want %v", turned, tc.turns) + } + }) + } + + // A PR closing two issues is held on the first it loses and claims no + // turn on the other; an issue only one PR closes is no contest. + winners, holds, turns := mergeTrainDuplicatePicks(map[int][]mergeTrainDuplicateCandidate{ + 15300: {cand(15320, 0, true), cand(15330, 60, true)}, + 15301: {cand(15330, 60, true), cand(15340, 90, false)}, + 15999: {cand(15350, 0, true)}, + }, nil) + if len(holds) != 1 || holds[0] != (mergeTrainDuplicateHold{Number: 15330, Winner: 15320, Issue: 15300}) { + t.Fatalf("holds = %+v", holds) + } + if len(turns) != 1 || !slices.Equal(turns[15340], []int{15301}) { + t.Fatalf("turns = %v", turns) + } + if _, contested := winners[15999]; contested || winners[15301] != 15330 { + t.Fatalf("winners = %v", winners) } - if s := m.Sentence(); s != "duplicate of #15402 for issue #15396; superseded by #15402 — the merge queue carries one PR per issue; close it or let the delegate decide" { - t.Fatalf("sentence = %q", s) + mt, ok := prstage.ParseMergeTrain(mergeTrainDuplicateReason(holds[0])) + if !ok || mt.State != prstage.MergeTrainStateDuplicateOf || mt.Front != 15320 || mt.Position != 15300 { + t.Fatalf("reason parsed %+v ok=%v", mt, ok) } } @@ -86,6 +136,9 @@ func TestPlanDuplicateHoldsFiltersCandidates(t *testing.T) { pr(15411, "Fixes #15397", t0, "clean"), } plan := &mergeTrainPlan{lane: "self", displayRepo: "o/r"} + for _, p := range prs { + c.noteMergeTrainOutcome(stageLaneSelf, "o", "r", p.GetNumber(), p.GetHead().GetSHA(), "") + } kept := c.planDuplicateHolds(context.Background(), plan, stageLaneSelf, "o", "r", "o/r", prs, func(*gh.PullRequest) string { return "" }) if len(kept) != 3 || kept[0].GetNumber() != 15407 { t.Fatalf("kept = %v", kept) @@ -154,6 +207,76 @@ func TestPlanMergeTrainsHoldsSameIssueDuplicate(t *testing.T) { } } +// hivecommons/hive#17439: #17414 and #17415 fixed one issue and, with CI +// at 19/64, both read "needs human decision" while the winner flipped on +// every red check. A head short of the merge gate is not held: the lane +// evaluates it, so its row reads CI running. The first to reach the merge +// point keeps the issue; the other is held there and, at the gate, held +// behind it. A red check on the winner does not flip the pick; a block no +// re-run clears does. +func TestPlanDuplicateHoldsJudgesOnlyHeadsAtTheGate(t *testing.T) { + c := &Engine{} + t0 := time.Date(2026, 11, 9, 17, 0, 0, 0, time.UTC) + early, late := claimPR(17414, "hive-app[bot]", "Fixes #17392", t0), claimPR(17415, "hive-app[bot]", "Fixes #17392", t0.Add(time.Minute)) + prs := []*gh.PullRequest{early, late} + note := func(pr *gh.PullRequest, reason string) { + c.noteMergeTrainOutcome(stageLaneSelf, "o", "r", pr.GetNumber(), pr.GetHead().GetSHA(), reason) + } + plan := func() *mergeTrainPlan { + p := &mergeTrainPlan{lane: stageLaneSelf, displayRepo: "o/r", owner: "o", repo: "r"} + c.planDuplicateHolds(context.Background(), p, stageLaneSelf, "o", "r", "o/r", prs, func(*gh.PullRequest) string { return "" }) + return p + } + atMergePoint := func(p *mergeTrainPlan, pr *gh.PullRequest) string { + return mergeTrainCandidateHold(c.mergeTrainEvalContext(context.Background(), p, pr.GetNumber())) + } + + note(early, "required-check-failing:lint") + note(late, "required-check-pending:build") + p := plan() + if p.duplicates != nil || p.claimConflicts != nil { + t.Fatalf("held while CI runs: dup=%v conflict=%v", p.duplicates, p.claimConflicts) + } + if _, turn := p.issueTurnOf(17414); !turn { + t.Fatal("the red head carries no turn to the merge point") + } + if r := atMergePoint(p, late); r != "" { + t.Fatalf("first at the merge point held: %q", r) + } + held := atMergePoint(p, early) + if mt, ok := prstage.ParseMergeTrain(held); !ok || mt.State != prstage.MergeTrainStateDuplicateOf || mt.Front != 17415 || mt.Position != 17392 { + t.Fatalf("second at the merge point = %q", held) + } + + note(late, prstage.ContributorLaneReasonSoaking) + note(early, held) + p = plan() + if h, ok := p.duplicateOf(17414); !ok || h.Winner != 17415 || h.Issue != 17392 { + t.Fatalf("at the gate behind the first: %+v held=%v", h, ok) + } + if _, ok := p.duplicateOf(17415); ok { + t.Fatal("the winner is held") + } + + note(late, "required-check-failing:build") + p = plan() + if h, ok := p.duplicateOf(17414); !ok || h.Winner != 17415 { + t.Fatalf("a red check flipped the pick: %+v held=%v", h, ok) + } + + note(late, "not-mergeable") + p = plan() + if _, ok := p.duplicateOf(17414); ok { + t.Fatal("a winner blocked for good still holds the issue") + } + if r := atMergePoint(p, early); r != "" { + t.Fatalf("the new winner is held at the merge point: %q", r) + } + if mt, ok := prstage.ParseMergeTrain(atMergePoint(p, late)); !ok || mt.State != prstage.MergeTrainStateDuplicateOf || mt.Front != 17414 { + t.Fatalf("the old winner at the merge point = %+v ok=%v", mt, ok) + } +} + // The same fix opened against two release lines says "Fixes #N" twice and // is not a duplicate: neither PR is held and neither is a claim conflict // (hivecommons/hive#17436). Two PRs on the SAME line still compete. @@ -168,6 +291,10 @@ func TestPlanDuplicateHoldsGroupsByBaseBranch(t *testing.T) { pr(17415, "v6", t0.Add(time.Minute)), pr(17420, "v6", t0.Add(2*time.Minute)), } + // Every head is at the merge gate, so the rule judges them now. + for _, p := range prs { + c.noteMergeTrainOutcome(stageLaneSelf, "o", "r", p.GetNumber(), p.GetHead().GetSHA(), prstage.ContributorLaneReasonSoaking) + } plan := &mergeTrainPlan{lane: "self", displayRepo: "o/r"} kept := c.planDuplicateHolds(context.Background(), plan, stageLaneSelf, "o", "r", "o/r", prs, func(*gh.PullRequest) string { return "" }) if len(kept) != 2 || kept[0].GetNumber() != 17414 || kept[1].GetNumber() != 17415 { diff --git a/src/pkg/github/automerge/automerge_sweep_loop.go b/src/pkg/github/automerge/automerge_sweep_loop.go index 7094451fd9..9626c2f2a3 100644 --- a/src/pkg/github/automerge/automerge_sweep_loop.go +++ b/src/pkg/github/automerge/automerge_sweep_loop.go @@ -303,7 +303,7 @@ func (c *Engine) SweepSelfAuthoredAutoMerges(ctx context.Context, opts AutoMerge result.Candidates++ repoCandidates++ c.sweepClock.evaluating(repo, number) - event, reason, err := c.trySweepSelfAuthoredPR(withMergeTrainCandidateHold(ctx, c.mergeTrainCandidateReason(train, number)), repo, owner, repoName, number, branchUpdateAttempts < selfAuthoredSweepMaxBranchUpdates) + event, reason, err := c.trySweepSelfAuthoredPR(c.mergeTrainEvalContext(ctx, train, number), repo, owner, repoName, number, branchUpdateAttempts < selfAuthoredSweepMaxBranchUpdates) c.sweepClock.evaluated() if event.BranchUpdated || reason == "updated-branch" || reason == "update-branch" { branchUpdateAttempts++ @@ -498,7 +498,7 @@ func (c *Engine) SweepTrustedAuthorAutoMerges(ctx context.Context, opts AutoMerg } result.Candidates++ c.sweepClock.evaluating(displayRepo, number) - event, reason, err := c.trySweepTrustedAuthorPR(withMergeTrainCandidateHold(ctx, c.mergeTrainCandidateReason(train, number)), displayRepo, owner, repoName, number, policy) + event, reason, err := c.trySweepTrustedAuthorPR(c.mergeTrainEvalContext(ctx, train, number), displayRepo, owner, repoName, number, policy) c.sweepClock.evaluated() if err == nil { c.noteMergeTrainOutcome(stageLaneTrusted, owner, repoName, number, pr.GetHead().GetSHA(), reason)