diff --git a/changelog.d/added-automerge-merge-train.md b/changelog.d/added-automerge-merge-train.md new file mode 100644 index 0000000000..57398c3e7a --- /dev/null +++ b/changelog.d/added-automerge-merge-train.md @@ -0,0 +1 @@ +- Conflict-aware merge train in every automerge lane ([#15117](https://github.com/hivecommons/hive/issues/15117)): approved PRs to one base with overlapping changed files are grouped per sweep, one front merges at a time, siblings wait at stage/080 as `merge_gate.state=queued-behind` ("In merge train · 3rd of 4", never a human stage), and after the front merges the lane updates `behind` branches and asks the rebase-resolve hook for `dirty` ones (`auto_merge.merge_train.max_rebase_attempts`, default 2; past it: stage/085 "Needs human decision · rebase failed twice" with "Rebase, then merge as maintainer"). The Activity Monitor renders a train contiguously with a left rail and a `⛓ merge train · N PRs · front #X` header chip, and `/api/stage-tracker` items carry `merge_train{id,state,front,position,size,overlap_files,ahead}`. `auto_merge.merge_train.enabled` (default on) turns it off. diff --git a/src/cmd/hive/main.go b/src/cmd/hive/main.go index bef18159f9..efa2bcb315 100644 --- a/src/cmd/hive/main.go +++ b/src/cmd/hive/main.go @@ -2547,7 +2547,6 @@ func (b *boot) bootAgentsWith(deps bootAgentsDeps) { // auto_merge.trusted_bot_authors takes effect without a restart. autoMergeOpts.TrustedBotAuthors = func() map[string]bool { return b.cfg.AutoMerge.TrustedBotAuthorSet() } autoMergeOpts.BotTakeoverEnabled = func() bool { return b.cfg.AutoMerge.BotTakeover.IsEnabled() } - autoMergeOpts.MergeTrainEnabled = func() bool { return b.cfg.AutoMerge.MergeTrain.IsEnabled() } // Intent tier gate (#6258): the human lane only queues PRs that // survive writeMergeEligible's intent check, but this sweep lists // the App's PRs on its own, so it carries the same policy (same @@ -2571,6 +2570,15 @@ func (b *boot) bootAgentsWith(deps bootAgentsDeps) { autoMergeOpts.MergeForwardTrustedHuman = func(repo string) bool { return b.cfg != nil && b.cfg.AutoMerge.MergeForwardTrustedHumanFor(repo) } + // Merge train (#15117): read live so a reload applies. RebaseResolver + // stays nil until the rebase-then-merge follow-through (#15104) lands + // and is wired here; a conflicting front then keeps its conflicting path. + autoMergeOpts.MergeTrain = func() automerge.MergeTrainPolicy { + if b.cfg == nil { + return automerge.DefaultMergeTrainPolicy + } + return automerge.MergeTrainPolicy{Enabled: b.cfg.AutoMerge.MergeTrain.IsEnabled(), MaxRebaseAttempts: b.cfg.AutoMerge.MergeTrain.EffectiveMaxRebaseAttempts()} + } b.requestRelays = newRequestRelaySupervisor(b.ctx, func(ctx context.Context, client *github.Client) <-chan struct{} { laneGate := relayLanePolicyGate(autoMergeOpts)(client) setMergeEligibleLaneGate(laneGate) @@ -9409,7 +9417,9 @@ func runAutoMergeSweepIfDue(ctx context.Context, ghClient *github.Client, cfg *c opts.HumanMergePaths = func(repo string) []string { return humanMergePathsFor(cfg, repo) } opts.RequireCurrentBase = func(repo string) bool { return cfg.AutoMerge.RequireCurrentBaseFor(repo) } opts.MergeForwardTrustedHuman = func(repo string) bool { return cfg.AutoMerge.MergeForwardTrustedHumanFor(repo) } - opts.MergeTrainEnabled = func() bool { return cfg.AutoMerge.MergeTrain.IsEnabled() } + opts.MergeTrain = func() automerge.MergeTrainPolicy { + return automerge.MergeTrainPolicy{Enabled: cfg.AutoMerge.MergeTrain.IsEnabled(), MaxRebaseAttempts: cfg.AutoMerge.MergeTrain.EffectiveMaxRebaseAttempts()} + } opts.TrustedBotAuthors = func() map[string]bool { return cfg.AutoMerge.TrustedBotAuthorSet() } } audit := func(event automerge.AutoMergeSweepEvent) { diff --git a/src/docs/dashboard-sections.md b/src/docs/dashboard-sections.md index 3f595920d1..f33331d9be 100644 --- a/src/docs/dashboard-sections.md +++ b/src/docs/dashboard-sections.md @@ -664,6 +664,8 @@ Fed by `GET /api/stage-tracker`. Every issue and pull request follows the same p **Unauthorized pill.** A pull request the hive cannot move because its GitHub App lacks a permission for the next step — it wears `hive:unauthorized` ([#14589](https://github.com/hivecommons/hive/issues/14589)); first case: approving held `action_required` workflow runs needs `actions: write` — carries the `unauthorized` modifier in the tracker payload, drawn in the far-right pill column exactly like `stalled` / `hold` / `draft` but muted (dashed, `--text-muted`), never red: it is nothing wrong, just a step only a person may take. It counts under **Needs attention**. Before the merge gate only the held-workflow-runs step shows (see *Pills before the merge gate*). Its tooltip (payload `unauthorized`) names the missing permission and the human step; on the **Needs human decision** lane the lit chevron and the caption read **Awaiting human: approve workflow runs** (payload `unauthorized_step`, from the `unauthorized:` stage reason). The pill, label and payload fields clear themselves in the tick the step is no longer needed. +**Merge train.** When several approved PRs to the same base touch the same files, the automerge lanes merge them one at a time ([#15117](https://github.com/hivecommons/hive/issues/15117)). The tracker renders every train contiguously, in merge order, with a thin left rail linking the rows and a header chip `⛓ merge train · 4 PRs · front #15074` (overlapping files listed beside it). Pill wording per state: the front reads "Merging · front of train"; siblings "In merge train · 3rd of 4"; once the front merged and the hive is rebasing a conflicting sibling, "Rebasing · attempt 1/2"; when the loop guard (`auto_merge.merge_train.max_rebase_attempts`) is spent, "Needs human decision · rebase failed twice" at stage/085 with the decision option "Rebase, then merge as maintainer". The tooltip lists the PRs ahead and the overlapping files. Waiting siblings are never red, never `needs human`; a `needs-rebase` label on one is reported under `stale_labels`, the train repairs it. Each item carries `merge_gate.state` (`queued-behind` / `rebasing` / `rebase-exhausted` / `front`, with an honest `reason` such as `behind #15074 in merge train (3rd of 4, overlapping files: stage_tracker.go, index.html)`) and `merge_train{id, state, front, position, size, attempt, max_attempts, overlap_files, ahead}`; the id is `#`. + **Pills before the merge gate.** Owner's rule (2026-10-27): until a pull request is in the merge gate there is nothing a person can do about a `sentinel`, `needs human` or `unauthorized` notice — the PR is still in motion — so the tracker must not show them. The server splits every marker into two classes (`stageTrackerModifiers` in `stage_tracker.go`; each carries an explicit `ActionableNow` flag) and the row's payload carries the pills it draws in `modifiers` and the ones it holds back in `deferred_modifiers`. A deferred pill is simply absent — no chip, no breathing chevron, no **Needs attention** count — until the PR's stage reaches the merge-gate band (`stage/080` and later: 080, 085, 090, 095, 100, the same boundary adoption yields at), when it renders exactly as before and counts; the row tooltip alone says "Pending at merge gate: sentinel, needs-human". The merge-gate cell's own words ("Sentinel alert · needs human", "Awaiting human: …") are unchanged — they only appear at the gate anyway. Issues on their own stages (Open, 010, 020, 045) keep every pill: a `needs-human` there is a park the person owes now; an issue riding its covering PR's stage follows the PR. The GitHub labels are untouched — this is presentation and counting only. | pill | class | when shown | diff --git a/src/docs/operator-reference.md b/src/docs/operator-reference.md index b22e54eee3..1e8eba4cd9 100644 --- a/src/docs/operator-reference.md +++ b/src/docs/operator-reference.md @@ -431,10 +431,11 @@ advanced into the merge path. | `auto_merge.human_merge_paths` | unset | Per-repo map of `owner/repo` to glob patterns (same syntax as `intent.guardrail_path_patterns`) for paths a person must merge, e.g. `Danathar/goodreads-mcp: [".claude/settings.json", ".claude/hooks/**"]`. Every App merge path honors it: the automerge sweep lanes (App self-merge, trusted-author, label-queued) and the agent merge-request relay refuse a PR that touches a listed path, add `hold`, and post one `` comment naming the path(s); a person must merge it. If the changed-file list cannot be fetched completely for a configured repo, the merge is withheld (#11038, #11039). | | `auto_merge.require_current_base` | unset (off) | Repo list (`owner/repo`) whose App self-merge, trusted-author and contributor lanes only merge a head that contains the current base tip. A green head tested against an older base is updated with GitHub update-branch (a merge commit; no author commit is rewritten) and merges once CI is green on the new head, so a semantic conflict with work merged since turns that PR red instead of breaking the base (#13634). Costs one compare call per merge candidate. | | `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 are never written. Attempted once per head (#13636). | +| `auto_merge.merge_train.enabled` | `true` | 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 — the clean-mergeable PR approved earliest — while the others wait at stage/080 with lane reason `merge-train:queued-behind#:/:…` (tracker pill "In merge train · 3rd of 4", 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 rebase-resolve hook (the #15104 task; until it lands the dirty PR falls through to the plain `conflicting` path, logged once). 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. `false` restores independent per-PR evaluation. | +| `auto_merge.merge_train.max_rebase_attempts` | `2` | 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.squash_only` | unset (no repo) | Repo list (`owner/repo`) whose branch policy forbids merge commits. Elsewhere, a `role/trusted` PR whose head mixes the contributor's commits with App-linked, App-signed ones (the hive's own in-place adoption fixes), every commit self-signed, is labelled `merge-method/merge` and merged with a merge commit instead of parking at `stage/085` as `adoption-stalled:hive-signoff-attribution`; a listed repo keeps squash-only policy and re-homes an in-place adoption to an App-authored superseder instead (#13975). | | `auto_merge.credit_issue_authors` | `true` | Append `Co-authored-by: ` for the human authors of the issues a PR closes (`Fixes|Closes|Resolves #N`) to the squash (or merge) commit message on every hive-performed merge, so reporters land on the contributor graph when an external clanker fixed their issue. Bots, the PR author and logins already credited on the branch are skipped; the logins are recorded as `issue_authors=` on the merge audit entry and in review evidence. One cached `GET /issues/{n}` per newly referenced issue. `false` leaves GitHub's default squash message untouched (#14581). | | `auto_merge.bot_takeover.enabled` | `true` | When `github-actions[bot]` is in `auto_merge.trusted_bot_authors`, re-author its same-repo PRs whose required `pull_request` checks can never run (the bot opened them with a workflow `GITHUB_TOKEN`). After the head is older than both `min_head_age` and 30 minutes with no `pull_request` run, the App pushes the same change as one signed-off commit to `hive/takeover-`, opens a PR with the original title/body plus `Supersedes #`, comments `Superseded by #N` on the bot PR and closes it. One attempt per bot head SHA. See [hive-merge.md](hive-merge.md#github-actionsbot-pr-takeover). | -| `auto_merge.merge_train.enabled` | `true` | Merge train for approved PRs whose changed files overlap (#15141). The self-authored and trusted-author sweeps group candidate PRs per repo and base branch by overlapping changed files (`GET /pulls/{n}/files`, cached per head SHA) and let only one front per group merge per pass: a head not last seen conflicting, red or held first, then the oldest. Siblings skip as `merge-train-queued` and are retried against the new base on later passes; a PR whose files cannot be read stays out of every train. One INFO `automerge merge train` line per slot change. `false` merges every green PR in parallel as before. | | `auto_merge.trusted_authors.enabled` | `false` | Enables the opt-in human-authored PR tier: Hive may merge a green PR only when the author already holds the required hive role and, by default, GitHub push/maintain/admin permission on that repo. | | `auto_merge.trusted_authors.repos` | empty = all watched repos | Optional repo allow-list for the trusted-author tier. The dashboard renders this as a watched-repo multi-select; selecting nothing preserves the all-repos default. | | `auto_merge.trusted_authors.require_role` | `merger` | Minimum `dashboard.authorized_users` role the PR author must hold (`merger` or `owner`). | diff --git a/src/pkg/config/automerge_merge_train_test.go b/src/pkg/config/automerge_merge_train_test.go index bb6c1be167..d63ec89cef 100644 --- a/src/pkg/config/automerge_merge_train_test.go +++ b/src/pkg/config/automerge_merge_train_test.go @@ -2,24 +2,27 @@ package config import "testing" -func TestMergeTrainAutoMergeConfigDefaultsOn(t *testing.T) { +func TestMergeTrainAutoMergeConfigDefaults(t *testing.T) { off, on := false, true - cases := []struct { - name string - cfg MergeTrainAutoMergeConfig - want bool + tests := []struct { + name string + cfg MergeTrainAutoMergeConfig + wantEnabled bool + wantMax int }{ - {"unset", MergeTrainAutoMergeConfig{}, true}, - {"explicit on", MergeTrainAutoMergeConfig{Enabled: &on}, true}, - {"explicit off", MergeTrainAutoMergeConfig{Enabled: &off}, false}, + {"unset is on with the default guard", MergeTrainAutoMergeConfig{}, true, DefaultMergeTrainMaxRebaseAttempts}, + {"explicit on", MergeTrainAutoMergeConfig{Enabled: &on, MaxRebaseAttempts: 3}, true, 3}, + {"explicit off", MergeTrainAutoMergeConfig{Enabled: &off}, false, DefaultMergeTrainMaxRebaseAttempts}, + {"negative guard falls back", MergeTrainAutoMergeConfig{MaxRebaseAttempts: -1}, true, DefaultMergeTrainMaxRebaseAttempts}, } - for _, tc := range cases { - if got := tc.cfg.IsEnabled(); got != tc.want { - t.Errorf("%s: IsEnabled() = %v, want %v", tc.name, got, tc.want) - } - } - var a AutoMergeConfig - if !a.MergeTrain.IsEnabled() { - t.Fatal("zero AutoMergeConfig disables the merge train; want default on") + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + if got := tc.cfg.IsEnabled(); got != tc.wantEnabled { + t.Fatalf("IsEnabled = %v, want %v", got, tc.wantEnabled) + } + if got := tc.cfg.EffectiveMaxRebaseAttempts(); got != tc.wantMax { + t.Fatalf("EffectiveMaxRebaseAttempts = %d, want %d", got, tc.wantMax) + } + }) } } diff --git a/src/pkg/config/config.go b/src/pkg/config/config.go index 9d22b63112..b5601d0a03 100644 --- a/src/pkg/config/config.go +++ b/src/pkg/config/config.go @@ -7938,26 +7938,42 @@ type AutoMergeConfig struct { // open-time attribution. nil/unset means ON; `credit_issue_authors: // false` sends the merge with GitHub's default message as before. CreditIssueAuthors *bool `yaml:"credit_issue_authors,omitempty" json:"credit_issue_authors,omitempty"` - // MergeTrain orders approved PRs whose changed files overlap so they - // merge one at a time instead of dirtying each other - // (hivecommons/hive#15117). + // MergeTrain orders approved PRs on the same base whose changed files + // overlap: one front merges at a time, the siblings wait as + // "queued-behind" (stage 080, never a human stage) and are updated or + // rebased right after the front lands instead of each going + // `conflicting` with nothing owning the repair + // (hivecommons/hive#15117). Default ON. MergeTrain MergeTrainAutoMergeConfig `yaml:"merge_train,omitempty" json:"merge_train,omitempty"` } // MergeTrainAutoMergeConfig is auto_merge.merge_train.*. type MergeTrainAutoMergeConfig struct { - // Enabled groups each sweep's candidate PRs per repo and base branch by - // overlapping changed files and lets only one front per group merge per - // pass: the mergeable one first, then the oldest. nil/unset means ON; - // an explicit `enabled: false` merges every green PR as before. + // Enabled turns the train on. nil/unset means ON; `enabled: false` + // restores the independent per-PR evaluation. Enabled *bool `yaml:"enabled,omitempty" json:"enabled,omitempty"` + // MaxRebaseAttempts bounds how many rebase-resolve tasks the train + // asks for on one PR behind one front before it parks the PR for a + // person (stage 085). 0/unset means DefaultMergeTrainMaxRebaseAttempts. + MaxRebaseAttempts int `yaml:"max_rebase_attempts,omitempty" json:"max_rebase_attempts,omitempty"` } +// DefaultMergeTrainMaxRebaseAttempts is the merge train's rebase loop guard. +const DefaultMergeTrainMaxRebaseAttempts = 2 + // IsEnabled reports the auto_merge.merge_train.enabled switch; unset is on. func (m MergeTrainAutoMergeConfig) IsEnabled() bool { return m.Enabled == nil || *m.Enabled } +// EffectiveMaxRebaseAttempts is max_rebase_attempts with the default applied. +func (m MergeTrainAutoMergeConfig) EffectiveMaxRebaseAttempts() int { + if m.MaxRebaseAttempts <= 0 { + return DefaultMergeTrainMaxRebaseAttempts + } + return m.MaxRebaseAttempts +} + // CreditIssueAuthorsEnabled reports the auto_merge.credit_issue_authors // switch; unset is on. func (a AutoMergeConfig) CreditIssueAuthorsEnabled() bool { diff --git a/src/pkg/dashboard/settings_parity_allowlist.txt b/src/pkg/dashboard/settings_parity_allowlist.txt index c213dfca6b..efe94dbe4f 100644 --- a/src/pkg/dashboard/settings_parity_allowlist.txt +++ b/src/pkg/dashboard/settings_parity_allowlist.txt @@ -153,10 +153,11 @@ agents.session_recycle.max_age # TODO #11854 agents.session_recycle.max_rss_mb # TODO #11854 agents.session_recycle.min_idle # TODO #11854 auto_merge.credit_issue_authors # merge-time Co-authored-by credit (#14581); yaml-only -auto_merge.merge_train.enabled # merge-train front selection (#15141); yaml-only auto_merge.allow_unprotected_base[] # TODO #11854 auto_merge.human_merge_paths. # TODO #11854 auto_merge.merge_forward_trusted_human[] # TODO #11854 +auto_merge.merge_train.enabled # conflict-aware merge train (#15117); yaml-only +auto_merge.merge_train.max_rebase_attempts # merge train rebase loop guard (#15117); yaml-only auto_merge.no_ci_ok[] # TODO #11854 auto_merge.repos..required_checks[] # TODO #11854 auto_merge.require_current_base[] # TODO #11854 diff --git a/src/pkg/dashboard/stage_tracker.go b/src/pkg/dashboard/stage_tracker.go index 1aa86d53bc..7b5d4a1a7d 100644 --- a/src/pkg/dashboard/stage_tracker.go +++ b/src/pkg/dashboard/stage_tracker.go @@ -403,6 +403,10 @@ type StageTrackerItem struct { // label on its next pass, hivecommons/hive#14586). Rendered as a muted // operator tooltip, never as the stage word. StaleLabels []string `json:"stale_labels,omitempty"` + // MergeTrain is set on a PR riding a conflict-aware merge train + // (hivecommons/hive#15117): its position, the front, the PRs ahead and + // the overlapping files. See stage_tracker_merge_train.go. + MergeTrain *StageTrackerMergeTrain `json:"merge_train,omitempty"` // BlockedOn lists the open GitHub "blocked by" dependencies ("repo#N") // of an issue the scheduler withholds from every kick until they close // (scheduler.partitionBlockedIssues). Without it a sequenced sub-issue @@ -542,8 +546,9 @@ type StageTrackerLinkedPR struct { DecisionNeeded *StageTrackerDecision `json:"decision_needed,omitempty"` // SoakRemainingSeconds / MergeGate mirror the PR item's (#15073): a // nested Soaking sub-row shows its time left too. - SoakRemainingSeconds int64 `json:"soak_remaining_seconds,omitempty"` - MergeGate *StageTrackerMergeGate `json:"merge_gate,omitempty"` + SoakRemainingSeconds int64 `json:"soak_remaining_seconds,omitempty"` + MergeGate *StageTrackerMergeGate `json:"merge_gate,omitempty"` + MergeTrain *StageTrackerMergeTrain `json:"merge_train,omitempty"` // Adoption mirrors the nested PR's own row field (hivecommons/hive#14524). Adoption *StageTrackerAdoption `json:"adoption,omitempty"` // LastOperatorAction / OperatorActionCount mirror the nested PR's own @@ -1523,6 +1528,9 @@ func stageTrackerCaption(stage stagelabels.Stage, reason, author string, kind st } return joinClause(c, clause), "hive" case stagelabels.AwaitingMergeAdmission: + if c := stageTrackerMergeTrainCaption(reason); c != "" { + return c, "automerge lane" + } return joinClause("Green and approved; waiting for the automerge lane to admit it", clause), "automerge lane" case stagelabels.NeedsDecision, stagelabels.IssueNeedsDecision: switch stageTrackerAdoptedByState(rec) { @@ -1536,6 +1544,9 @@ func stageTrackerCaption(stage stagelabels.Stage, reason, author string, kind st // person may take; the gate cell says the same. return joinClause("Awaiting human: "+step, clause), "maintainer" } + if c := stageTrackerMergeTrainCaption(reason); c != "" { + return c, "maintainer" + } return joinClause("A human decision is owed", clause), "maintainer" case stagelabels.ReadyForHumanMerge: if base, approver := stagelabels.HumanApprovedReason(reason); stageTrackerManualMergeReason(base) { @@ -1802,6 +1813,7 @@ func (s *Server) buildStageTracker(status *StatusPayload, store *stagelabels.Sto } it.Modifiers = slices.DeleteFunc(it.Modifiers, func(m string) bool { return m == modNeedsTriage }) } + stageTrackerApplyMergeTrain(it) it.Caption, it.Actor = stageTrackerCaption(stage, it.Reason, it.Author, it.Kind, it.Mirrored, rec, hasRec, it.LinkedPR, it.Head) // Adoption is clanker-only (a human's PR is never adopted), so a // human-authored 072 row keeps its plain caption. @@ -2005,6 +2017,7 @@ func (s *Server) buildStageTracker(status *StatusPayload, store *stagelabels.Sto } } resp.OpenIssues, resp.OpenPRs = stageTrackerOpenCounts(resp.Items) + stageTrackerMergeTrainPostPass(resp.Items) s.applyOperatorActions(resp.Items, now) resp.NeedsHuman, resp.OnHold = stageTrackerHumanCounts(resp.Items) stageTrackerResolveLinkedPRs(resp.Items) @@ -2740,6 +2753,7 @@ func stageTrackerResolveLinkedPRs(items []StageTrackerItem) { lp.BaseRef = pr.BaseRef lp.DecisionNeeded = pr.DecisionNeeded lp.SoakRemainingSeconds, lp.MergeGate = pr.SoakRemainingSeconds, pr.MergeGate + lp.MergeTrain = pr.MergeTrain lp.LastOperatorAction, lp.OperatorActionCount = pr.LastOperatorAction, pr.OperatorActionCount lp.Hold = pr.Hold if len(pr.Modifiers) > 0 { diff --git a/src/pkg/dashboard/stage_tracker_decision.go b/src/pkg/dashboard/stage_tracker_decision.go index 4085b6a6c4..8b919b2996 100644 --- a/src/pkg/dashboard/stage_tracker_decision.go +++ b/src/pkg/dashboard/stage_tracker_decision.go @@ -149,6 +149,8 @@ func stageTrackerDecisionNeeded(it StageTrackerItem, verdict *review.Aggregate, func stageTrackerDecisionNeededAt(it StageTrackerItem, verdict *review.Aggregate, sentinelLabel string, now time.Time) *StageTrackerDecision { held := slices.Contains(it.Modifiers, modHold) switch { + case it.Kind == "pr" && it.MergeTrain != nil && it.MergeTrain.State == MergeGateStateRebaseExhausted: + return decisionForMergeTrain(it, held) case it.Kind == "pr" && (it.Stage == stagelabels.NeedsDecision.Number() || slices.Contains(it.Modifiers, modNeedsDecision)): return decisionForPR(it, verdict, sentinelLabel, held) case it.Kind == "pr" && it.Stage == stagelabels.ReadyForHumanMerge.Number(): diff --git a/src/pkg/dashboard/stage_tracker_merge_train.go b/src/pkg/dashboard/stage_tracker_merge_train.go new file mode 100644 index 0000000000..6fa204a18a --- /dev/null +++ b/src/pkg/dashboard/stage_tracker_merge_train.go @@ -0,0 +1,232 @@ +package dashboard + +import ( + "slices" + "sort" + "strconv" + "strings" + + "github.com/hivecommons/hive/pkg/stageengine/prstage" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// Merge-gate states a PR riding a merge train reports (hivecommons/hive#15117). +// They extend the pipeline states in stage_tracker_merge_gate.go: a train +// member at 080 is not "ready" — it is waiting its turn behind the front. +const ( + // MergeGateStateQueuedBehind: a sibling parked behind the train's front. + MergeGateStateQueuedBehind = prstage.MergeTrainStateQueuedBehind + // MergeGateStateRebasing: the front merged and this PR's rebase was + // requested (attempt n of max). + MergeGateStateRebasing = prstage.MergeTrainStateRebasing + // MergeGateStateRebaseExhausted: the rebase failed max times; a person + // rebases and merges. + MergeGateStateRebaseExhausted = prstage.MergeTrainStateRebaseExhausted + // MergeGateStateTrainFront: the PR leads its train and is merging first. + MergeGateStateTrainFront = prstage.MergeTrainStateFront +) + +// StageTrackerMergeTrain is the row's place in a conflict-aware merge +// train: PRs to one base whose changed files overlap, merged one front at +// a time (automerge_merge_train.go). The Activity Monitor draws the +// train's rows contiguous in merge order under one header and words the +// pill from State ("In merge train · 3rd of 4"). +type StageTrackerMergeTrain struct { + // ID names the train across its members: "#". + ID string `json:"id"` + // State is one of front, queued-behind, rebasing, rebase-exhausted. + State string `json:"state"` + // Front is the PR merging first; Position is this PR's 1-based slot + // of Size in merge order. + Front int `json:"front"` + Position int `json:"position"` + Size int `json:"size"` + // Attempt / MaxAttempts count the rebase loop guard when State is + // rebasing or rebase-exhausted. + Attempt int `json:"attempt,omitempty"` + MaxAttempts int `json:"max_attempts,omitempty"` + // OverlapFiles are the changed-file basenames the train's members + // share (the reason the PRs cannot merge in parallel). + OverlapFiles []string `json:"overlap_files,omitempty"` + // Ahead lists the PRs still to merge before this one, in order. + Ahead []int `json:"ahead,omitempty"` +} + +// stageTrackerMergedStage is the terminal strip number (the dashboard's +// STAGE_TRACKER_TERMINAL); no open item carries it. +const stageTrackerMergedStage = 110 + +// stageTrackerMergeTrainFrom reads the train the stage reason encodes, or +// nil when the reason is not a train reason. +func stageTrackerMergeTrainFrom(reason string) *StageTrackerMergeTrain { + mt, ok := prstage.ParseMergeTrain(reason) + if !ok { + return nil + } + return &StageTrackerMergeTrain{ + State: mt.State, + Front: mt.Front, + Position: mt.Position, + Size: mt.Size, + Attempt: mt.Attempt, + MaxAttempts: mt.MaxAttempts, + OverlapFiles: append([]string(nil), mt.OverlapFiles...), + } +} + +// stageTrackerApplyMergeTrain decorates a PR row the lane parked in a +// train: the merge-gate state becomes the train state with the sentence +// as reason, and a `needs-rebase` label stops reading as a human flag — +// the train is repairing it (it stays in StaleLabels so the hover still +// names it). The exhausted state keeps the label: that row is a person's. +func stageTrackerApplyMergeTrain(it *StageTrackerItem) { + if it == nil || it.Kind != "pr" { + return + } + mt := stageTrackerMergeTrainFrom(it.Reason) + if mt == nil { + return + } + mt.ID = it.BaseRef + "#" + strconv.Itoa(mt.Front) + it.MergeTrain = mt + parsed, _ := prstage.ParseMergeTrain(it.Reason) + if it.MergeGate == nil { + it.MergeGate = &StageTrackerMergeGate{} + } + it.MergeGate.State = mt.State + it.MergeGate.Reason = parsed.Sentence() + if mt.State != MergeGateStateRebaseExhausted && slices.Contains(it.Modifiers, modNeedsRebase) { + it.Modifiers = slices.DeleteFunc(it.Modifiers, func(m string) bool { return m == modNeedsRebase }) + if !slices.Contains(it.StaleLabels, modNeedsRebase) { + it.StaleLabels = append(it.StaleLabels, modNeedsRebase) + } + } +} + +// stageTrackerMergeTrainPostPass runs over the finished items: the front +// (which the lane evaluates on its normal path and so carries no train +// reason) is marked from its siblings' records, and every member learns +// the PRs ahead of it. Members are matched on repo and base. +func stageTrackerMergeTrainPostPass(items []StageTrackerItem) { + type trainKey struct{ repo, id string } + members := map[trainKey][]int{} + for i := range items { + it := &items[i] + if it.MergeTrain == nil || it.Kind != "pr" { + continue + } + members[trainKey{it.Repo, it.MergeTrain.ID}] = append(members[trainKey{it.Repo, it.MergeTrain.ID}], i) + } + for key, idx := range members { + sample := items[idx[0]].MergeTrain + frontIdx := -1 + for i := range items { + it := &items[i] + if it.Kind == "pr" && it.Repo == key.repo && it.Number == sample.Front && it.Stage < stageTrackerMergedStage { + frontIdx = i + break + } + } + if frontIdx >= 0 && items[frontIdx].MergeTrain == nil { + front := &items[frontIdx] + front.MergeTrain = &StageTrackerMergeTrain{ + ID: sample.ID, State: MergeGateStateTrainFront, Front: sample.Front, Position: 1, Size: sample.Size, + OverlapFiles: append([]string(nil), sample.OverlapFiles...), + } + if front.MergeGate == nil { + front.MergeGate = &StageTrackerMergeGate{} + } + if front.MergeGate.State == "" || front.MergeGate.State == MergeGateStateReady { + front.MergeGate.State = MergeGateStateTrainFront + } + if front.MergeGate.Reason == "" { + front.MergeGate.Reason = "front of merge train (1st of " + strconv.Itoa(sample.Size) + ", overlapping files: " + strings.Join(sample.OverlapFiles, ", ") + ")" + } + if front.Stage == stagelabels.AwaitingMergeAdmission.Number() { + front.Caption = stageTrackerMergeTrainFrontCaption(sample) + } + idx = append(idx, frontIdx) + } + // Members in merge order, so each one's Ahead is the prefix. + sort.Slice(idx, func(a, b int) bool { + return items[idx[a]].MergeTrain.Position < items[idx[b]].MergeTrain.Position + }) + for n, i := range idx { + var ahead []int + for _, j := range idx[:n] { + ahead = append(ahead, items[j].Number) + } + items[i].MergeTrain.Ahead = ahead + } + } +} + +// stageTrackerMergeTrainFrontCaption is the 080 caption of a train's front. +func stageTrackerMergeTrainFrontCaption(mt *StageTrackerMergeTrain) string { + files := "" + if len(mt.OverlapFiles) > 0 { + files = " (overlapping files: " + strings.Join(mt.OverlapFiles, ", ") + ")" + } + return "Front of a merge train of " + strconv.Itoa(mt.Size) + " PRs; merges first, then the lane updates or rebases the others" + files +} + +// stageTrackerMergeTrainCaption is the caption for a sibling's train +// reason at 080 / 085 ("" when the reason is not a train reason). +func stageTrackerMergeTrainCaption(reason string) string { + mt, ok := prstage.ParseMergeTrain(reason) + if !ok { + return "" + } + onto := "the base" + if mt.Front > 0 { + onto = "the merged front #" + strconv.Itoa(mt.Front) + } + switch mt.State { + case prstage.MergeTrainStateRebasing: + return "Conflicts with " + onto + "; the hive is rebasing this PR (attempt " + strconv.Itoa(mt.Attempt) + " of " + strconv.Itoa(mt.MaxAttempts) + ")" + case prstage.MergeTrainStateRebaseExhausted: + return "The rebase onto " + onto + " failed " + strconv.Itoa(mt.Attempt) + " times; a maintainer rebases and merges" + default: + return "Approved; waiting its turn in the merge train — " + mt.Sentence() + } +} + +// decisionForMergeTrain: the train gave up rebasing this PR after the loop +// guard (hivecommons/hive#15117). The lead option is the rebase-then-merge +// follow-through; until that follow-through (hivecommons/hive#15104) is +// wired the option is offered disabled with the honest reason, and the +// row menu's usual decision options remain. +func decisionForMergeTrain(it StageTrackerItem, held bool) *StageTrackerDecision { + mt := it.MergeTrain + d := &StageTrackerDecision{Since: it.StageSince, AskedBy: decisionAskedByLane, BlockerReason: it.Reason} + onto := "the base" + if mt.Front > 0 { + onto = "the merged front #" + strconv.Itoa(mt.Front) + } + d.Question = "The merge train could not rebase this PR onto " + onto + " after " + strconv.Itoa(mt.Attempt) + " attempts (overlapping files: " + strings.Join(mt.OverlapFiles, ", ") + "). Rebase it by hand and merge as maintainer, or close it?" + d.Blocker = "The rebase failed " + strconv.Itoa(mt.Attempt) + " of " + strconv.Itoa(mt.MaxAttempts) + " times; the train does not retry past the loop guard." + if held { + d.Blocker += " " + decisionHeldBlocker(it.Hold) + } + mergeWhy := decisionWhyMergeNowUnwired + if held { + mergeWhy = decisionWhyHeld + } + d.Options = []StageTrackerDecisionOption{ + decisionOption(decisionActionMergeNow, rebaseMergeFollowThroughLabel, mergeNowWired && !held, mergeWhy), + decisionOption(decisionActionCloseSuperseded, "Close as superseded", !held, decisionWhyHeld), + decisionHoldOption(held), + } + return d +} + +// rebaseMergeFollowThroughLabel is the option's wording. +const rebaseMergeFollowThroughLabel = "Rebase, then merge as maintainer" + +// mergeNowWired flips true once decide:merge-now rebases a conflicting head +// before merging (hivecommons/hive#15104). The #15087 handler merges a +// mergeable head only, so on a rebase-exhausted (dirty) PR the option is +// offered disabled with the honest reason. +var mergeNowWired = false + +const decisionWhyMergeNowUnwired = "Rebase-then-merge is not wired in this hive yet (#15104); rebase the branch on GitHub, then approve & merge as maintainer" diff --git a/src/pkg/dashboard/stage_tracker_merge_train_test.go b/src/pkg/dashboard/stage_tracker_merge_train_test.go new file mode 100644 index 0000000000..c090d65355 --- /dev/null +++ b/src/pkg/dashboard/stage_tracker_merge_train_test.go @@ -0,0 +1,304 @@ +package dashboard + +import ( + "encoding/json" + "os/exec" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/stageengine/prstage" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// TestStageTrackerMergeTrainPayload: a train of four PRs to v5 whose lane +// records are the merge-train reasons (hivecommons/hive#15117). The front +// carries no train reason of its own (the lane evaluates it on its normal +// path) and is marked from its siblings; siblings read queued-behind with +// the PRs ahead; a rebasing sibling and an exhausted one carry the attempt +// counters, and only the exhausted one is a person's row. +func TestStageTrackerMergeTrainPayload(t *testing.T) { + now := time.Date(2026, 10, 27, 14, 0, 0, 0, time.UTC) + s, store := stageTrackerFixture(t, now) + overlap := []string{"stage_tracker.go", "index.html"} + queued := func(pos int) string { + return prstage.MergeTrain{State: prstage.MergeTrainStateQueuedBehind, Front: 15074, Position: pos, Size: 4, OverlapFiles: overlap}.Reason() + } + store.Observe(stagelabels.Key("acme/console", 15074), stagelabels.AwaitingMergeAdmission, "") + store.Observe(stagelabels.Key("acme/console", 15085), stagelabels.AwaitingMergeAdmission, queued(2)) + store.Observe(stagelabels.Key("acme/console", 15087), stagelabels.AwaitingMergeAdmission, prstage.MergeTrain{State: prstage.MergeTrainStateRebasing, Front: 15074, Position: 3, Size: 4, Attempt: 1, MaxAttempts: 2, OverlapFiles: overlap}.Reason()) + store.Observe(stagelabels.Key("acme/console", 15097), stagelabels.NeedsDecision, prstage.MergeTrain{State: prstage.MergeTrainStateRebaseExhausted, Front: 15074, Position: 4, Size: 4, Attempt: 2, MaxAttempts: 2, OverlapFiles: overlap}.Reason()) + s.statusMu.Lock() + for _, n := range []int{15074, 15085, 15087, 15097} { + stage := "stage/080-awaiting-merge-admission" + if n == 15097 { + stage = "stage/085-needs-decision" + } + labels := []string{stage, "author/hive"} + if n != 15074 { + labels = append(labels, "needs-rebase") + } + s.status.Repos[0].OpenPrs = append(s.status.Repos[0].OpenPrs, + FrontendPR{PullRequest: github.PullRequest{Number: n, Title: "Tracker change", Author: "hive-app[bot]", AppAuthored: true, BaseRef: "v5", Labels: labels}}) + } + s.statusMu.Unlock() + resp := s.buildStageTracker(s.status, store, now) + + front := trackerItem(t, resp, "pr", 15074) + if front.MergeTrain == nil || front.MergeTrain.State != MergeGateStateTrainFront || front.MergeTrain.Position != 1 || front.MergeTrain.Size != 4 || front.MergeTrain.ID != "v5#15074" { + t.Fatalf("front merge_train = %+v", front.MergeTrain) + } + if front.MergeGate == nil || front.MergeGate.State != MergeGateStateTrainFront || !strings.Contains(front.Caption, "Front of a merge train of 4 PRs") { + t.Fatalf("front gate %+v caption %q", front.MergeGate, front.Caption) + } + if hasMod(front, modNeedsRebase) || len(front.MergeTrain.Ahead) != 0 { + t.Fatalf("front must not wear needs-rebase nor have PRs ahead: %+v", front) + } + + sib := trackerItem(t, resp, "pr", 15085) + if sib.Stage != 80 || sib.MergeTrain == nil || sib.MergeTrain.State != MergeGateStateQueuedBehind || sib.MergeTrain.Position != 2 || sib.MergeTrain.Front != 15074 { + t.Fatalf("sibling = stage %d train %+v", sib.Stage, sib.MergeTrain) + } + if sib.MergeGate == nil || sib.MergeGate.State != "queued-behind" || sib.MergeGate.Reason != "behind #15074 in merge train (2nd of 4, overlapping files: stage_tracker.go, index.html)" { + t.Fatalf("sibling merge_gate = %+v", sib.MergeGate) + } + if hasMod(sib, modNeedsRebase) || hasMod(sib, modNeedsHuman) || !hasStale(sib, modNeedsRebase) { + t.Fatalf("sibling modifiers %v stale %v: needs-rebase is the train's to repair, not a human flag", sib.Modifiers, sib.StaleLabels) + } + if got := sib.MergeTrain.Ahead; len(got) != 1 || got[0] != 15074 { + t.Fatalf("ahead = %v, want [15074]", got) + } + if !strings.HasPrefix(sib.Caption, "Approved; waiting its turn in the merge train — behind #15074") || sib.Actor != "automerge lane" { + t.Fatalf("sibling caption %q actor %q", sib.Caption, sib.Actor) + } + if sib.DecisionNeeded != nil { + t.Fatalf("a queued sibling owes no decision: %+v", sib.DecisionNeeded) + } + + rebasing := trackerItem(t, resp, "pr", 15087) + if rebasing.MergeTrain.State != MergeGateStateRebasing || rebasing.MergeTrain.Attempt != 1 || rebasing.MergeTrain.MaxAttempts != 2 || !strings.Contains(rebasing.Caption, "rebasing this PR (attempt 1 of 2)") { + t.Fatalf("rebasing = %+v caption %q", rebasing.MergeTrain, rebasing.Caption) + } + if got := rebasing.MergeTrain.Ahead; len(got) != 2 || got[0] != 15074 || got[1] != 15085 { + t.Fatalf("rebasing ahead = %v", got) + } + + exhausted := trackerItem(t, resp, "pr", 15097) + if exhausted.Stage != 85 || exhausted.MergeTrain.State != MergeGateStateRebaseExhausted || !hasMod(exhausted, modNeedsRebase) { + t.Fatalf("exhausted = stage %d train %+v mods %v", exhausted.Stage, exhausted.MergeTrain, exhausted.Modifiers) + } + if !strings.Contains(exhausted.Caption, "failed 2 times; a maintainer rebases and merges") || exhausted.Actor != "maintainer" { + t.Fatalf("exhausted caption %q actor %q", exhausted.Caption, exhausted.Actor) + } + d := exhausted.DecisionNeeded + if d == nil || len(d.Options) == 0 || d.Options[0].Action != decisionActionMergeNow || d.Options[0].Label != rebaseMergeFollowThroughLabel { + t.Fatalf("exhausted decision = %+v", d) + } + if d.Options[0].Enabled || d.Options[0].ReasonIfDisabled != decisionWhyMergeNowUnwired { + t.Fatalf("merge-now must be offered disabled with the honest reason until #15104 lands: %+v", d.Options[0]) + } + if !strings.Contains(d.Question, "could not rebase this PR onto the merged front #15074 after 2 attempts") { + t.Fatalf("question = %q", d.Question) + } + + raw, err := json.Marshal(sib) + if err != nil { + t.Fatal(err) + } + for _, want := range []string{`"merge_gate":{"state":"queued-behind","reason":"behind #15074 in merge train (2nd of 4, overlapping files: stage_tracker.go, index.html)"`, + `"merge_train":{"id":"v5#15074","state":"queued-behind","front":15074,"position":2,"size":4,"overlap_files":["stage_tracker.go","index.html"],"ahead":[15074]}`} { + if !strings.Contains(string(raw), want) { + t.Fatalf("payload lacks %s:\n%s", want, raw) + } + } + // Rows outside the train are untouched. + if plain := trackerItem(t, resp, "pr", 102); plain.MergeTrain != nil { + t.Fatalf("PR 102 is not in a train: %+v", plain.MergeTrain) + } + // A dirty front the train is rebasing carries its own reason and is not + // re-marked. + store.Observe(stagelabels.Key("acme/console", 15074), stagelabels.AwaitingMergeAdmission, prstage.MergeTrain{State: prstage.MergeTrainStateRebasing, Front: 15074, Position: 1, Size: 4, Attempt: 1, MaxAttempts: 2, OverlapFiles: overlap}.Reason()) + resp = s.buildStageTracker(s.status, store, now) + if f := trackerItem(t, resp, "pr", 15074); f.MergeTrain.State != MergeGateStateRebasing || f.MergeTrain.Position != 1 { + t.Fatalf("rebasing front = %+v", f.MergeTrain) + } +} + +func hasStale(it StageTrackerItem, want string) bool { + for _, x := range it.StaleLabels { + if x == want { + return true + } + } + return false +} + +// TestStageTrackerMergeTrainCaptions pins the sentence per state. +func TestStageTrackerMergeTrainCaptions(t *testing.T) { + overlap := []string{"a.go"} + cases := []struct { + reason string + want string + }{ + {prstage.MergeTrain{State: prstage.MergeTrainStateQueuedBehind, Front: 7, Position: 3, Size: 4, OverlapFiles: overlap}.Reason(), "Approved; waiting its turn in the merge train — behind #7 in merge train (3rd of 4, overlapping files: a.go)"}, + {prstage.MergeTrain{State: prstage.MergeTrainStateRebasing, Front: 0, Position: 1, Size: 2, Attempt: 2, MaxAttempts: 2}.Reason(), "Conflicts with the base; the hive is rebasing this PR (attempt 2 of 2)"}, + {prstage.MergeTrain{State: prstage.MergeTrainStateRebaseExhausted, Front: 7, Position: 2, Size: 2, Attempt: 2, MaxAttempts: 2}.Reason(), "The rebase onto the merged front #7 failed 2 times; a maintainer rebases and merges"}, + {"check-pending", ""}, + } + for _, tc := range cases { + if got := stageTrackerMergeTrainCaption(tc.reason); got != tc.want { + t.Errorf("caption(%q) = %q, want %q", tc.reason, got, tc.want) + } + } + if stageTrackerMergeTrainFrom("held") != nil { + t.Fatal("a plain reason is no train") + } + var nilItem *StageTrackerItem + stageTrackerApplyMergeTrain(nilItem) + stageTrackerApplyMergeTrain(&StageTrackerItem{Kind: "issue", Reason: cases[0].reason}) + if mergeNowWired { + t.Fatal("merge-now is not wired until hivecommons/hive#15104 lands") + } +} + +// TestStageTrackerMergeTrainSPA runs the SPA's train helpers under node: +// the pill wording per state, the contiguous ordering with the header line +// and rail classes, and the tooltip's queue + files. +func TestStageTrackerMergeTrainSPA(t *testing.T) { + html := indexHTML(t) + for _, css := range []string{".ham-train-head {", ".ham-row.ham-train {", ".ham-row.ham-train-last::before {", ".ham-train-chip {"} { + if !strings.Contains(html, css) { + t.Fatalf("stylesheet lacks %q", css) + } + } + node, err := exec.LookPath("node") + if err != nil { + t.Skip("node not on PATH — the merge-train rendering was NOT executed by this run") + } + script := strings.Join([]string{ + "const assert = require('assert');", + "function esc(s) { return String(s).replace(/&/g, '&').replace(/ Object.assign({ repo: 'hivecommons/hive', number: n, kind: 'pr', title: 'PR ' + n, stage, stage_since: '2026-10-27T13:00:00Z', base_ref: 'v5', modifiers: [], merge_train: Object.assign({ id: 'v5#15074', front: 15074, size: 4, overlap_files: files }, mt) }, extra || {}); +const front = mk(15074, 80, { state: 'front', position: 1 }); +const second = mk(15085, 80, { state: 'queued-behind', position: 2, ahead: [15074] }); +const third = mk(15087, 80, { state: 'rebasing', position: 3, attempt: 1, max_attempts: 2, ahead: [15074, 15085] }); +const fourth = mk(15097, 85, { state: 'rebase-exhausted', position: 4, attempt: 2, max_attempts: 2, ahead: [15074, 15085, 15087] }, { modifiers: ['needs-rebase'] }); +const other = { repo: 'hivecommons/hive', number: 15100, kind: 'pr', title: 'Docs', stage: 80, modifiers: [] }; + +// Pill wording per state. +assert.strictEqual(stageTrackerTrainWord(front), 'Merging · front of train'); +assert.strictEqual(stageTrackerTrainWord(second), 'In merge train · 2nd of 4'); +assert.strictEqual(stageTrackerTrainWord(third), 'Rebasing · attempt 1/2'); +assert.strictEqual(stageTrackerTrainWord(fourth), 'Needs human decision · rebase failed twice'); +assert.strictEqual(stageTrackerTrainWord(other), ''); +for (const [n, w] of [[1, '1st'], [2, '2nd'], [3, '3rd'], [4, '4th'], [11, '11th'], [12, '12th'], [13, '13th'], [21, '21st'], [22, '22nd'], [111, '111th']]) assert.strictEqual(stageTrackerOrdinal(n), w, 'ordinal ' + n); +// The lit chevron carries the word: 080 for the members, the 085 detour for the exhausted one. +const lit = it => stageTrackerSegments(it, stages, now).find(s => s.state === 'active'); +assert.strictEqual(lit(front).short, 'Merging · front of train'); +assert.strictEqual(lit(second).short, 'In merge train · 2nd of 4'); +assert.strictEqual(lit(third).short, 'Rebasing · attempt 1/2'); +assert.strictEqual(lit(fourth).detour.text, 'Needs human decision · rebase failed twice'); +assert.strictEqual(lit(other).short, 'Ready for auto merge'); + +// Tooltip: the queue ahead and the overlapping files. +const tip = stageTrackerTooltip(third, now); +assert.ok(tip.includes('Merge train: 4 PRs to v5 touch the same files; they merge one at a time, front first (front #15074)'), tip); +assert.ok(tip.includes('Position: 3rd of 4 · ahead: #15074, #15085'), tip); +assert.ok(tip.includes('Front merged; rebase attempt 1 of 2 in progress'), tip); +assert.ok(tip.includes('Overlapping files: stage_tracker.go, index.html'), tip); +assert.ok(stageTrackerTooltip(front, now).includes('Position: front — merges first'), 'front tip'); +assert.ok(stageTrackerTooltip(fourth, now).includes('Rebase failed 2 of 2 times — a maintainer rebases, then merges')); +assert.ok(!stageTrackerTooltip(other, now).includes('Merge train')); + +// Contiguity: a sorted list that interleaves the train is regrouped at the +// first member's slot, in merge order; other rows keep their order. +const sorted = [fourth, other, third, { repo: 'x/y', number: 1, kind: 'issue', stage: 50 }, front, second]; +const grouped = stageTrackerTrainContiguous(sorted).map(it => it.number); +assert.deepStrictEqual(grouped, [15074, 15085, 15087, 15097, 15100, 1]); +// Header line before the run, once; rail classes on members, last closes it. +const rows = stageTrackerTrainRows(stageTrackerTrainContiguous(sorted), (it) => ({ key: String(it.number), html: '' })); +assert.deepStrictEqual(rows.map(r => r.key), ['train:hivecommons/hive|v5#15074', '15074', '15085', '15087', '15097', '15100', '1']); +assert.ok(rows[0].html.includes('⛓ merge train · 4 PRs · front #15074'), rows[0].html); +assert.ok(rows[0].html.includes('class="ham-train-chip"') && rows[0].html.includes('overlapping: stage_tracker.go, index.html'), rows[0].html); +assert.strictEqual(stageTrackerTrainHeaderText(front.merge_train), '⛓ merge train · 4 PRs · front #15074'); +assert.strictEqual(stageTrackerTrainRowClass(front, null, second), ' ham-train ham-train-front'); +assert.strictEqual(stageTrackerTrainRowClass(second, front, third), ' ham-train'); +assert.strictEqual(stageTrackerTrainRowClass(fourth, third, other), ' ham-train ham-train-last'); +assert.strictEqual(stageTrackerTrainRowClass(other, fourth, null), ''); +// The grouped view's {group}/{item} wrappers are read through. +const wrapped = stageTrackerTrainRows([{ group: { key: 'g' } }, { group: { key: 'g' }, item: front }, { group: { key: 'g' }, item: second }], (e) => ({ key: e.item ? String(e.item.number) : e.group.key, html: '' })); +assert.deepStrictEqual(wrapped.map(r => r.key), ['g', 'train:hivecommons/hive|v5#15074', '15074', '15085']); +` + out, err := exec.Command(node, "-e", script).CombinedOutput() + if err != nil { + t.Fatalf("node: %v\n%s", err, out) + } +} diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 9e0c333fe5..1cf2518119 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -1482,6 +1482,19 @@ busiest column). */ .ham-group { position: sticky; top: 0; z-index: var(--z-sticky); display: flex; align-items: center; gap: var(--sp-3); padding: var(--sp-2) var(--sp-2); margin-top: var(--sp-3); border-top: var(--line-width) solid var(--line-subtle); border-bottom: var(--line-width) solid var(--line-subtle); background: var(--panel); font-size: var(--fs-sm); color: var(--text); min-width: 0; } .ham-cols + .ham-group { margin-top: 0; border-top: 0; } + /* Merge train (hivecommons/hive issue 15117): members sit contiguous in merge + order under one header line, joined by a thin rail on the left. Muted, + hairline weights: the rail says "these move together", nothing more. */ + .ham-train-head { display: flex; align-items: center; gap: var(--sp-2); padding: var(--sp-2) var(--sp-2) 0 var(--sp-5); margin-top: var(--sp-2); font-size: var(--fs-xs); color: var(--muted); min-width: 0; position: relative; } + .ham-train-head::before { content: ''; position: absolute; left: calc(var(--sp-2) + 2px); top: 50%; bottom: 0; border-left: var(--line-width) solid var(--line-strong); } + .ham-train-chip { display: inline-flex; align-items: center; gap: 0.4em; padding: 0 0.6em; border: var(--line-width) solid var(--line-strong); border-radius: var(--r-pill); line-height: 1.6; color: var(--text); font-size: var(--fs-xs); white-space: nowrap; } + .ham-train-files { color: var(--text-faint); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } + .ham-row.ham-train { padding-left: var(--sp-5); position: relative; } + .ham-row.ham-train::before { content: ''; position: absolute; left: calc(var(--sp-2) + 2px); top: 0; bottom: 0; border-left: var(--line-width) solid var(--line-strong); } + .ham-row.ham-train-last::before { bottom: 50%; } + .ham-row.ham-train::after { content: ''; position: absolute; left: calc(var(--sp-2) - 1px); top: calc(50% - 3px); width: 6px; height: 6px; border-radius: var(--r-pill); background: var(--line-strong); } + .ham-row.ham-train-front::after { background: var(--text); } + .ham-train-head + .ham-row.ham-train { border-top: 0; } .ham-group-toggle { flex: 0 0 auto; display: inline-flex; align-items: center; justify-content: center; width: 1.4em; height: 1.4em; padding: 0; border: 0; border-radius: var(--r-sm); background: transparent; color: var(--muted); font: inherit; cursor: pointer; } .ham-group-toggle:hover { color: var(--text); background: color-mix(in srgb, var(--text) 8%, transparent); } .ham-group-toggle:focus-visible { outline: var(--focus-ring-width) solid var(--line-strong); outline-offset: 1px; } @@ -14107,6 +14120,114 @@

Where to get help

}; const STAGE_TRACKER_MOTION_AUTHOR_STAGES = [30, 40]; const STAGE_TRACKER_MOTION_HUMAN_MODS = ['stalled', 'blocked', 'needs-human', 'needs-decision', 'needs-triage', 'unauthorized']; + /* ---- merge train (hivecommons/hive#15117) ------------------------------ + Pure helpers; stage_tracker_merge_train_ui_test.go runs them under node. + A PR's merge_train names its train (id, state, front, position/size, + the PRs ahead, the overlapping files). The pill wording is the state's, + the rows of one train are drawn contiguous in merge order under one + header, and the tooltip lists the queue. */ + const STAGE_TRACKER_TRAIN_STATE_FRONT = 'front'; + const STAGE_TRACKER_TRAIN_STATE_QUEUED = 'queued-behind'; + const STAGE_TRACKER_TRAIN_STATE_REBASING = 'rebasing'; + const STAGE_TRACKER_TRAIN_STATE_EXHAUSTED = 'rebase-exhausted'; + const STAGE_TRACKER_TRAIN_GLYPH = '⛓'; + function stageTrackerTrain(item) { + const mt = item && item.merge_train; + return mt && typeof mt === 'object' && mt.state ? mt : null; + } + function stageTrackerOrdinal(n) { + n = Number(n) || 0; + const r100 = n % 100, r10 = n % 10; + if (r100 >= 11 && r100 <= 13) return n + 'th'; + return n + (r10 === 1 ? 'st' : r10 === 2 ? 'nd' : r10 === 3 ? 'rd' : 'th'); + } + // The chevron word for a train member ('' for a row not in a train). + function stageTrackerTrainWord(item) { + const mt = stageTrackerTrain(item); + if (!mt) return ''; + switch (mt.state) { + case STAGE_TRACKER_TRAIN_STATE_FRONT: return 'Merging · front of train'; + case STAGE_TRACKER_TRAIN_STATE_QUEUED: return 'In merge train · ' + stageTrackerOrdinal(mt.position) + ' of ' + (Number(mt.size) || 0); + case STAGE_TRACKER_TRAIN_STATE_REBASING: return 'Rebasing · attempt ' + (Number(mt.attempt) || 0) + '/' + (Number(mt.max_attempts) || 0); + case STAGE_TRACKER_TRAIN_STATE_EXHAUSTED: return 'Needs human decision · rebase failed ' + (Number(mt.attempt) === 2 ? 'twice' : (Number(mt.attempt) || 0) + ' times'); + } + return ''; + } + // Tooltip lines: the queue ahead and the overlapping files. + function stageTrackerTrainLines(item) { + const mt = stageTrackerTrain(item); + if (!mt) return []; + const ahead = Array.isArray(mt.ahead) ? mt.ahead.filter(n => Number(n) > 0) : []; + const files = Array.isArray(mt.overlap_files) ? mt.overlap_files.filter(Boolean) : []; + const lines = ['Merge train: ' + (Number(mt.size) || 0) + ' PRs to ' + (String(mt.id || '').split('#')[0] || 'the same base') + ' touch the same files; they merge one at a time, front first' + (mt.front ? ' (front #' + mt.front + ')' : '')]; + if (mt.state === STAGE_TRACKER_TRAIN_STATE_FRONT) lines.push('Position: front — merges first; the lane then updates or rebases the others'); + else lines.push('Position: ' + stageTrackerOrdinal(mt.position) + ' of ' + (Number(mt.size) || 0) + (ahead.length ? ' · ahead: ' + ahead.map(n => '#' + n).join(', ') : '')); + if (mt.state === STAGE_TRACKER_TRAIN_STATE_REBASING) lines.push('Front merged; rebase attempt ' + (Number(mt.attempt) || 0) + ' of ' + (Number(mt.max_attempts) || 0) + ' in progress'); + if (mt.state === STAGE_TRACKER_TRAIN_STATE_EXHAUSTED) lines.push('Rebase failed ' + (Number(mt.attempt) || 0) + ' of ' + (Number(mt.max_attempts) || 0) + ' times — a maintainer rebases, then merges'); + if (files.length) lines.push('Overlapping files: ' + files.join(', ')); + return lines; + } + function stageTrackerTrainKey(item) { + const mt = stageTrackerTrain(item); + return mt ? (item.repo || '') + '|' + (mt.id || ('#' + mt.front)) : ''; + } + // Pure: re-orders a sorted row list so each train's members sit together + // at the first member's slot, in merge order (front first). + function stageTrackerTrainContiguous(rows) { + rows = Array.isArray(rows) ? rows : []; + const byTrain = {}; + rows.forEach(it => { const k = stageTrackerTrainKey(it); if (k) (byTrain[k] = byTrain[k] || []).push(it); }); + const emitted = {}; + const out = []; + rows.forEach(it => { + const k = stageTrackerTrainKey(it); + if (!k) { out.push(it); return; } + if (emitted[k]) return; + emitted[k] = true; + byTrain[k].slice().sort((a, b) => (Number(a.merge_train.position) || 0) - (Number(b.merge_train.position) || 0) || ((a.number || 0) - (b.number || 0))).forEach(m => out.push(m)); + }); + return out; + } + // Header line over a train's rows: "⛓ merge train · 4 PRs · front #15074". + function stageTrackerTrainHeaderText(mt) { + return STAGE_TRACKER_TRAIN_GLYPH + ' merge train · ' + (Number(mt.size) || 0) + ' PRs · front #' + (Number(mt.front) || 0); + } + function stageTrackerTrainHeaderHtml(item) { + const mt = stageTrackerTrain(item); + if (!mt) return ''; + const files = Array.isArray(mt.overlap_files) ? mt.overlap_files.filter(Boolean) : []; + const tip = stageTrackerTrainLines(item).join('\n'); + return `
+ ${esc(stageTrackerTrainHeaderText(mt))}${files.length ? `overlapping: ${esc(files.join(', '))}` : ''} +
`; + } + // Row classes for a train member: the rail, and whether it leads / ends + // the run (prev/next are the neighbouring rows, or null). + function stageTrackerTrainRowClass(item, prev, next) { + const k = stageTrackerTrainKey(item); + if (!k) return ''; + const mt = stageTrackerTrain(item); + let cls = ' ham-train'; + if (mt.state === STAGE_TRACKER_TRAIN_STATE_FRONT) cls += ' ham-train-front'; + if (!next || stageTrackerTrainKey(next) !== k) cls += ' ham-train-last'; + return cls; + } + // Inserts a train header entry before the first row of each train run. + // Entries are {key, html}; items are the page's row items in order. + function stageTrackerTrainRows(items, rowOf) { + const out = []; + let prevKey = ''; + (items || []).forEach((e, i) => { + // Entries are items, or the grouped view's {item} / {group} wrappers. + const it = e && e.item ? e.item : (e && e.group ? null : e); + const k = it ? stageTrackerTrainKey(it) : ''; + if (k && k !== prevKey) out.push({ key: 'train:' + k, html: stageTrackerTrainHeaderHtml(it) }); + prevKey = k; + out.push(rowOf(e, i)); + }); + return out; + } + function stageTrackerMotion(item) { const mods = Array.isArray(item && item.modifiers) ? item.modifiers : []; if (mods.indexOf('hold') >= 0) return STAGE_TRACKER_MOTION_STATIC; @@ -14632,7 +14753,7 @@

Where to get help

const terminal = anchor >= STAGE_TRACKER_TERMINAL; const sentinelLane = stageTrackerSentinelInLane(item); const detour = (!terminal && STAGE_TRACKER_PATH.indexOf(cur) < 0 && cur !== STAGE_TRACKER_TRIAGE_NEEDED && cur !== STAGE_TRACKER_POSSIBLY_EXISTS && cur !== 40 && cur !== 60 && cur !== STAGE_TRACKER_PRE_MERGE_SOAK && cur !== 100) - ? { text: sentinelLane ? STAGE_TRACKER_SENTINEL_WORD : (cur === STAGE_TRACKER_READY_FOR_HUMAN_MERGE && item.manual_merge ? stageTrackerManualMergeWord(item) : (cur === STAGE_TRACKER_HUMAN_GATED && stageTrackerUnauthorizedWord(item)) || ((cur === STAGE_TRACKER_ADOPTED_BY_HIVE || (cur === STAGE_TRACKER_HUMAN_GATED && item.adopted_by_state)) && stageTrackerAdoptedWord(item)) || (cur === STAGE_TRACKER_READY_FOR_HUMAN_MERGE && stageTrackerReadyForHumanWord(item, STAGE_TRACKER_ACTIVE_WORD[cur] || curStage.short || curStage.label || String(cur))) || (STAGE_TRACKER_ACTIVE_WORD[cur] || curStage.short || curStage.label || String(cur))), sentinel: sentinelLane } : null; + ? { text: sentinelLane ? STAGE_TRACKER_SENTINEL_WORD : (cur === STAGE_TRACKER_HUMAN_GATED && typeof stageTrackerTrainWord === 'function' && stageTrackerTrainWord(item)) || (cur === STAGE_TRACKER_READY_FOR_HUMAN_MERGE && item.manual_merge ? stageTrackerManualMergeWord(item) : (cur === STAGE_TRACKER_HUMAN_GATED && stageTrackerUnauthorizedWord(item)) || ((cur === STAGE_TRACKER_ADOPTED_BY_HIVE || (cur === STAGE_TRACKER_HUMAN_GATED && item.adopted_by_state)) && stageTrackerAdoptedWord(item)) || (cur === STAGE_TRACKER_READY_FOR_HUMAN_MERGE && stageTrackerReadyForHumanWord(item, STAGE_TRACKER_ACTIVE_WORD[cur] || curStage.short || curStage.label || String(cur))) || (STAGE_TRACKER_ACTIVE_WORD[cur] || curStage.short || curStage.label || String(cur))), sentinel: sentinelLane } : null; if (detour && cur === 70 && !detour.sentinel && typeof stageTrackerFixingWord === 'function') detour.text = stageTrackerFixingWord(item, detour.text); const segs = STAGE_TRACKER_PATH.map(n => { const st = n === 0 ? { label: 'Open', short: 'Open', description: 'Open, not yet in a stage' } : (byNum[n] || {}); @@ -14646,6 +14767,8 @@

Where to get help

// read as 080 with "Next move: human"). const tipStage = (state === 'active' && curStage.label) ? curStage : st; let word = state === 'active' && STAGE_TRACKER_ACTIVE_WORD[cur] ? STAGE_TRACKER_ACTIVE_WORD[cur] : (st.short || st.label || String(n)); + // A merge-train member's word is its place in the train (#15117). + if (state === 'active' && !detour && n > 0 && typeof stageTrackerTrainWord === 'function') { const tw = stageTrackerTrainWord(item); if (tw) word = tw; } const mods = Array.isArray(item.modifiers) ? item.modifiers : []; const openHold = n === 0 && state === 'active' && mods.indexOf('hold') >= 0; // hold is a human instruction, so it wins over blocked when both are present. @@ -15002,6 +15125,7 @@

Where to get help

if (typeof stageTrackerNewLines === 'function') lines.push(...stageTrackerNewLines(item, nowMs)); const blockedPR = mods.indexOf('blocked') >= 0 ? stageTrackerBlockedOnPR(item) : null; if (blockedPR) lines.push(blockedPR.tip); + if (typeof stageTrackerTrainLines === 'function') lines.push(...stageTrackerTrainLines(item)); // Guarded: the test harnesses load helpers by name, and this one is additive. const adoption = typeof stageTrackerAdoptionTip === 'function' ? stageTrackerAdoptionTip(item) : ''; if (adoption) lines.push('Adoption: ' + adoption + (typeof stageTrackerAdoptOffered === 'function' && stageTrackerAdoptOffered(item) ? ' (row menu → Adopt)' : '')); @@ -15338,7 +15462,7 @@

Where to get help

stage: lp.stage || 0, stage_since: lp.stage_since || '', stage_slug: lp.stage_slug || '', modifiers: lp.modifiers || [], deferred_modifiers: lp.deferred_modifiers || [], caption: lp.caption || '', reason: lp.reason || '', human_approver: lp.human_approver || '', manual_merge: !!lp.manual_merge, human_merge_reason: lp.human_merge_reason || '', human_merge_reason_text: lp.human_merge_reason_text || '', sentinel_alert: !!lp.sentinel_alert, sentinel_reason: lp.sentinel_reason || '', head: lp.head || '', base_ref: lp.base_ref || '', decision_needed: lp.decision_needed || null, adoption: lp.adoption || null, superseded: !!lp.superseded, parent: Number(issue.number) || 0, siblings: open.map(x => Number(x.number) || 0), - last_operator_action: lp.last_operator_action || null, operator_action_count: lp.operator_action_count || 0, hold: lp.hold || null, stage_actor: lp.stage_actor || null, presence: lp.presence || null }; + last_operator_action: lp.last_operator_action || null, operator_action_count: lp.operator_action_count || 0, hold: lp.hold || null, stage_actor: lp.stage_actor || null, presence: lp.presence || null, merge_gate: lp.merge_gate || null, merge_train: lp.merge_train || null }; } // Pure: whether a linked PR is still open (not merged / closed / superseded). function stageTrackerLinkedPROpen(lp) { @@ -15570,7 +15694,7 @@

Where to get help

const who = (lp.author_kind || 'hive') + (lp.author ? ' · @' + lp.author : ''); prRefs = [STAGE_TRACKER_LINKED_GLYPH + stageTrackerPRRef(lp.number, lp.url, lp.author_kind || '', lp.author, lp.title, (lp.title ? lp.title + '\n' : '') + 'Author: ' + who + ' · ' + stageTrackerLinkedTip(item, lp.number, ctx)) + (lp.base_ref ? ` ${esc(lp.base_ref)}` : '')]; shown = Object.assign({}, item, { via_pr: true, stage: lp.stage || 40, stage_since: lp.stage_since || item.stage_since, modifiers: lp.modifiers || item.modifiers, deferred_modifiers: lp.deferred_modifiers || item.deferred_modifiers || [], caption: lp.caption || item.caption, reason: lp.reason || item.reason, - human_approver: lp.human_approver || '', manual_merge: !!lp.manual_merge, human_merge_reason: lp.human_merge_reason || '', human_merge_reason_text: lp.human_merge_reason_text || '', sentinel_alert: !!lp.sentinel_alert, sentinel_reason: lp.sentinel_reason || '', head: lp.head || '', decision_needed: lp.decision_needed || null, hold: lp.hold || null, adoption: lp.adoption || null, stage_actor: lp.stage_actor || item.stage_actor || null, presence: lp.presence || null }); + human_approver: lp.human_approver || '', manual_merge: !!lp.manual_merge, human_merge_reason: lp.human_merge_reason || '', human_merge_reason_text: lp.human_merge_reason_text || '', sentinel_alert: !!lp.sentinel_alert, sentinel_reason: lp.sentinel_reason || '', head: lp.head || '', decision_needed: lp.decision_needed || null, hold: lp.hold || null, adoption: lp.adoption || null, stage_actor: lp.stage_actor || item.stage_actor || null, presence: lp.presence || null, merge_gate: lp.merge_gate || null, merge_train: lp.merge_train || null }); } else if (linked.length) { // Several covering PRs: the issue strip ends at Claimed, all passed // and nothing lit; one sub-row per PR carries the live chevron. @@ -15631,7 +15755,8 @@

Where to get help

`; } const prs = typeof stageTrackerRowPRKeys === 'function' ? stageTrackerRowPRKeys(item) : ''; - return `
+ const trainKey = typeof stageTrackerTrainKey === 'function' ? stageTrackerTrainKey(item) : ''; + return `
${stageTrackerRowToolsHtml(item)}
${stageTrackerHeadColsHtml(prRefs, leadHtml, stageTrackerActedBadgeHtml(item, nowMs) + stageTrackerChipsHtml(shown))} @@ -16756,7 +16881,7 @@

All labels

// Repo chips ride the setting against how many repos the payload spans. if (stageTrackerRepoChipOn(stageTrackerRepoChipSetting(), repos.length)) ctx.repoChips = { primary: stageTrackerPrimaryRepo(), active: f.repo }; const visible = stageTrackerVisible(d.items, f, ctx); - const ordered = stageTrackerSort(visible, stages, f); + const ordered = stageTrackerTrainContiguous(stageTrackerSort(visible, stages, f)); const grouping = _stageTrackerGroup === 'repo'; const page = grouping ? stageTrackerGroupPage(stageTrackerGroups(ordered, { primary: stageTrackerPrimaryRepo(), collapsed: _stageTrackerCollapsed, stages }), f) @@ -16808,11 +16933,18 @@

All labels

// unchanged, collapsed out with their last row). // A row celebrating its merge is pinned past the payload that // dropped it (stageTrackerPinnedRows) until its moment is over. + // A merge train's rows carry a header line and a rail (#15117); the + // row renderer sees its neighbours to close the rail on the last one. const pageItems = stageTrackerAdoptOrphanPRs(page.rows.map(e => grouping ? e.item : e)); + const itemRow = (it, i) => { + const near = Object.assign({}, ctx, { prevRow: i > 0 && pageItems[i - 1] ? pageItems[i - 1] : null, nextRow: pageItems[i + 1] ? pageItems[i + 1] : null }); + return { key: stageTrackerItemKey(it), html: stageTrackerRowHtml(it, stages, near) }; + }; + const rowOf = (e, i) => grouping && !e.item + ? { key: e.group.key, html: stageTrackerGroupHeaderHtml(e.group, stages, ctx) } + : itemRow(pageItems[i], i); const rows = stageTrackerPinnedRows(el, [{ key: STAGE_TRACKER_HEADER_KEY, html: stageTrackerHeaderHtml(stages) }] - .concat(page.rows.map((e, i) => grouping && !e.item - ? { key: e.group.key, html: stageTrackerGroupHeaderHtml(e.group, stages, ctx) } - : { key: stageTrackerItemKey(pageItems[i]), html: stageTrackerRowHtml(pageItems[i], stages, ctx) })), nowMs); + .concat(stageTrackerTrainRows(page.rows, rowOf)), nowMs); const rec = stageTrackerReconcile(el, rows, { effect, hidden: document.hidden }); if (rec.deferred) stageTrackerScheduleSettle(); // Selection is keyed on the row, never baked into its HTML, so a kept diff --git a/src/pkg/github/automerge/automerge_contributor_lane.go b/src/pkg/github/automerge/automerge_contributor_lane.go index 7f3f091d40..08b77e72de 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane.go +++ b/src/pkg/github/automerge/automerge_contributor_lane.go @@ -417,6 +417,9 @@ func (c *Engine) SweepContributorLaneAutoMerges(ctx context.Context, opts AutoMe c.info("contributor-lane automerge sweep skipped repo", "repo", displayRepo, "reason", skipReason) continue } + train := c.planMergeTrains(ctx, stageLaneContributor, displayRepo, owner, repoName, prs, func(pr *gh.PullRequest) string { + return c.prefilterContributorLanePR(pr, policy) + }) for _, pr := range prs { if len(result.Merged) >= maxMerges { break @@ -429,6 +432,17 @@ func (c *Engine) SweepContributorLaneAutoMerges(ctx context.Context, opts AutoMe stage := func(reason string, merged bool, err error) { c.recordStage(stageLaneContributor, displayRepo, owner, repoName, pr, reason, merged, err) } + if train.isSibling(number) { + result.Skipped++ + stage(c.mergeTrainSkip(train, number), false, nil) + continue + } + if reason, handled := c.mergeTrainFront(ctx, train, pr); handled { + c.recordContributorLaneSkip(displayRepo, number, reason) + result.Skipped++ + stage(reason, false, nil) + continue + } if reason := c.prefilterContributorLanePR(pr, policy); reason != "" { c.recordContributorLaneSkip(displayRepo, number, reason) result.Skipped++ @@ -437,6 +451,9 @@ func (c *Engine) SweepContributorLaneAutoMerges(ctx context.Context, opts AutoMe } result.Candidates++ event, reason, err := c.trySweepContributorLanePR(ctx, displayRepo, owner, repoName, number, policy) + if err == nil { + c.noteMergeTrainOutcome(owner, repoName, number, pr.GetHead().GetSHA(), reason) + } if err != nil { c.warn("contributor-lane automerge sweep skipped PR", "repo", displayRepo, "pr", number, "reason", reason, "error", err) result.Skipped++ @@ -454,6 +471,9 @@ func (c *Engine) SweepContributorLaneAutoMerges(ctx context.Context, opts AutoMe if opts.Audit != nil { opts.Audit(event) } + if train.isFront(number) { + c.mergeTrainAfterFrontMerge(ctx, train, number) + } } } return result, nil diff --git a/src/pkg/github/automerge/automerge_merge_train.go b/src/pkg/github/automerge/automerge_merge_train.go index 1e8df9cead..2ff376c83e 100644 --- a/src/pkg/github/automerge/automerge_merge_train.go +++ b/src/pkg/github/automerge/automerge_merge_train.go @@ -2,6 +2,8 @@ package automerge import ( "context" + "fmt" + "net/http" "sort" "strconv" "strings" @@ -9,301 +11,717 @@ import ( "time" gh "github.com/google/go-github/v72/github" + hgithub "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/stageengine/prstage" +) + +// Merge train (hivecommons/hive#15117). +// +// Six approved PRs to v5 all touched stage_tracker.go and index.html; the +// moment one merged the rest went `dirty`, and every lane logged +// `conflicting` / `label:needs-rebase` each cycle while a person stacked +// branches by hand. The train makes the lane order them instead: +// +// - candidates on the same base whose changed files overlap form one +// train (GET /pulls/{n}/files, cached by head SHA); +// - the train has one front — clean-mergeable first, then oldest — that +// the lane evaluates and merges as before; every sibling is parked as +// `merge-train:queued-behind#:/:…` (stage 080, never a +// human stage); +// - right after the front merges each sibling is read back: `behind` gets +// update-branch, `dirty` gets a rebase-resolve request through the +// RebaseResolver hook, at most MaxRebaseAttempts per PR per front; a +// front that stays dirty past the guard parks at 085 for a person. +// +// Train state (attempt counters, last merged front, transition log +// signatures) is in-memory per process; a restart simply re-plans. + +// MergeTrainPolicy is the live auto_merge.merge_train.* policy. +type MergeTrainPolicy struct { + Enabled bool + MaxRebaseAttempts int +} + +// DefaultMergeTrainPolicy is the policy without an Options.MergeTrain hook: +// on, two rebase attempts per PR per front (config.DefaultMergeTrainMaxRebaseAttempts). +var DefaultMergeTrainPolicy = MergeTrainPolicy{Enabled: true, MaxRebaseAttempts: 2} + +// RebaseRequest names the PR the merge train wants rebased onto its base. +type RebaseRequest struct { + // Repo is owner/repo; Number the PR; Base and Head the branch names. + Repo string + Number int + Base string + Head string + // HeadSHA is the head the train saw conflicting. + HeadSHA string + // Front is the PR whose merge made this head conflict (0 when the + // front itself is the conflicting one at the start of a train). + Front int + // Attempt (1-based) of MaxAttempts for this PR behind this front. + Attempt int + MaxAttempts int + // Lane is the sweep lane that owns the PR (prstage.StageLane*). + Lane string +} + +// RebaseResolver enqueues a rebase-resolve task for a conflicting PR. The +// rebase-then-merge follow-through (hivecommons/hive#15104, pkg/rebasemerge) +// is the intended implementation: cmd/hive adapts its store here so the +// fixer lane rebases the head and the dashboard ticker merges it on green. +// TODO(hivecommons/hive#15104): wire pkg/rebasemerge.Store as the resolver +// once it lands; until then a nil resolver leaves a dirty front on its +// normal `conflicting` path and logs the gap once per PR head. +type RebaseResolver interface { + RequestRebase(ctx context.Context, req RebaseRequest) error +} + +// RebaseResolverFunc adapts a function to RebaseResolver. +type RebaseResolverFunc func(ctx context.Context, req RebaseRequest) error + +// RequestRebase implements RebaseResolver. +func (f RebaseResolverFunc) RequestRebase(ctx context.Context, req RebaseRequest) error { + return f(ctx, req) +} + +// Tick-stat buckets for the train's park reasons (the full reason is per +// PR and carries the position). +const ( + autoMergeReasonMergeTrainQueued = "merge-train-queued" + autoMergeReasonMergeTrainRebasing = "merge-train-rebasing" ) -// autoMergeReasonMergeTrainQueued is the skip reason of an approved PR that -// shares changed files with another candidate on the same base and is not -// that group's merge-train front (hivecommons/hive#15117). Merging every -// overlapping PR in parallel dirties all but the first; the train merges one -// front per group and leaves the rest for the next pass against the new base. -const autoMergeReasonMergeTrainQueued = "merge-train-queued" +// mergeTrainFilesPages bounds the /files pagination per PR (100 per page). +const mergeTrainFilesPages = 3 -// mergeTrainMember is one candidate PR as the train planner sees it. +// mergeTrainMember is one candidate the planner groups. type mergeTrainMember struct { - Number int - Base string - Created time.Time - // Blocked marks a head the sweep last saw unable to merge on its own - // (conflicting, red, or a terminal hold): it never fronts a train while a - // mergeable member exists, so it cannot stall its siblings. + Number int + Base string + Head string + HeadSHA string + Files []string + CreatedAt time.Time + MergeableState string + Fork bool + // Blocked marks a head the lane last saw unable to merge on its own + // (red required check, terminal hold, permanent merge refusal): it + // does not front a train, and a group whose best member is blocked + // imposes no ordering at all (fail-open), so nobody waits behind a PR + // that is not moving. Blocked bool - Files []string } -// mergeTrainSlot is a PR's place in its overlap group. Position 1 is the -// front; OverlapFiles are the member's files that another member also changes. -type mergeTrainSlot struct { - Front int - Position int - Size int +// mergeTrainGroup is one train: the ordered members (front first) and the +// files at least two of them touch. +type mergeTrainGroup struct { + Base string + Order []int OverlapFiles []string + members map[int]mergeTrainMember } -// planMergeTrains groups members per base branch by transitively overlapping -// changed files and orders each group of two or more: mergeable before -// blocked, then oldest, then lowest number. A group whose best member is -// itself blocked gets no slots, so a train never holds back siblings behind a -// front that cannot merge. -func planMergeTrains(members []mergeTrainMember) map[int]mergeTrainSlot { - parent := make([]int, len(members)) - for i := range parent { - parent[i] = i - } - var find func(int) int - find = func(i int) int { - if parent[i] != i { - parent[i] = find(parent[i]) - } - return parent[i] - } - owner := make(map[string]int) - for i, m := range members { - for _, f := range m.Files { - key := m.Base + "\x00" + f - if j, ok := owner[key]; ok { - parent[find(i)] = find(j) - continue - } - owner[key] = i +// Front is the PR the train merges next. +func (g mergeTrainGroup) Front() int { + if len(g.Order) == 0 { + return 0 + } + return g.Order[0] +} + +// Position is the 1-based slot of number in the train (0 when absent). +func (g mergeTrainGroup) Position(number int) int { + for i, n := range g.Order { + if n == number { + return i + 1 } } - groups := make(map[int][]int) - for i := range members { - root := find(i) - groups[root] = append(groups[root], i) + return 0 +} + +// Siblings are the members behind the front, in order. +func (g mergeTrainGroup) Siblings() []int { + if len(g.Order) <= 1 { + return nil } - slots := make(map[int]mergeTrainSlot) - for _, group := range groups { - if len(group) < 2 { + return g.Order[1:] +} + +func (g mergeTrainGroup) signature() string { + parts := make([]string, len(g.Order)) + for i, n := range g.Order { + parts[i] = strconv.Itoa(n) + } + return g.Base + ":" + strings.Join(parts, ",") +} + +// mergeTrainStateRank orders members for the front: a head GitHub says +// merges cleanly leads, an unknown state next, then behind, then dirty. +func mergeTrainStateRank(state string) int { + switch strings.ToLower(strings.TrimSpace(state)) { + case "clean", "unstable", "has_hooks", "blocked": + return 0 + case "", "unknown": + return 1 + case "behind": + return 2 + } + return 3 +} + +// mergeTrainDirty reports whether a mergeable state is a conflict. +func mergeTrainDirty(state string) bool { + s := strings.ToLower(strings.TrimSpace(state)) + return s == "dirty" || s == "conflicting" +} + +// groupMergeTrains groups members per base by overlapping files (union of +// pairwise overlaps) and orders each group front-first. Groups of one are +// dropped: a PR with nothing to collide with is no train. +func groupMergeTrains(members []mergeTrainMember) []mergeTrainGroup { + byBase := map[string][]mergeTrainMember{} + bases := []string{} + for _, m := range members { + if m.Number <= 0 || len(m.Files) == 0 { continue } - sort.Slice(group, func(a, b int) bool { - ma, mb := members[group[a]], members[group[b]] - if ma.Blocked != mb.Blocked { - return !ma.Blocked + if _, ok := byBase[m.Base]; !ok { + bases = append(bases, m.Base) + } + byBase[m.Base] = append(byBase[m.Base], m) + } + sort.Strings(bases) + var out []mergeTrainGroup + for _, base := range bases { + list := byBase[base] + parent := make([]int, len(list)) + for i := range parent { + parent[i] = i + } + var find func(int) int + find = func(i int) int { + for parent[i] != i { + parent[i] = parent[parent[i]] + i = parent[i] } - if !ma.Created.IsZero() && !mb.Created.IsZero() && !ma.Created.Equal(mb.Created) { - return ma.Created.Before(mb.Created) + return i + } + owner := map[string]int{} + shared := map[string]bool{} + for i, m := range list { + seen := map[string]bool{} + for _, f := range m.Files { + f = strings.TrimSpace(f) + if f == "" || seen[f] { + continue + } + seen[f] = true + if j, ok := owner[f]; ok { + shared[f] = true + parent[find(i)] = find(j) + } else { + owner[f] = i + } } - return ma.Number < mb.Number - }) - front := members[group[0]] - if front.Blocked { - continue } - touches := make(map[string]int) - for _, i := range group { - for _, f := range uniqueStrings(members[i].Files) { - touches[f]++ + groups := map[int][]mergeTrainMember{} + roots := []int{} + for i, m := range list { + r := find(i) + if _, ok := groups[r]; !ok { + roots = append(roots, r) } + groups[r] = append(groups[r], m) } - for pos, i := range group { - var overlap []string - for _, f := range uniqueStrings(members[i].Files) { - if touches[f] > 1 { - overlap = append(overlap, f) + sort.Ints(roots) + for _, r := range roots { + ms := groups[r] + if len(ms) < 2 { + continue + } + sort.SliceStable(ms, func(a, b int) bool { + if ms[a].Blocked != ms[b].Blocked { + return !ms[a].Blocked + } + ra, rb := mergeTrainStateRank(ms[a].MergeableState), mergeTrainStateRank(ms[b].MergeableState) + if ra != rb { + return ra < rb + } + if !ms[a].CreatedAt.Equal(ms[b].CreatedAt) { + if ms[a].CreatedAt.IsZero() != ms[b].CreatedAt.IsZero() { + return !ms[a].CreatedAt.IsZero() + } + return ms[a].CreatedAt.Before(ms[b].CreatedAt) } + return ms[a].Number < ms[b].Number + }) + if ms[0].Blocked { + continue } - sort.Strings(overlap) - slots[members[i].Number] = mergeTrainSlot{Front: front.Number, Position: pos + 1, Size: len(group), OverlapFiles: overlap} - } - } - return slots -} - -func uniqueStrings(in []string) []string { - seen := make(map[string]bool, len(in)) - out := make([]string, 0, len(in)) - for _, s := range in { - if s == "" || seen[s] { - continue + g := mergeTrainGroup{Base: base, members: map[int]mergeTrainMember{}} + count := map[string]int{} + for _, m := range ms { + g.Order = append(g.Order, m.Number) + g.members[m.Number] = m + seen := map[string]bool{} + for _, f := range m.Files { + if f = strings.TrimSpace(f); f != "" && !seen[f] { + seen[f] = true + count[f]++ + } + } + } + for f, n := range count { + if n >= 2 && shared[f] { + g.OverlapFiles = append(g.OverlapFiles, f) + } + } + sort.Strings(g.OverlapFiles) + out = append(out, g) } - seen[s] = true - out = append(out, s) } return out } -// mergeTrainState is the engine's per-process train memory: changed files per -// PR cached by head SHA, heads last seen blocked, and the slot last logged per -// PR so a transition logs once rather than every pass. -type mergeTrainState struct { - mu sync.Mutex - files map[string]mergeTrainFiles - blocked map[string]string - logged map[string]string +// mergeTrainPlan is one lane's trains for one repository this pass. +type mergeTrainPlan struct { + lane, displayRepo, owner, repo string + groups []mergeTrainGroup + byNumber map[int]*mergeTrainGroup } -type mergeTrainFiles struct { - sha string - files []string +// group returns the train number rides in, if any. +func (p *mergeTrainPlan) group(number int) (*mergeTrainGroup, bool) { + if p == nil || p.byNumber == nil { + return nil, false + } + g, ok := p.byNumber[number] + return g, ok } -func (c *Engine) mergeTrainEnabled() bool { - return c.mergeTrainEnabledFn == nil || c.mergeTrainEnabledFn() +// isSibling reports whether number waits behind a front this pass. +func (p *mergeTrainPlan) isSibling(number int) bool { + g, ok := p.group(number) + return ok && g.Front() != number } -// mergeTrainBlockingReason reports whether a lane's skip reason means the head -// cannot merge without someone changing it, so it must not front a train. -func mergeTrainBlockingReason(reason string) bool { - switch reason { - case "conflicting", "not-mergeable", autoMergeReasonForkNotEditable, autoMergeReasonClosedSuperseded: - return true - } - return isTerminalRequiredCheckHold(reason) || isPermanentMergeRefusal(reason) || classifyCIReason(reason) == ciReasonRed +// isFront reports whether number leads a train this pass. +func (p *mergeTrainPlan) isFront(number int) bool { + g, ok := p.group(number) + return ok && g.Front() == number } -// noteMergeTrainOutcome records whether the lane's verdict for a head blocks -// it from fronting a train. A merged or non-blocking verdict clears the mark. -func (c *Engine) noteMergeTrainOutcome(owner, repo string, number int, sha, reason string) { - if c == nil || number <= 0 { - return - } - key := c.evaluatedHeadKey(owner, repo, number) - c.mergeTrain.mu.Lock() - defer c.mergeTrain.mu.Unlock() - if !mergeTrainBlockingReason(reason) || strings.TrimSpace(sha) == "" { - delete(c.mergeTrain.blocked, key) - return +// mergeTrainPolicyNow reads the live policy. +func (c *Engine) mergeTrainPolicyNow() MergeTrainPolicy { + if c == nil || c.mergeTrainPolicy == nil { + return DefaultMergeTrainPolicy } - if c.mergeTrain.blocked == nil { - c.mergeTrain.blocked = make(map[string]string) + p := c.mergeTrainPolicy() + if p.MaxRebaseAttempts <= 0 { + p.MaxRebaseAttempts = DefaultMergeTrainPolicy.MaxRebaseAttempts } - c.mergeTrain.blocked[key] = sha + return p } -// mergeTrainChangedFiles returns pr's changed file names, served from the -// cache while the head SHA is unchanged. -func (c *Engine) mergeTrainChangedFiles(ctx context.Context, owner, repo string, pr *gh.PullRequest) ([]string, error) { - key := c.evaluatedHeadKey(owner, repo, pr.GetNumber()) - sha := pr.GetHead().GetSHA() - c.mergeTrain.mu.Lock() - cached, ok := c.mergeTrain.files[key] - c.mergeTrain.mu.Unlock() - if ok && cached.sha == sha { - return cached.files, nil - } - changed, err := ListChangedFiles(hgithub.WithRESTCaller(ctx, "hive:automerge_merge_train"), c.gh, owner, repo, pr) - if err != nil { - return nil, err - } - files := make([]string, 0, len(changed)) - for _, f := range changed { - files = append(files, f.Filename) - } - c.mergeTrain.mu.Lock() - if c.mergeTrain.files == nil { - c.mergeTrain.files = make(map[string]mergeTrainFiles) - } - c.mergeTrain.files[key] = mergeTrainFiles{sha: sha, files: files} - c.mergeTrain.mu.Unlock() - return files, nil +// mergeTrainCandidate reports whether a prefilter verdict admits a PR to +// the train: a clean pass, or a `needs-rebase` label — the very PR the +// train exists to order and repair. +func mergeTrainCandidate(prefilterReason string) bool { + return prefilterReason == "" || prefilterReason == "label:needs-rebase" } -// mergeTrainSlots plans the merge trains for one repo's listed PRs. eligible -// is the lane's cheap list-payload prefilter; only PRs it admits, on a base -// with at least two of them, cost a files lookup. A PR whose files cannot be -// read stays out of every train, so a lookup failure never holds a merge back. -func (c *Engine) mergeTrainSlots(ctx context.Context, displayRepo, owner, repo string, prs []*gh.PullRequest, eligible func(*gh.PullRequest) bool) map[int]mergeTrainSlot { - if c == nil || c.gh == nil || !c.mergeTrainEnabled() { - return nil +// planMergeTrains groups this lane's candidates on owner/repo into trains. +// prefilter is the lane's own cheap gate; API reads (files, then the +// mergeable state of grouped members) are spent only where two or more +// candidates share a base. Errors are logged and leave the PR out of any +// train, so a failed read never blocks a merge the lane would have made. +func (c *Engine) planMergeTrains(ctx context.Context, lane, displayRepo, owner, repo string, prs []*gh.PullRequest, prefilter func(*gh.PullRequest) string) *mergeTrainPlan { + plan := &mergeTrainPlan{lane: lane, displayRepo: displayRepo, owner: owner, repo: repo, byNumber: map[int]*mergeTrainGroup{}} + if !c.mergeTrainPolicyNow().Enabled || c == nil || c.gh == nil { + return plan } - perBase := make(map[string][]*gh.PullRequest) + perBase := map[string][]*gh.PullRequest{} for _, pr := range prs { - if pr == nil || pr.GetNumber() <= 0 || !eligible(pr) { + if pr == nil || pr.GetNumber() <= 0 || pr.GetHead() == nil || pr.GetHead().GetSHA() == "" { + continue + } + if !mergeTrainCandidate(prefilter(pr)) { continue } base := pr.GetBase().GetRef() perBase[base] = append(perBase[base], pr) } var members []mergeTrainMember - live := make(map[string]bool) - listed := make(map[string]bool) - for _, group := range perBase { - for _, pr := range group { - listed[c.evaluatedHeadKey(owner, repo, pr.GetNumber())] = true - } - } - for base, group := range perBase { - if len(group) < 2 { + for base, list := range perBase { + if len(list) < 2 { continue } - for _, pr := range group { - files, err := c.mergeTrainChangedFiles(ctx, owner, repo, pr) + for _, pr := range list { + files, err := c.mergeTrainFiles(ctx, owner, repo, pr.GetNumber(), pr.GetHead().GetSHA()) if err != nil { - c.warn("merge train could not list PR files; PR stays out of the train", "repo", displayRepo, "pr", pr.GetNumber(), "error", err) + c.warn("merge train: could not list PR files; left out of the train", "repo", displayRepo, "pr", pr.GetNumber(), "error", err) continue } - key := c.evaluatedHeadKey(owner, repo, pr.GetNumber()) - live[key] = true - c.mergeTrain.mu.Lock() - blocked := c.mergeTrain.blocked[key] == pr.GetHead().GetSHA() - c.mergeTrain.mu.Unlock() members = append(members, mergeTrainMember{ - Number: pr.GetNumber(), - Base: base, - Created: pr.GetCreatedAt().Time, - Blocked: blocked, - Files: files, + Number: pr.GetNumber(), + Base: base, + Head: pr.GetHead().GetRef(), + HeadSHA: pr.GetHead().GetSHA(), + Files: files, + CreatedAt: pr.GetCreatedAt().Time, + MergeableState: pr.GetMergeableState(), + Fork: c.isForkPR(pr), + Blocked: c.mergeTrainHeadBlocked(owner, repo, pr.GetNumber(), pr.GetHead().GetSHA()), }) } } - slots := planMergeTrains(members) - c.logMergeTrainTransitions(displayRepo, owner, repo, slots, live, listed) - return slots -} - -// logMergeTrainTransitions writes one INFO line when a PR joins a train or its -// slot changes, and forgets cached files and blocked marks of this repo's PRs -// that are no longer listed (live: files were read; listed: lane-eligible). -func (c *Engine) logMergeTrainTransitions(displayRepo, owner, repo string, slots map[int]mergeTrainSlot, live, listed map[string]bool) { - prefix := strings.ToLower(strings.TrimSpace(owner)) + "/" + strings.ToLower(strings.TrimSpace(repo)) + "#" - numbers := make([]int, 0, len(slots)) - for n := range slots { - numbers = append(numbers, n) - } - sort.Ints(numbers) - c.mergeTrain.mu.Lock() - if c.mergeTrain.logged == nil { - c.mergeTrain.logged = make(map[string]string) - } - var lines [][]any - current := make(map[string]bool, len(slots)) - for _, n := range numbers { - slot := slots[n] - key := c.evaluatedHeadKey(owner, repo, n) - current[key] = true - summary := strconv.Itoa(slot.Front) + "/" + strconv.Itoa(slot.Position) + "/" + strconv.Itoa(slot.Size) - if c.mergeTrain.logged[key] == summary { + if len(members) == 0 { + return plan + } + // A first grouping on files alone says which members need their live + // mergeable state (the list API does not carry it); then regroup so + // the front is chosen on what GitHub reports now. + needState := map[int]bool{} + for _, g := range groupMergeTrains(members) { + for _, n := range g.Order { + needState[n] = true + } + } + for i := range members { + m := &members[i] + if !needState[m.Number] || m.MergeableState != "" { continue } - c.mergeTrain.logged[key] = summary - lines = append(lines, []any{"repo", displayRepo, "pr", n, "front", slot.Front, "position", slot.Position, "size", slot.Size, "overlap_files", strings.Join(slot.OverlapFiles, ",")}) + live, _, err := c.gh.PullRequests.Get(hgithub.WithRESTCaller(ctx, "hive:automerge_merge_train"), owner, repo, m.Number) + if err != nil { + c.warn("merge train: could not read PR state; treated as unknown", "repo", displayRepo, "pr", m.Number, "error", err) + continue + } + if !strings.EqualFold(live.GetState(), "open") { + m.Number = 0 // closed between listing and here: not a member + continue + } + m.MergeableState = live.GetMergeableState() + if live.GetHead() != nil && live.GetHead().GetSHA() != "" { + m.HeadSHA = live.GetHead().GetSHA() + } } - for key := range c.mergeTrain.logged { - if strings.HasPrefix(key, prefix) && !current[key] { - delete(c.mergeTrain.logged, key) + plan.groups = groupMergeTrains(members) + for i := range plan.groups { + g := &plan.groups[i] + for _, n := range g.Order { + plan.byNumber[n] = g } + c.logMergeTrainTransition(displayRepo, lane, *g) + } + return plan +} + +// mergeTrainFiles lists a PR's changed paths, cached by head SHA. +func (c *Engine) mergeTrainFiles(ctx context.Context, owner, repo string, number int, headSHA string) ([]string, error) { + key := owner + "/" + repo + "#" + strconv.Itoa(number) + c.mergeTrainMu.Lock() + if c.mergeTrainFilesCache == nil { + c.mergeTrainFilesCache = map[string]mergeTrainFilesEntry{} } - for key := range c.mergeTrain.files { - if strings.HasPrefix(key, prefix) && !live[key] { - delete(c.mergeTrain.files, key) + if e, ok := c.mergeTrainFilesCache[key]; ok && e.sha == headSHA { + c.mergeTrainMu.Unlock() + return e.files, nil + } + c.mergeTrainMu.Unlock() + var files []string + opts := &gh.ListOptions{PerPage: 100} + for page := 0; page < mergeTrainFilesPages; page++ { + list, resp, err := c.gh.PullRequests.ListFiles(hgithub.WithRESTCaller(ctx, "hive:automerge_merge_train"), owner, repo, number, opts) + if err != nil { + return nil, err + } + for _, f := range list { + if name := f.GetFilename(); name != "" { + files = append(files, name) + } + if prev := f.GetPreviousFilename(); prev != "" { + files = append(files, prev) + } + } + if resp == nil || resp.NextPage == 0 { + break + } + opts.Page = resp.NextPage + } + c.mergeTrainMu.Lock() + c.mergeTrainFilesCache[key] = mergeTrainFilesEntry{sha: headSHA, files: files} + c.mergeTrainMu.Unlock() + return files, nil +} + +type mergeTrainFilesEntry struct { + sha string + files []string +} + +// logMergeTrainTransition logs one INFO line when a train's order changes +// (first sight, a new front, a member joining or leaving), never per cycle. +func (c *Engine) logMergeTrainTransition(displayRepo, lane string, g mergeTrainGroup) { + if len(g.Order) == 0 { + return + } + key := displayRepo + "|" + lane + "|" + g.Base + "|" + strconv.Itoa(c.mergeTrainMemberRoot(g)) + sig := g.signature() + c.mergeTrainMu.Lock() + if c.mergeTrainLogged == nil { + c.mergeTrainLogged = map[string]string{} + } + prev := c.mergeTrainLogged[key] + c.mergeTrainLogged[key] = sig + c.mergeTrainMu.Unlock() + if prev == sig { + return + } + c.info("merge train", "repo", displayRepo, "lane", lane, "base", g.Base, "front", g.Front(), "order", g.Order, "size", len(g.Order), "overlap_files", g.OverlapFiles) +} + +// mergeTrainMemberRoot is the lowest member number: the key that names a +// train across passes while its front changes. +func (c *Engine) mergeTrainMemberRoot(g mergeTrainGroup) int { + root := 0 + for _, n := range g.Order { + if root == 0 || n < root { + root = n + } + } + return root +} + +// mergeTrainAttemptKey keys the rebase loop guard: one PR behind one front. +func mergeTrainAttemptKey(owner, repo string, number, front int) string { + return owner + "/" + repo + "#" + strconv.Itoa(number) + "@" + strconv.Itoa(front) +} + +func (c *Engine) mergeTrainAttempts(owner, repo string, number, front int) int { + c.mergeTrainMu.Lock() + defer c.mergeTrainMu.Unlock() + return c.mergeTrainAttemptCount[mergeTrainAttemptKey(owner, repo, number, front)] +} + +func (c *Engine) bumpMergeTrainAttempts(owner, repo string, number, front int) int { + c.mergeTrainMu.Lock() + defer c.mergeTrainMu.Unlock() + if c.mergeTrainAttemptCount == nil { + c.mergeTrainAttemptCount = map[string]int{} + } + key := mergeTrainAttemptKey(owner, repo, number, front) + c.mergeTrainAttemptCount[key]++ + return c.mergeTrainAttemptCount[key] +} + +// mergeTrainLastFront is the front that last merged on owner/repo:base +// (0 when none this process), the "front" a leading dirty PR is behind. +func (c *Engine) mergeTrainLastFront(owner, repo, base string) int { + c.mergeTrainMu.Lock() + defer c.mergeTrainMu.Unlock() + return c.mergeTrainMerged[owner+"/"+repo+":"+base] +} + +func (c *Engine) noteMergeTrainFrontMerged(owner, repo, base string, front int) { + c.mergeTrainMu.Lock() + defer c.mergeTrainMu.Unlock() + if c.mergeTrainMerged == nil { + c.mergeTrainMerged = map[string]int{} + } + c.mergeTrainMerged[owner+"/"+repo+":"+base] = front +} + +// mergeTrainSiblingReason is the park reason for a PR behind the front. +func mergeTrainSiblingReason(g *mergeTrainGroup, number int) string { + return prstage.MergeTrain{ + State: prstage.MergeTrainStateQueuedBehind, + Front: g.Front(), + Position: g.Position(number), + Size: len(g.Order), + OverlapFiles: g.OverlapFiles, + }.Reason() +} + +// mergeTrainFrontReason is the reason for a dirty front the train is +// rebasing (or gave up on). +func mergeTrainFrontReason(g *mergeTrainGroup, state string, attempt, maxAttempts int) string { + return prstage.MergeTrain{ + State: state, + Front: g.Front(), + Position: 1, + Size: len(g.Order), + Attempt: attempt, + MaxAttempts: maxAttempts, + OverlapFiles: g.OverlapFiles, + }.Reason() +} + +// mergeTrainFront handles a train's front before the lane evaluates it: +// a conflicting front is sent to the rebase resolver (bounded by the loop +// guard) and parked as rebasing; past the guard it parks for a person. +// handled=false hands a clean or behind front to the lane's normal path, +// and a dirty one too when no resolver is wired. +func (c *Engine) mergeTrainFront(ctx context.Context, plan *mergeTrainPlan, pr *gh.PullRequest) (reason string, handled bool) { + if plan == nil || pr == nil { + return "", false + } + number := pr.GetNumber() + g, ok := plan.group(number) + if !ok || g.Front() != number { + return "", false + } + m := g.members[number] + if !mergeTrainDirty(m.MergeableState) { + return "", false + } + policy := c.mergeTrainPolicyNow() + behind := c.mergeTrainLastFront(plan.owner, plan.repo, g.Base) + attempts := c.mergeTrainAttempts(plan.owner, plan.repo, number, behind) + if attempts >= policy.MaxRebaseAttempts { + return mergeTrainFrontReason(g, prstage.MergeTrainStateRebaseExhausted, attempts, policy.MaxRebaseAttempts), true + } + if c.rebaseResolver == nil { + if c.markTerminalHoldLogged(plan.owner, plan.repo, number, m.HeadSHA, "merge-train-no-resolver") { + c.info("merge train: front is conflicting and no rebase resolver is wired; leaving it on the conflicting path", "repo", plan.displayRepo, "pr", number, "base", g.Base) } + return "", false + } + attempt := c.bumpMergeTrainAttempts(plan.owner, plan.repo, number, behind) + req := RebaseRequest{Repo: plan.owner + "/" + plan.repo, Number: number, Base: g.Base, Head: m.Head, HeadSHA: m.HeadSHA, Front: behind, Attempt: attempt, MaxAttempts: policy.MaxRebaseAttempts, Lane: plan.lane} + if err := c.rebaseResolver.RequestRebase(ctx, req); err != nil { + c.warn("merge train: rebase request failed", "repo", plan.displayRepo, "pr", number, "attempt", attempt, "error", err) + } else { + c.info("merge train: rebase requested for the front", "repo", plan.displayRepo, "pr", number, "base", g.Base, "behind", behind, "attempt", attempt, "max_attempts", policy.MaxRebaseAttempts) } - for key := range c.mergeTrain.blocked { - if strings.HasPrefix(key, prefix) && !listed[key] { - delete(c.mergeTrain.blocked, key) + return mergeTrainFrontReason(g, prstage.MergeTrainStateRebasing, attempt, policy.MaxRebaseAttempts), true +} + +// mergeTrainAfterFrontMerge runs right after a train's front merged: every +// sibling is read back and, as GitHub now reports it, updated (behind) or +// sent for a rebase (dirty, within the loop guard). A sibling GitHub has +// not re-evaluated yet (unknown) is left to the next pass, which replans. +func (c *Engine) mergeTrainAfterFrontMerge(ctx context.Context, plan *mergeTrainPlan, front int) { + if plan == nil { + return + } + g, ok := plan.group(front) + if !ok || g.Front() != front { + return + } + c.noteMergeTrainFrontMerged(plan.owner, plan.repo, g.Base, front) + policy := c.mergeTrainPolicyNow() + for _, n := range g.Siblings() { + live, _, err := c.gh.PullRequests.Get(hgithub.WithRESTCaller(ctx, "hive:automerge_merge_train"), plan.owner, plan.repo, n) + if err != nil { + if !isGitHubStatus(err, http.StatusNotFound) { + c.warn("merge train: could not read sibling after the front merged", "repo", plan.displayRepo, "pr", n, "front", front, "error", err) + } + continue + } + if !strings.EqualFold(live.GetState(), "open") { + continue } + state := strings.ToLower(strings.TrimSpace(live.GetMergeableState())) + head := live.GetHead().GetSHA() + switch { + case state == "behind": + if err := c.executeUpdateBranchEffect(ctx, plan.owner, plan.repo, n, head, "merge-train:front-merged#"+strconv.Itoa(front)); err != nil { + switch classifyUpdateBranchError(err, c.isForkPR(live)) { + case updateBranchUpToDate: + case updateBranchConflict: + c.mergeTrainRequestSiblingRebase(ctx, plan, g, live, front, policy) + default: + c.warn("merge train: update-branch failed on sibling", "repo", plan.displayRepo, "pr", n, "front", front, "error", err) + } + continue + } + c.info("merge train: sibling branch updated after the front merged", "repo", plan.displayRepo, "pr", n, "front", front) + case mergeTrainDirty(state): + c.mergeTrainRequestSiblingRebase(ctx, plan, g, live, front, policy) + } + } +} + +func (c *Engine) mergeTrainRequestSiblingRebase(ctx context.Context, plan *mergeTrainPlan, g *mergeTrainGroup, live *gh.PullRequest, front int, policy MergeTrainPolicy) { + n := live.GetNumber() + if c.rebaseResolver == nil { + return + } + if c.mergeTrainAttempts(plan.owner, plan.repo, n, front) >= policy.MaxRebaseAttempts { + return + } + attempt := c.bumpMergeTrainAttempts(plan.owner, plan.repo, n, front) + req := RebaseRequest{Repo: plan.owner + "/" + plan.repo, Number: n, Base: g.Base, Head: live.GetHead().GetRef(), HeadSHA: live.GetHead().GetSHA(), Front: front, Attempt: attempt, MaxAttempts: policy.MaxRebaseAttempts, Lane: plan.lane} + if err := c.rebaseResolver.RequestRebase(ctx, req); err != nil { + c.warn("merge train: rebase request failed on sibling", "repo", plan.displayRepo, "pr", n, "front", front, "attempt", attempt, "error", err) + return + } + c.info("merge train: rebase requested for sibling after the front merged", "repo", plan.displayRepo, "pr", n, "front", front, "attempt", attempt, "max_attempts", policy.MaxRebaseAttempts) +} + +// mergeTrainSkip is the per-cycle sibling skip: recorded on the stage +// tracker (so the row reads "In merge train · 3rd of 4") and logged at +// debug only — the train's INFO line is the transition, not the wait. +func (c *Engine) mergeTrainSkip(plan *mergeTrainPlan, number int) string { + g, _ := plan.group(number) + reason := mergeTrainSiblingReason(g, number) + c.debug("automerge skip", "repo", plan.displayRepo, "pr", number, "reason", reason, "lane", plan.lane) + return reason +} + +// mergeTrainState is the engine's in-memory train bookkeeping. +type mergeTrainState struct { + mergeTrainMu sync.Mutex + mergeTrainFilesCache map[string]mergeTrainFilesEntry + mergeTrainAttemptCount map[string]int + mergeTrainMerged map[string]int + mergeTrainLogged map[string]string + mergeTrainBlocked map[string]string +} + +// mergeTrainBlockingReason reports whether a lane's verdict means the head +// cannot merge without someone changing it. A conflict is NOT blocking: the +// train's own rebase step repairs it. +func mergeTrainBlockingReason(reason string) bool { + switch reason { + case "not-mergeable", autoMergeReasonForkNotEditable, autoMergeReasonClosedSuperseded: + return true } - c.mergeTrain.mu.Unlock() - for _, args := range lines { - c.info("automerge merge train", args...) + return isTerminalRequiredCheckHold(reason) || isPermanentMergeRefusal(reason) || classifyCIReason(reason) == ciReasonRed +} + +// noteMergeTrainOutcome remembers, per head, whether the lane's verdict +// blocks it from fronting a train; a non-blocking verdict clears the mark. +func (c *Engine) noteMergeTrainOutcome(owner, repo string, number int, sha, reason string) { + if c == nil || number <= 0 { + return + } + key := owner + "/" + repo + "#" + strconv.Itoa(number) + c.mergeTrainMu.Lock() + defer c.mergeTrainMu.Unlock() + if !mergeTrainBlockingReason(reason) || strings.TrimSpace(sha) == "" { + delete(c.mergeTrainBlocked, key) + return + } + if c.mergeTrainBlocked == nil { + c.mergeTrainBlocked = map[string]string{} + } + c.mergeTrainBlocked[key] = sha +} + +// mergeTrainHeadBlocked reports whether this exact head was last seen blocked. +func (c *Engine) mergeTrainHeadBlocked(owner, repo string, number int, sha string) bool { + if c == nil || sha == "" { + return false } + c.mergeTrainMu.Lock() + defer c.mergeTrainMu.Unlock() + return c.mergeTrainBlocked[owner+"/"+repo+"#"+strconv.Itoa(number)] == sha } -// mergeTrainQueued reports whether number waits behind another PR's front. -func mergeTrainQueued(slots map[int]mergeTrainSlot, number int) (mergeTrainSlot, bool) { - slot, ok := slots[number] - return slot, ok && slot.Position > 1 +// String renders a request for logs. +func (r RebaseRequest) String() string { + return fmt.Sprintf("%s#%d onto %s (behind #%d, attempt %d/%d, %s)", r.Repo, r.Number, r.Base, r.Front, r.Attempt, r.MaxAttempts, r.Lane) } diff --git a/src/pkg/github/automerge/automerge_merge_train_test.go b/src/pkg/github/automerge/automerge_merge_train_test.go index 0f1b5b80f8..dbcabefc80 100644 --- a/src/pkg/github/automerge/automerge_merge_train_test.go +++ b/src/pkg/github/automerge/automerge_merge_train_test.go @@ -1,253 +1,492 @@ package automerge import ( + "bytes" "context" + "encoding/json" + "log/slog" + "net/http" + "net/http/httptest" "reflect" - "sort" + "strconv" + "strings" + "sync" "testing" "time" + + gh "github.com/google/go-github/v72/github" + + "github.com/hivecommons/hive/pkg/stageengine/prstage" ) -func TestPlanMergeTrains(t *testing.T) { - day := func(d int) time.Time { return time.Date(2026, 10, d, 0, 0, 0, 0, time.UTC) } +func mtMember(n int, state string, created time.Time, files ...string) mergeTrainMember { + return mergeTrainMember{Number: n, Base: "v5", HeadSHA: "sha" + strconv.Itoa(n), Files: files, CreatedAt: created, MergeableState: state} +} + +func TestGroupMergeTrains(t *testing.T) { + t0 := time.Date(2026, 10, 20, 9, 0, 0, 0, time.UTC) cases := []struct { name string members []mergeTrainMember - want map[int]mergeTrainSlot + want [][]int + overlap []string }{ { - name: "disjoint PRs form no train", + name: "disjoint files form no train", members: []mergeTrainMember{ - {Number: 1, Base: "v5", Files: []string{"a.go"}}, - {Number: 2, Base: "v5", Files: []string{"b.go"}}, + mtMember(1, "clean", t0, "a.go"), + mtMember(2, "clean", t0.Add(time.Minute), "b.go"), }, - want: map[int]mergeTrainSlot{}, }, { - name: "overlap picks the lowest number when ages tie", + name: "oldest approved leads when all are clean", members: []mergeTrainMember{ - {Number: 2, Base: "v5", Files: []string{"stage_tracker.go"}}, - {Number: 1, Base: "v5", Files: []string{"stage_tracker.go", "a.go"}}, - }, - want: map[int]mergeTrainSlot{ - 1: {Front: 1, Position: 1, Size: 2, OverlapFiles: []string{"stage_tracker.go"}}, - 2: {Front: 1, Position: 2, Size: 2, OverlapFiles: []string{"stage_tracker.go"}}, + mtMember(3, "clean", t0.Add(2*time.Minute), "tracker.go", "index.html"), + mtMember(1, "clean", t0, "tracker.go"), + mtMember(2, "clean", t0.Add(time.Minute), "index.html", "other.go"), }, + want: [][]int{{1, 2, 3}}, + overlap: []string{"index.html", "tracker.go"}, }, { - name: "oldest fronts over a lower number", + name: "clean mergeable leads over an older dirty one", members: []mergeTrainMember{ - {Number: 1, Base: "v5", Created: day(3), Files: []string{"x.go"}}, - {Number: 2, Base: "v5", Created: day(1), Files: []string{"x.go"}}, - }, - want: map[int]mergeTrainSlot{ - 2: {Front: 2, Position: 1, Size: 2, OverlapFiles: []string{"x.go"}}, - 1: {Front: 2, Position: 2, Size: 2, OverlapFiles: []string{"x.go"}}, + mtMember(1, "dirty", t0, "tracker.go"), + mtMember(2, "behind", t0.Add(time.Minute), "tracker.go"), + mtMember(3, "clean", t0.Add(2*time.Minute), "tracker.go"), }, + want: [][]int{{3, 2, 1}}, + overlap: []string{"tracker.go"}, }, { - name: "mergeable fronts over an older blocked head", + name: "transitive overlap joins one train, a stranger stays out", members: []mergeTrainMember{ - {Number: 1, Base: "v5", Created: day(1), Blocked: true, Files: []string{"x.go"}}, - {Number: 2, Base: "v5", Created: day(2), Files: []string{"x.go"}}, - }, - want: map[int]mergeTrainSlot{ - 2: {Front: 2, Position: 1, Size: 2, OverlapFiles: []string{"x.go"}}, - 1: {Front: 2, Position: 2, Size: 2, OverlapFiles: []string{"x.go"}}, + mtMember(1, "clean", t0, "a.go"), + mtMember(2, "clean", t0.Add(time.Minute), "a.go", "b.go"), + mtMember(3, "clean", t0.Add(2*time.Minute), "b.go"), + mtMember(9, "clean", t0, "z.go"), }, + want: [][]int{{1, 2, 3}}, + overlap: []string{"a.go", "b.go"}, }, { - name: "every member blocked holds nobody back", + name: "different bases never share a train", members: []mergeTrainMember{ - {Number: 1, Base: "v5", Blocked: true, Files: []string{"x.go"}}, - {Number: 2, Base: "v5", Blocked: true, Files: []string{"x.go"}}, + mtMember(1, "clean", t0, "a.go"), + {Number: 2, Base: "main", Files: []string{"a.go"}, MergeableState: "clean", CreatedAt: t0}, }, - want: map[int]mergeTrainSlot{}, }, { - name: "transitive overlap is one train", + name: "unknown state sorts between clean and behind, ties on number", members: []mergeTrainMember{ - {Number: 1, Base: "v5", Files: []string{"a.go", "b.go"}}, - {Number: 2, Base: "v5", Files: []string{"b.go", "c.go"}}, - {Number: 3, Base: "v5", Files: []string{"c.go", "d.go"}}, - {Number: 4, Base: "v5", Files: []string{"e.go"}}, - }, - want: map[int]mergeTrainSlot{ - 1: {Front: 1, Position: 1, Size: 3, OverlapFiles: []string{"b.go"}}, - 2: {Front: 1, Position: 2, Size: 3, OverlapFiles: []string{"b.go", "c.go"}}, - 3: {Front: 1, Position: 3, Size: 3, OverlapFiles: []string{"c.go"}}, + mtMember(5, "", t0, "a.go"), + mtMember(4, "behind", t0, "a.go"), + mtMember(6, "", t0, "a.go"), }, + want: [][]int{{5, 6, 4}}, + overlap: []string{"a.go"}, }, { - name: "different bases never share a train", + name: "a mergeable head fronts over an older blocked one", members: []mergeTrainMember{ - {Number: 1, Base: "v5", Files: []string{"x.go"}}, - {Number: 2, Base: "main", Files: []string{"x.go"}}, + blocked(mtMember(1, "clean", t0, "x.go")), + mtMember(2, "clean", t0.Add(time.Minute), "x.go"), }, - want: map[int]mergeTrainSlot{}, + want: [][]int{{2, 1}}, + overlap: []string{"x.go"}, }, { - name: "two separate trains on one base", + name: "every member blocked holds nobody back (fail-open)", members: []mergeTrainMember{ - {Number: 1, Base: "v5", Files: []string{"x.go"}}, - {Number: 2, Base: "v5", Files: []string{"y.go"}}, - {Number: 3, Base: "v5", Files: []string{"x.go"}}, - {Number: 4, Base: "v5", Files: []string{"y.go"}}, - }, - want: map[int]mergeTrainSlot{ - 1: {Front: 1, Position: 1, Size: 2, OverlapFiles: []string{"x.go"}}, - 3: {Front: 1, Position: 2, Size: 2, OverlapFiles: []string{"x.go"}}, - 2: {Front: 2, Position: 1, Size: 2, OverlapFiles: []string{"y.go"}}, - 4: {Front: 2, Position: 2, Size: 2, OverlapFiles: []string{"y.go"}}, + blocked(mtMember(1, "clean", t0, "x.go")), + blocked(mtMember(2, "clean", t0.Add(time.Minute), "x.go")), }, }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { - got := planMergeTrains(tc.members) + groups := groupMergeTrains(tc.members) + var got [][]int + for _, g := range groups { + got = append(got, g.Order) + } if !reflect.DeepEqual(got, tc.want) { - t.Fatalf("planMergeTrains = %#v, want %#v", got, tc.want) + t.Fatalf("orders = %v, want %v", got, tc.want) + } + if len(groups) == 1 && !reflect.DeepEqual(groups[0].OverlapFiles, tc.overlap) { + t.Fatalf("overlap = %v, want %v", groups[0].OverlapFiles, tc.overlap) + } + if len(groups) == 1 { + g := groups[0] + if g.Front() != tc.want[0][0] || g.Position(tc.want[0][len(tc.want[0])-1]) != len(tc.want[0]) || len(g.Siblings()) != len(tc.want[0])-1 { + t.Fatalf("front/position/siblings wrong: %d %v", g.Front(), g.Siblings()) + } } }) } } -func TestMergeTrainBlockingReason(t *testing.T) { - cases := []struct { - reason string - want bool - }{ - {"", false}, - {"conflicting", true}, - {"not-mergeable", true}, - {autoMergeReasonForkNotEditable, true}, - {"behind", false}, - {"updated-branch", false}, - {autoMergeReasonMergeTrainQueued, false}, +// mergeTrainAPI is a GitHub API for one repository whose PRs have files and +// a mergeable state; it counts update-branch calls. +type mergeTrainAPI struct { + mu sync.Mutex + files map[int][]string + state map[int]string + heads map[int]string + closed map[int]bool + updates []int + gets int + lists int +} + +func newMergeTrainServer(t *testing.T, api *mergeTrainAPI) *httptest.Server { + t.Helper() + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + api.mu.Lock() + defer api.mu.Unlock() + path := strings.TrimPrefix(r.URL.Path, "/repos/acme/widget/pulls/") + parts := strings.Split(path, "/") + n, _ := strconv.Atoi(parts[0]) + switch { + case r.Method == http.MethodGet && len(parts) == 2 && parts[1] == "files": + api.lists++ + var out []map[string]any + for _, f := range api.files[n] { + out = append(out, map[string]any{"filename": f}) + } + _ = json.NewEncoder(w).Encode(out) + case r.Method == http.MethodGet && len(parts) == 1: + api.gets++ + state := "open" + if api.closed[n] { + state = "closed" + } + head := api.heads[n] + if head == "" { + head = "sha" + strconv.Itoa(n) + } + _ = json.NewEncoder(w).Encode(map[string]any{"number": n, "state": state, "mergeable_state": api.state[n], + "head": map[string]any{"sha": head, "ref": "feat/" + strconv.Itoa(n), "repo": map[string]any{"full_name": "acme/widget"}}, + "base": map[string]any{"ref": "v5", "repo": map[string]any{"full_name": "acme/widget"}}}) + case r.Method == http.MethodPut && len(parts) == 2 && parts[1] == "update-branch": + api.updates = append(api.updates, n) + w.WriteHeader(http.StatusAccepted) + _, _ = w.Write([]byte(`{"message":"Updating pull request branch."}`)) + default: + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + w.WriteHeader(http.StatusNotFound) + } + })) +} + +func mtPR(n int, created time.Time, state string) *gh.PullRequest { + return &gh.PullRequest{Number: gh.Ptr(n), State: gh.Ptr("open"), MergeableState: gh.Ptr(state), CreatedAt: &gh.Timestamp{Time: created}, + Head: &gh.PullRequestBranch{SHA: gh.Ptr("sha" + strconv.Itoa(n)), Ref: gh.Ptr("feat/" + strconv.Itoa(n)), Repo: &gh.Repository{FullName: gh.Ptr("acme/widget")}}, + Base: &gh.PullRequestBranch{Ref: gh.Ptr("v5"), Repo: &gh.Repository{FullName: gh.Ptr("acme/widget")}}} +} + +func newMergeTrainEngine(t *testing.T, srvURL string, policy MergeTrainPolicy, resolver RebaseResolver) (*Engine, *bytes.Buffer) { + t.Helper() + c := newAutoMergeSweepClient(srvURL) + var logs bytes.Buffer + c.logger = slog.New(slog.NewTextHandler(&logs, &slog.HandlerOptions{Level: slog.LevelInfo})) + c.mergeTrainPolicy = func() MergeTrainPolicy { return policy } + c.rebaseResolver = resolver + return c, &logs +} + +func TestPlanMergeTrainsSiblingsAndFront(t *testing.T) { + t0 := time.Date(2026, 10, 20, 9, 0, 0, 0, time.UTC) + api := &mergeTrainAPI{ + files: map[int][]string{10: {"tracker.go", "index.html"}, 11: {"tracker.go"}, 12: {"index.html", "x.go"}, 13: {"readme.md"}}, + state: map[int]string{10: "clean", 11: "dirty", 12: "behind"}, } - for _, tc := range cases { - if got := mergeTrainBlockingReason(tc.reason); got != tc.want { - t.Errorf("mergeTrainBlockingReason(%q) = %v, want %v", tc.reason, got, tc.want) + srv := newMergeTrainServer(t, api) + defer srv.Close() + c, logs := newMergeTrainEngine(t, srv.URL, DefaultMergeTrainPolicy, nil) + // The list API carries no mergeable state; the planner reads it. + prs := []*gh.PullRequest{mtPR(13, t0, ""), mtPR(11, t0.Add(time.Minute), ""), mtPR(10, t0.Add(2*time.Minute), ""), mtPR(12, t0.Add(3*time.Minute), "")} + prefilter := func(pr *gh.PullRequest) string { + if pr.GetNumber() == 11 { + return "label:needs-rebase" // admitted: the label is what the train repairs } + return "" + } + plan := c.planMergeTrains(context.Background(), "self", "acme/widget", "acme", "widget", prs, prefilter) + if !plan.isFront(10) || !plan.isSibling(11) || !plan.isSibling(12) || plan.isSibling(13) || plan.isFront(13) { + t.Fatalf("front/siblings wrong: front10=%v sib11=%v sib12=%v 13 in train=%v", plan.isFront(10), plan.isSibling(11), plan.isSibling(12), plan.isSibling(13) || plan.isFront(13)) + } + g, _ := plan.group(10) + if !reflect.DeepEqual(g.Order, []int{10, 12, 11}) { + t.Fatalf("order = %v, want clean, behind, dirty", g.Order) + } + reason := c.mergeTrainSkip(plan, 11) + mt, ok := prstage.ParseMergeTrain(reason) + if !ok || mt.State != prstage.MergeTrainStateQueuedBehind || mt.Front != 10 || mt.Position != 3 || mt.Size != 3 { + t.Fatalf("sibling reason %q parsed %+v", reason, mt) + } + if got := mt.Sentence(); !strings.Contains(got, "behind #10 in merge train (3rd of 3, overlapping files: index.html, tracker.go)") { + t.Fatalf("sentence = %q", got) + } + // A clean front is the lane's to merge on its normal path. + if r, handled := c.mergeTrainFront(context.Background(), plan, prs[2]); handled || r != "" { + t.Fatalf("clean front handled=%v reason=%q", handled, r) + } + // One INFO transition line for the train; a replan with the same order + // adds none, and the files are served from the head-SHA cache. + lists, gets := api.lists, api.gets + _ = c.planMergeTrains(context.Background(), "self", "acme/widget", "acme", "widget", prs, prefilter) + if n := strings.Count(logs.String(), "msg=\"merge train\""); n != 1 { + t.Fatalf("transition lines = %d, want 1\n%s", n, logs.String()) + } + if api.lists != lists { + t.Fatalf("files re-listed on an unchanged head: %d -> %d", lists, api.lists) + } + if api.gets <= gets { + t.Fatalf("mergeable state must be re-read each pass") + } + // A new front is a transition. + api.mu.Lock() + api.state[10] = "dirty" + api.mu.Unlock() + plan = c.planMergeTrains(context.Background(), "self", "acme/widget", "acme", "widget", prs, prefilter) + if !plan.isFront(12) { + t.Fatalf("expected #12 to lead once #10 went dirty; order %v", plan.groups[0].Order) + } + if n := strings.Count(logs.String(), "msg=\"merge train\""); n != 2 { + t.Fatalf("transition lines = %d, want 2", n) } } -func greenTrainPR(number int, files ...string) selfAuthoredPR { - return selfAuthoredPR{ - number: number, author: testHiveAppBotLogin, mergeableState: "clean", - statusState: "success", checkStatus: "completed", checkConclusion: "success", - files: files, +func TestPlanMergeTrainsDisabledAndSingletons(t *testing.T) { + t0 := time.Now() + api := &mergeTrainAPI{files: map[int][]string{1: {"a.go"}, 2: {"a.go"}}, state: map[int]string{1: "clean", 2: "clean"}} + srv := newMergeTrainServer(t, api) + defer srv.Close() + c, _ := newMergeTrainEngine(t, srv.URL, MergeTrainPolicy{Enabled: false}, nil) + prs := []*gh.PullRequest{mtPR(1, t0, ""), mtPR(2, t0, "")} + plan := c.planMergeTrains(context.Background(), "self", "acme/widget", "acme", "widget", prs, func(*gh.PullRequest) string { return "" }) + if len(plan.groups) != 0 || api.lists != 0 { + t.Fatalf("disabled train planned %d groups and listed %d files", len(plan.groups), api.lists) + } + c, _ = newMergeTrainEngine(t, srv.URL, DefaultMergeTrainPolicy, nil) + // A lone candidate on its base costs no API call at all. + plan = c.planMergeTrains(context.Background(), "self", "acme/widget", "acme", "widget", prs[:1], func(*gh.PullRequest) string { return "" }) + if len(plan.groups) != 0 || api.lists != 0 { + t.Fatalf("singleton planned %d groups, listed %d", len(plan.groups), api.lists) + } + // Prefiltered-out PRs (draft, hold, …) never join. + plan = c.planMergeTrains(context.Background(), "self", "acme/widget", "acme", "widget", prs, func(*gh.PullRequest) string { return "draft" }) + if len(plan.groups) != 0 { + t.Fatalf("prefiltered PRs joined a train") + } + if mergeTrainCandidate("label:needs-rebase") != true || mergeTrainCandidate("label:hold") { + t.Fatal("candidate gate: needs-rebase admits, hold does not") } } -func TestSweepSelfAuthoredMergeTrain(t *testing.T) { - cases := []struct { - name string - disabled bool - prs []selfAuthoredPR - wantMerged []int - wantQueued int - }{ - { - name: "only the front of an overlapping group merges", - prs: []selfAuthoredPR{ - greenTrainPR(11, "src/pkg/dashboard/stage_tracker.go", "static/index.html"), - greenTrainPR(12, "src/pkg/dashboard/stage_tracker.go"), - greenTrainPR(13, "README.md"), - }, - wantMerged: []int{11, 13}, - wantQueued: 1, - }, - { - name: "the oldest PR fronts the train", - prs: func() []selfAuthoredPR { - newer, older := greenTrainPR(11, "static/index.html"), greenTrainPR(12, "static/index.html") - newer.createdAt, older.createdAt = "2026-10-28T10:00:00Z", "2026-10-27T10:00:00Z" - return []selfAuthoredPR{newer, older} - }(), - wantMerged: []int{12}, - wantQueued: 1, - }, - { - name: "disabled merges every green PR as before", - disabled: true, - prs: []selfAuthoredPR{ - greenTrainPR(11, "src/pkg/dashboard/stage_tracker.go"), - greenTrainPR(12, "src/pkg/dashboard/stage_tracker.go"), - }, - wantMerged: []int{11, 12}, - }, - { - name: "disjoint PRs all merge", - prs: []selfAuthoredPR{ - greenTrainPR(11, "a.go"), - greenTrainPR(12, "b.go"), - }, - wantMerged: []int{11, 12}, - }, +func TestMergeTrainAfterFrontMergeBehindAndDirty(t *testing.T) { + t0 := time.Date(2026, 10, 20, 9, 0, 0, 0, time.UTC) + api := &mergeTrainAPI{ + files: map[int][]string{20: {"a.go"}, 21: {"a.go"}, 22: {"a.go"}, 23: {"a.go"}}, + state: map[int]string{20: "clean", 21: "clean", 22: "clean", 23: "clean"}, } - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - var merged []int - api := newSelfAuthoredAutoMergeAPI(t, tc.prs, &merged) - defer api.Close() - c := newAutoMergeSweepClient(api.URL) - if tc.disabled { - c.mergeTrainEnabledFn = func() bool { return false } - } - result, err := c.SweepSelfAuthoredAutoMerges(context.Background(), AutoMergeSweepOptions{}) - if err != nil { - t.Fatalf("SweepSelfAuthoredAutoMerges returned error: %v", err) - } - sort.Ints(merged) - if !reflect.DeepEqual(merged, tc.wantMerged) { - t.Fatalf("merged = %v, want %v", merged, tc.wantMerged) - } - if queued := result.Skipped; queued != tc.wantQueued { - t.Fatalf("skipped = %d, want %d train-queued sibling(s)", queued, tc.wantQueued) - } - }) + srv := newMergeTrainServer(t, api) + defer srv.Close() + var mu sync.Mutex + var requests []RebaseRequest + resolver := RebaseResolverFunc(func(_ context.Context, req RebaseRequest) error { + mu.Lock() + defer mu.Unlock() + requests = append(requests, req) + return nil + }) + c, _ := newMergeTrainEngine(t, srv.URL, DefaultMergeTrainPolicy, resolver) + prs := []*gh.PullRequest{mtPR(20, t0, ""), mtPR(21, t0.Add(time.Minute), ""), mtPR(22, t0.Add(2*time.Minute), ""), mtPR(23, t0.Add(3*time.Minute), "")} + plan := c.planMergeTrains(context.Background(), "self", "acme/widget", "acme", "widget", prs, func(*gh.PullRequest) string { return "" }) + if !plan.isFront(20) { + t.Fatalf("front = %v", plan.groups) + } + // The front merged: GitHub now says #21 is behind, #22 dirty, #23 closed. + api.mu.Lock() + api.state[21], api.state[22] = "behind", "dirty" + api.closed = map[int]bool{23: true} + api.mu.Unlock() + c.mergeTrainAfterFrontMerge(context.Background(), plan, 20) + api.mu.Lock() + updates := append([]int(nil), api.updates...) + api.mu.Unlock() + if !reflect.DeepEqual(updates, []int{21}) { + t.Fatalf("update-branch on %v, want [21]", updates) + } + mu.Lock() + got := append([]RebaseRequest(nil), requests...) + mu.Unlock() + if len(got) != 1 || got[0].Number != 22 || got[0].Front != 20 || got[0].Attempt != 1 || got[0].MaxAttempts != 2 || got[0].Base != "v5" || got[0].Repo != "acme/widget" || got[0].Lane != "self" { + t.Fatalf("rebase requests = %+v", got) + } + if c.mergeTrainLastFront("acme", "widget", "v5") != 20 { + t.Fatal("merged front not recorded") + } + if s := got[0].String(); !strings.Contains(s, "acme/widget#22 onto v5 (behind #20, attempt 1/2, self)") { + t.Fatalf("String() = %q", s) + } +} + +func TestMergeTrainFrontRebaseLoopGuard(t *testing.T) { + t0 := time.Date(2026, 10, 20, 9, 0, 0, 0, time.UTC) + api := &mergeTrainAPI{ + files: map[int][]string{30: {"a.go"}, 31: {"a.go"}}, + state: map[int]string{30: "dirty", 31: "dirty"}, + } + srv := newMergeTrainServer(t, api) + defer srv.Close() + var mu sync.Mutex + calls := 0 + resolver := RebaseResolverFunc(func(context.Context, RebaseRequest) error { + mu.Lock() + defer mu.Unlock() + calls++ + return nil + }) + c, logs := newMergeTrainEngine(t, srv.URL, MergeTrainPolicy{Enabled: true, MaxRebaseAttempts: 2}, resolver) + // #29 merged earlier on this base: the dirty front is "behind" it. + c.noteMergeTrainFrontMerged("acme", "widget", "v5", 29) + prs := []*gh.PullRequest{mtPR(30, t0, ""), mtPR(31, t0.Add(time.Minute), "")} + var states []string + for pass := 0; pass < 4; pass++ { + plan := c.planMergeTrains(context.Background(), "self", "acme/widget", "acme", "widget", prs, func(*gh.PullRequest) string { return "" }) + reason, handled := c.mergeTrainFront(context.Background(), plan, prs[0]) + if !handled { + t.Fatalf("pass %d: dirty front with a resolver must be handled", pass) + } + mt, ok := prstage.ParseMergeTrain(reason) + if !ok { + t.Fatalf("pass %d: reason %q", pass, reason) + } + states = append(states, mt.State+":"+strconv.Itoa(mt.Attempt)+"/"+strconv.Itoa(mt.MaxAttempts)) + } + want := []string{"rebasing:1/2", "rebasing:2/2", "rebase-exhausted:2/2", "rebase-exhausted:2/2"} + if !reflect.DeepEqual(states, want) { + t.Fatalf("states = %v, want %v", states, want) + } + mu.Lock() + defer mu.Unlock() + if calls != 2 { + t.Fatalf("resolver calls = %d, want 2 (loop guard)", calls) + } + if !strings.Contains(logs.String(), "rebase requested for the front") { + t.Fatalf("missing rebase log\n%s", logs.String()) + } + // The exhausted reason classifies to a human stage with the action. + mt, _ := prstage.ParseMergeTrain(mergeTrainFrontReason(&plan0(c, prs).groups[0], prstage.MergeTrainStateRebaseExhausted, 2, 2)) + if mt.Sentence() == "" || !strings.Contains(mt.Sentence(), "after 2 rebase attempts") { + t.Fatalf("exhausted sentence = %q", mt.Sentence()) } } -// A conflicting front must not stall its train: once the sweep has seen the -// head conflict, the next pass fronts the mergeable sibling instead. -func TestSweepSelfAuthoredMergeTrainSkipsConflictingFront(t *testing.T) { - var merged []int - dirty := greenTrainPR(11, "static/index.html") - dirty.mergeableState = "dirty" - api := newSelfAuthoredAutoMergeAPI(t, []selfAuthoredPR{dirty, greenTrainPR(12, "static/index.html")}, &merged) - defer api.Close() - c := newAutoMergeSweepClient(api.URL) +func plan0(c *Engine, prs []*gh.PullRequest) *mergeTrainPlan { + return c.planMergeTrains(context.Background(), "self", "acme/widget", "acme", "widget", prs, func(*gh.PullRequest) string { return "" }) +} - if _, err := c.SweepSelfAuthoredAutoMerges(context.Background(), AutoMergeSweepOptions{}); err != nil { - t.Fatalf("first pass: %v", err) +func TestMergeTrainFrontNoResolverFallsThroughOnce(t *testing.T) { + t0 := time.Now() + api := &mergeTrainAPI{files: map[int][]string{40: {"a.go"}, 41: {"a.go"}}, state: map[int]string{40: "dirty", 41: "dirty"}} + srv := newMergeTrainServer(t, api) + defer srv.Close() + c, logs := newMergeTrainEngine(t, srv.URL, DefaultMergeTrainPolicy, nil) + prs := []*gh.PullRequest{mtPR(40, t0, ""), mtPR(41, t0.Add(time.Minute), "")} + for i := 0; i < 2; i++ { + plan := plan0(c, prs) + if r, handled := c.mergeTrainFront(context.Background(), plan, prs[0]); handled || r != "" { + t.Fatalf("no resolver: front must fall through, got handled=%v %q", handled, r) + } + } + if n := strings.Count(logs.String(), "no rebase resolver is wired"); n != 1 { + t.Fatalf("no-resolver line logged %d times, want once\n%s", n, logs.String()) } - if len(merged) != 0 { - t.Fatalf("first pass merged %v, want the sibling queued behind the untried front", merged) + // Without a resolver a sibling's dirty state after the merge is a no-op. + plan := plan0(c, prs) + c.mergeTrainAfterFrontMerge(context.Background(), plan, 40) + if len(api.updates) != 0 { + t.Fatalf("unexpected update-branch calls %v", api.updates) + } +} + +func TestMergeTrainPolicyDefaults(t *testing.T) { + var c *Engine + if p := c.mergeTrainPolicyNow(); p != DefaultMergeTrainPolicy { + t.Fatalf("nil engine policy = %+v", p) } - if _, err := c.SweepSelfAuthoredAutoMerges(context.Background(), AutoMergeSweepOptions{}); err != nil { - t.Fatalf("second pass: %v", err) + c = &Engine{mergeTrainPolicy: func() MergeTrainPolicy { return MergeTrainPolicy{Enabled: true} }} + if p := c.mergeTrainPolicyNow(); p.MaxRebaseAttempts != 2 { + t.Fatalf("zero max attempts must default to 2, got %d", p.MaxRebaseAttempts) } - if !reflect.DeepEqual(merged, []int{12}) { - t.Fatalf("second pass merged %v, want the mergeable sibling #12 as the new front", merged) + var plan *mergeTrainPlan + if plan.isFront(1) || plan.isSibling(1) { + t.Fatal("nil plan has no members") } } -func TestSweepSelfAuthoredMergeTrainCachesFilesByHead(t *testing.T) { - var merged []int - fetches11, fetches12 := 0, 0 - a, b := greenTrainPR(11, "x.go"), greenTrainPR(12, "x.go") - a.filesFetches, b.filesFetches = &fetches11, &fetches12 - api := newSelfAuthoredAutoMergeAPI(t, []selfAuthoredPR{a, b}, &merged) - defer api.Close() - c := newAutoMergeSweepClient(api.URL) - for i := 0; i < 3; i++ { - if _, err := c.SweepSelfAuthoredAutoMerges(context.Background(), AutoMergeSweepOptions{}); err != nil { - t.Fatalf("pass %d: %v", i, err) +func TestMergeTrainPolicyOptionLegacySwitch(t *testing.T) { + if mergeTrainPolicyOption(Options{}) != nil { + t.Fatal("no switch, no policy func") + } + full := func() MergeTrainPolicy { return MergeTrainPolicy{Enabled: false, MaxRebaseAttempts: 5} } + if got := mergeTrainPolicyOption(Options{MergeTrain: full, MergeTrainEnabled: func() bool { return true }})(); got.MaxRebaseAttempts != 5 || got.Enabled { + t.Fatalf("full policy must win: %+v", got) + } + off := false + got := mergeTrainPolicyOption(Options{MergeTrainEnabled: func() bool { return off }}) + if p := got(); p.Enabled || p.MaxRebaseAttempts != DefaultMergeTrainPolicy.MaxRebaseAttempts { + t.Fatalf("legacy off: %+v", p) + } + off = true + if p := got(); !p.Enabled { + t.Fatalf("legacy on: %+v", p) + } +} + +func blocked(m mergeTrainMember) mergeTrainMember { + m.Blocked = true + return m +} + +// TestMergeTrainBlockedHeadMemory: the lane's verdict per head decides +// whether it may front; a red/terminal verdict marks the exact head, any +// other verdict clears it, and a new head starts clean. A conflict is not +// blocking: the train's own rebase step repairs it. +func TestMergeTrainBlockedHeadMemory(t *testing.T) { + for reason, want := range map[string]bool{ + "": false, + "conflicting": false, + "check-pending": false, + "not-mergeable": true, + autoMergeReasonForkNotEditable: true, + autoMergeReasonClosedSuperseded: true, + "required-check-failing:ci/build": true, + workflowApprovalRequiredPrefix + "x": true, + } { + if got := mergeTrainBlockingReason(reason); got != want { + t.Fatalf("mergeTrainBlockingReason(%q) = %v, want %v", reason, got, want) } } - if fetches11 != 1 || fetches12 != 1 { - t.Fatalf("files fetches = #11:%d #12:%d, want one per unchanged head", fetches11, fetches12) + c := &Engine{} + c.noteMergeTrainOutcome("acme", "widget", 7, "sha1", "not-mergeable") + if !c.mergeTrainHeadBlocked("acme", "widget", 7, "sha1") || c.mergeTrainHeadBlocked("acme", "widget", 7, "sha2") { + t.Fatal("the exact head is blocked, a new head is not") + } + c.noteMergeTrainOutcome("acme", "widget", 7, "sha1", "") + if c.mergeTrainHeadBlocked("acme", "widget", 7, "sha1") { + t.Fatal("a clean verdict clears the mark") + } + c.noteMergeTrainOutcome("acme", "widget", 7, "", "not-mergeable") + if c.mergeTrainHeadBlocked("acme", "widget", 7, "") { + t.Fatal("no sha, no mark") + } + var nilEngine *Engine + nilEngine.noteMergeTrainOutcome("acme", "widget", 7, "sha1", "not-mergeable") + if nilEngine.mergeTrainHeadBlocked("acme", "widget", 7, "sha1") { + t.Fatal("nil engine blocks nothing") } } diff --git a/src/pkg/github/automerge/automerge_sweep.go b/src/pkg/github/automerge/automerge_sweep.go index 8f3e7b1592..03ab0974ec 100644 --- a/src/pkg/github/automerge/automerge_sweep.go +++ b/src/pkg/github/automerge/automerge_sweep.go @@ -94,9 +94,6 @@ type Options struct { // auto_merge.merge_forward_trusted_human membership for owner/repo // (hivecommons/hive#13636); nil means off. MergeForwardTrustedHuman func(repo string) bool - // MergeTrainEnabled returns the live auto_merge.merge_train.enabled - // switch (hivecommons/hive#15117). nil means on. - MergeTrainEnabled func() bool // SelfAuthorizationHoldEnabled returns the live per-repo #5117 hold switch. // nil preserves the default-on behavior. SelfAuthorizationHoldEnabled func(repo string) bool @@ -184,6 +181,16 @@ type Options struct { // SweepTrigger is the trigger webhook deliveries notify; nil with OpenPRs // set builds one, reachable through Engine.Trigger. SweepTrigger *SweepTrigger + // MergeTrain returns the live auto_merge.merge_train.* policy + // (hivecommons/hive#15117); nil means DefaultMergeTrainPolicy. + MergeTrain func() MergeTrainPolicy + // MergeTrainEnabled is the on/off-only form of MergeTrain kept for + // callers wired before the policy existed (#15149); ignored when + // MergeTrain is set. + MergeTrainEnabled func() bool + // RebaseResolver receives the train's rebase-resolve requests for a + // conflicting PR; nil leaves a dirty front on its conflicting path. + RebaseResolver RebaseResolver } // Engine owns the automerge sweep policy state. @@ -223,9 +230,6 @@ type Engine struct { requireCurrentBase func(repo string) bool mergeForwardTrustedHuman func(repo string) bool - mergeTrainEnabledFn func() bool - // mergeTrain is the merge-train memory (automerge_merge_train.go). - mergeTrain mergeTrainState selfAuthorizationHoldEnabled func(repo string) bool selfAuthorizationHoldReleaseLimit int @@ -301,6 +305,10 @@ type Engine struct { sweepFloor time.Duration // sweepClock records the lanes' passes for the dashboard (sweep_clock.go). sweepClock *SweepClock + + mergeTrainPolicy func() MergeTrainPolicy + rebaseResolver RebaseResolver + mergeTrainState } // New returns an automerge sweep engine over a GitHub transport client. @@ -325,7 +333,6 @@ func New(transport Transport, opts Options) *Engine { humanMergePaths: opts.HumanMergePaths, requireCurrentBase: opts.RequireCurrentBase, mergeForwardTrustedHuman: opts.MergeForwardTrustedHuman, - mergeTrainEnabledFn: opts.MergeTrainEnabled, selfAuthorizationHoldEnabled: opts.SelfAuthorizationHoldEnabled, selfAuthorizationHoldReleaseLimit: opts.SelfAuthorizationHoldReleaseLimit, repoAutoMergeEnabled: opts.RepoAutoMergeEnabled, @@ -351,6 +358,8 @@ func New(transport Transport, opts Options) *Engine { checkSummary: opts.CheckSummary, openPRs: opts.OpenPRs, sweepTrigger: opts.SweepTrigger, + mergeTrainPolicy: mergeTrainPolicyOption(opts), + rebaseResolver: opts.RebaseResolver, evaluatedHeads: make(map[string]string), requiredChecksFallbackWarned: make(map[string]bool), } @@ -1041,14 +1050,12 @@ func (c *Engine) SweepSelfAuthoredAutoMerges(ctx context.Context, opts AutoMerge c.info("self-authored automerge sweep skipped repo", "repo", repo, "reason", skipReason) continue } - trains := c.mergeTrainSlots(ctx, repo, owner, repoName, prs, func(pr *gh.PullRequest) bool { - return c.prefilterSelfAuthoredPR(pr) == "" - }) repoSeen := 0 repoSkipped := 0 repoCandidates := 0 repoMergedBefore := len(result.Merged) repoSkipReasons := make(map[string]int) + train := c.planMergeTrains(ctx, stageLaneSelf, repo, owner, repoName, prs, c.prefilterSelfAuthoredPR) for _, pr := range prs { if len(result.Merged) >= maxMerges { break @@ -1062,6 +1069,21 @@ func (c *Engine) SweepSelfAuthoredAutoMerges(ctx context.Context, opts AutoMerge stage := func(reason string, merged bool, err error) { c.recordStage(stageLaneSelf, repo, owner, repoName, pr, reason, merged, err) } + if train.isSibling(number) { + result.Skipped++ + repoSkipped++ + repoSkipReasons[autoMergeReasonMergeTrainQueued]++ + stage(c.mergeTrainSkip(train, number), false, nil) + continue + } + if reason, handled := c.mergeTrainFront(ctx, train, pr); handled { + c.info("automerge skip", "repo", repo, "pr", number, "reason", reason) + result.Skipped++ + repoSkipped++ + repoSkipReasons[autoMergeReasonMergeTrainRebasing]++ + stage(reason, false, nil) + continue + } // #5117 hold release must run before the prefilter: the // prefilter skips held PRs outright, and an eligible // self-authorization hold has to be released, not skipped. @@ -1131,23 +1153,12 @@ func (c *Engine) SweepSelfAuthoredAutoMerges(ctx context.Context, opts AutoMerge stage(reason, false, nil) continue } - if _, queued := mergeTrainQueued(trains, number); queued { - // The stage is left as last recorded: the train does not - // re-evaluate a waiting sibling, so it has no new verdict. - result.Skipped++ - repoSkipped++ - repoSkipReasons[autoMergeReasonMergeTrainQueued]++ - continue - } result.Candidates++ repoCandidates++ event, reason, err := c.trySweepSelfAuthoredPR(ctx, repo, owner, repoName, number, branchUpdateAttempts < selfAuthoredSweepMaxBranchUpdates) if event.BranchUpdated || reason == "updated-branch" || reason == "update-branch" { branchUpdateAttempts++ } - if err == nil { - c.noteMergeTrainOutcome(owner, repoName, number, pr.GetHead().GetSHA(), reason) - } if err != nil { c.warnSelfAuthoredSkipOnce(repo, number, reason, err) result.Skipped++ @@ -1159,6 +1170,7 @@ func (c *Engine) SweepSelfAuthoredAutoMerges(ctx context.Context, opts AutoMerge if event.BranchUpdated || reason == "updated-branch" { result.UpdatedBranches++ } + c.noteMergeTrainOutcome(owner, repoName, number, pr.GetHead().GetSHA(), reason) if reason != "" { if isTerminalRequiredCheckHold(reason) || isPermanentMergeRefusal(reason) { if c.markTerminalHoldLogged(owner, repoName, number, pr.GetHead().GetSHA(), reason) { @@ -1178,6 +1190,9 @@ func (c *Engine) SweepSelfAuthoredAutoMerges(ctx context.Context, opts AutoMerge if opts.Audit != nil { opts.Audit(event) } + if train.isFront(number) { + c.mergeTrainAfterFrontMerge(ctx, train, number) + } } if repoSeen > 0 || repoCandidates > 0 || len(result.Merged) > repoMergedBefore { args := []any{ @@ -1188,7 +1203,7 @@ func (c *Engine) SweepSelfAuthoredAutoMerges(ctx context.Context, opts AutoMerge "updated_branches", result.UpdatedBranches, "skipped", repoSkipped, } - skipReasonOrder := []string{"label:hold", "label:needs-human", "label:needs-rebase", "held", "exempt-label", "draft", "closed", "not-app-authored", "missing-head-sha", "updated-branch", autoMergeReasonBehindBudgetExhausted, "conflicting", autoMergeReasonForkNotEditable, "not-mergeable", autoMergeReasonClosedSuperseded, autoMergeReasonMergeQueueRequired, autoMergeReasonMergeRefusedReview, autoMergeReasonMergeRefusedMethod, autoMergeReasonMergeTrainQueued} + skipReasonOrder := []string{"label:hold", "label:needs-human", "label:needs-rebase", "held", "exempt-label", "draft", "closed", "not-app-authored", "missing-head-sha", "updated-branch", autoMergeReasonBehindBudgetExhausted, "conflicting", autoMergeReasonForkNotEditable, "not-mergeable", autoMergeReasonClosedSuperseded, autoMergeReasonMergeQueueRequired, autoMergeReasonMergeRefusedReview, autoMergeReasonMergeRefusedMethod, autoMergeReasonMergeTrainQueued, autoMergeReasonMergeTrainRebasing} if sentinelLabel := c.sentinelLabelName(); sentinelLabel != "" { skipReasonOrder = append([]string{sentinelLabel}, skipReasonOrder...) } @@ -1252,15 +1267,15 @@ func (c *Engine) SweepTrustedAuthorAutoMerges(ctx context.Context, opts AutoMerg c.info("trusted-author automerge sweep skipped repo", "repo", displayRepo, "reason", skipReason) continue } - trains := c.mergeTrainSlots(ctx, displayRepo, owner, repoName, prs, func(pr *gh.PullRequest) bool { - author := hgithub.SafeGetLogin(pr.GetUser()) - if opts.SelfAuthoredLaneActive && c.isTrustedBotLaneAuthor(author) { - return false + train := c.planMergeTrains(ctx, stageLaneTrusted, displayRepo, owner, repoName, prs, func(pr *gh.PullRequest) string { + login := hgithub.SafeGetLogin(pr.GetUser()) + if opts.SelfAuthoredLaneActive && c.isTrustedBotLaneAuthor(login) { + return trustedBotLaneSkipReason } - if lane, _ := c.sweepLaneForAuthor(author); lane == stageLaneSelf { - return false + if lane, _ := c.sweepLaneForAuthor(login); lane == stageLaneSelf { + return trustedAuthorReasonAppAuthored } - return c.prefilterTrustedAuthorPR(pr, policy) == "" + return c.prefilterTrustedAuthorPR(pr, policy) }) for _, pr := range prs { if len(result.Merged) >= maxMerges { @@ -1274,6 +1289,17 @@ func (c *Engine) SweepTrustedAuthorAutoMerges(ctx context.Context, opts AutoMerg stage := func(reason string, merged bool, err error) { c.recordStage(stageLaneTrusted, displayRepo, owner, repoName, pr, reason, merged, err) } + if train.isSibling(number) { + result.Skipped++ + stage(c.mergeTrainSkip(train, number), false, nil) + continue + } + if reason, handled := c.mergeTrainFront(ctx, train, pr); handled { + c.info("automerge skip", "repo", displayRepo, "pr", number, "reason", reason, "tier", "trusted-author") + result.Skipped++ + stage(reason, false, nil) + continue + } if opts.SelfAuthoredLaneActive && c.isTrustedBotLaneAuthor(hgithub.SafeGetLogin(pr.GetUser())) { // The self-authored sweep owns trusted-bot PRs and records // their eligibility verdict; the tier hand-off is logged here @@ -1301,11 +1327,6 @@ func (c *Engine) SweepTrustedAuthorAutoMerges(ctx context.Context, opts AutoMerg stage(reason, false, nil) continue } - if _, queued := mergeTrainQueued(trains, number); queued { - // See the self-authored lane: a waiting sibling keeps its stage. - result.Skipped++ - continue - } result.Candidates++ event, reason, err := c.trySweepTrustedAuthorPR(ctx, displayRepo, owner, repoName, number, policy) if err == nil { @@ -1328,6 +1349,9 @@ func (c *Engine) SweepTrustedAuthorAutoMerges(ctx context.Context, opts AutoMerg if opts.Audit != nil { opts.Audit(event) } + if train.isFront(number) { + c.mergeTrainAfterFrontMerge(ctx, train, number) + } } } return result, nil @@ -3115,3 +3139,16 @@ func (c *Engine) debug(msg string, args ...any) { c.logger.Debug(msg, args...) } } + +// mergeTrainPolicyOption folds the legacy MergeTrainEnabled switch into +// the policy func when no full policy is wired. +func mergeTrainPolicyOption(opts Options) func() MergeTrainPolicy { + if opts.MergeTrain != nil || opts.MergeTrainEnabled == nil { + return opts.MergeTrain + } + return func() MergeTrainPolicy { + p := DefaultMergeTrainPolicy + p.Enabled = opts.MergeTrainEnabled() + return p + } +} diff --git a/src/pkg/github/automerge/automerge_sweep_test.go b/src/pkg/github/automerge/automerge_sweep_test.go index 39bf1d71f0..b437b9a2fe 100644 --- a/src/pkg/github/automerge/automerge_sweep_test.go +++ b/src/pkg/github/automerge/automerge_sweep_test.go @@ -1760,25 +1760,6 @@ type selfAuthoredPR struct { // okToTestWorkflowStatus is the status GET /contents/.github/workflows/hive-ok-to-test.yml // answers with (0 = 404). okToTestWorkflowStatus int - // files is GET /pulls/{n}/files; unset is one file no other PR touches. - files []string - // createdAt, when set, is the PR's created_at (the merge train's age order). - createdAt string - // filesFetches, when set, counts GET /pulls/{n}/files calls. - filesFetches *int -} - -// prFilesResponse renders a fake GET /pulls/{n}/files payload; no files means -// one path unique to the PR, so the merge train never groups it. -func prFilesResponse(number int, files []string) []map[string]any { - if len(files) == 0 { - files = []string{"pr" + strconv.Itoa(number) + ".go"} - } - out := make([]map[string]any, 0, len(files)) - for _, f := range files { - out = append(out, map[string]any{"filename": f, "status": "modified"}) - } - return out } func newSelfAuthoredAutoMergeAPI(t *testing.T, prs []selfAuthoredPR, merged *[]int, getCounts ...*map[int]int) *httptest.Server { @@ -1796,18 +1777,14 @@ func newSelfAuthoredAutoMergeAPI(t *testing.T, prs []selfAuthoredPR, merged *[]i case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/pulls": var out []map[string]any for _, pr := range prs { - item := map[string]any{ + out = append(out, map[string]any{ "number": pr.number, "state": "open", "draft": pr.draft, "user": map[string]string{"login": pr.author}, "head": map[string]string{"sha": "sha" + strconv.Itoa(pr.number)}, "labels": issueLabels("", pr.extraLabels), - } - if pr.createdAt != "" { - item["created_at"] = pr.createdAt - } - out = append(out, item) + }) } json.NewEncoder(w).Encode(out) case r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/repos/acme/widget/pulls/") && strings.HasSuffix(r.URL.Path, "/reviews"): @@ -1815,12 +1792,9 @@ func newSelfAuthoredAutoMergeAPI(t *testing.T, prs []selfAuthoredPR, merged *[]i // human's changes_requested (hivecommons/hive#13041). json.NewEncoder(w).Encode([]map[string]any{}) case r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/repos/acme/widget/pulls/") && strings.HasSuffix(r.URL.Path, "/files"): - number := pathNumber(t, r.URL.Path, "/repos/acme/widget/pulls/", "/files") - pr := byNumber[number] - if pr.filesFetches != nil { - (*pr.filesFetches)++ - } - json.NewEncoder(w).Encode(prFilesResponse(number, pr.files)) + // The merge train lists changed files to group overlapping PRs + // (hivecommons/hive#15117); disjoint here, so no train forms. + json.NewEncoder(w).Encode([]map[string]any{}) case r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/repos/acme/widget/pulls/"): number := pathNumber(t, r.URL.Path, "/repos/acme/widget/pulls/", "") pr := byNumber[number] @@ -2944,8 +2918,6 @@ func newAutoMergeSweepAPI(t *testing.T, expectedLabel string, prs []sweepPR, mer "commit_id": reviewCommitID, "user": map[string]string{"login": reviewAuthor}, }}) - case r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/repos/acme/widget/pulls/") && strings.HasSuffix(r.URL.Path, "/files"): - json.NewEncoder(w).Encode(prFilesResponse(pathNumber(t, r.URL.Path, "/repos/acme/widget/pulls/", "/files"), nil)) case r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/repos/acme/widget/pulls/"): number := pathNumber(t, r.URL.Path, "/repos/acme/widget/pulls/", "") pr := byNumber[number] diff --git a/src/pkg/stageengine/derive_pr_classify.go b/src/pkg/stageengine/derive_pr_classify.go index d211c675b5..8a9f296c56 100644 --- a/src/pkg/stageengine/derive_pr_classify.go +++ b/src/pkg/stageengine/derive_pr_classify.go @@ -359,6 +359,15 @@ func ClassifyStage(o stageObservation) stageVerdict { if stageHandoffReasons[reason] { return stageVerdict{} } + if train, ok := prstage.ParseMergeTrain(reason); ok { + // The merge train parked this PR behind an overlapping front + // (hivecommons/hive#15117): a hive-side wait at the merge gate, + // never a human stage — unless the train gave up rebasing it. + if train.State == prstage.MergeTrainStateRebaseExhausted { + return stageVerdict{Stage: stagelabels.NeedsDecision, Rank: stageRankOwned, Human: true} + } + return WithHumanApprovalReason(stageVerdict{Stage: stagelabels.AwaitingMergeAdmission, Rank: stageRankOwned}, humanApprovalReason) + } if rank := stageAdmissionReasons[reason]; rank != stageRankNone { if HasLabelFold(o.Labels, "needs-rebase") { return stageVerdict{Stage: fixing, Rank: stageRankOwned} diff --git a/src/pkg/stageengine/derive_pr_merge_train_test.go b/src/pkg/stageengine/derive_pr_merge_train_test.go new file mode 100644 index 0000000000..4ece54d8ee --- /dev/null +++ b/src/pkg/stageengine/derive_pr_merge_train_test.go @@ -0,0 +1,33 @@ +package stageengine + +import ( + "testing" + + "github.com/hivecommons/hive/pkg/stageengine/prstage" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// hivecommons/hive#15117: a PR the merge train parked behind an overlapping +// front stays at the merge gate (080) in the hive's hands — even while it +// wears needs-rebase, which the train repairs itself — and only the loop +// guard giving up makes it a person's row (085). +func TestClassifyStageMergeTrainReasons(t *testing.T) { + files := []string{"stage_tracker.go", "index.html"} + for _, lane := range []string{stageLaneSelf, stageLaneTrusted, stageLaneContributor} { + for _, tc := range []struct { + name string + reason string + want stagelabels.Stage + human bool + }{ + {"queued-behind", prstage.MergeTrain{State: prstage.MergeTrainStateQueuedBehind, Front: 15074, Position: 3, Size: 4, OverlapFiles: files}.Reason(), stagelabels.AwaitingMergeAdmission, false}, + {"rebasing", prstage.MergeTrain{State: prstage.MergeTrainStateRebasing, Front: 15074, Position: 2, Size: 4, Attempt: 1, MaxAttempts: 2, OverlapFiles: files}.Reason(), stagelabels.AwaitingMergeAdmission, false}, + {"exhausted", prstage.MergeTrain{State: prstage.MergeTrainStateRebaseExhausted, Front: 15074, Position: 2, Size: 4, Attempt: 2, MaxAttempts: 2, OverlapFiles: files}.Reason(), stagelabels.NeedsDecision, true}, + } { + v := ClassifyStage(stageObservation{Lane: lane, Reason: tc.reason, HeadSHA: goldenHead, Labels: []string{"needs-rebase"}}) + if v.Stage != tc.want || v.Rank != stageRankOwned || v.Human != tc.human || v.Terminal { + t.Fatalf("%s/%s classified as %+v, want stage %v human %v", lane, tc.name, v, tc.want, tc.human) + } + } + } +} diff --git a/src/pkg/stageengine/prstage/merge_train.go b/src/pkg/stageengine/prstage/merge_train.go new file mode 100644 index 0000000000..8fe6372b52 --- /dev/null +++ b/src/pkg/stageengine/prstage/merge_train.go @@ -0,0 +1,187 @@ +package prstage + +import ( + "path" + "strconv" + "strings" +) + +// Merge-train reasons (hivecommons/hive#15117). The automerge sweep groups +// candidate PRs on the same base whose changed files overlap, merges one +// front at a time and parks the rest behind it. The park is recorded as a +// lane reason so the stage engine (080 merge gate, never a human stage) and +// the dashboard (the "In merge train · 3rd of 4" chevron word) read the +// same fact. Wire format: +// +// merge-train:#:/:/:,[,+N] +// +// where is one of the MergeTrainState* constants, is the +// PR number the train merges first, is this PR's 1-based slot, +// / counts the rebase attempts the train has spent on this +// PR behind this front (0/0 when none applies) and the files are the base +// names of the overlapping paths (at most MergeTrainReasonMaxFiles, the +// rest counted as "+N"). +const ( + MergeTrainReasonPrefix = "merge-train:" + // MergeTrainStateQueuedBehind: a sibling waiting for the front to merge. + MergeTrainStateQueuedBehind = "queued-behind" + // MergeTrainStateRebasing: the train's front is conflicting and the + // hive has asked its rebase-resolve lane to rebase it onto the base. + MergeTrainStateRebasing = "rebasing" + // MergeTrainStateRebaseExhausted: the front stayed conflicting after + // the configured number of rebase attempts; a person decides (085). + MergeTrainStateRebaseExhausted = "rebase-exhausted" + // MergeTrainStateFront is never written by the sweep: the dashboard + // synthesises it on the PR the siblings name as their front. + MergeTrainStateFront = "front" + MergeTrainReasonMaxFiles = 4 +) + +// MergeTrain is the decoded form of a merge-train reason. +type MergeTrain struct { + State string `json:"state"` + Front int `json:"front"` + Position int `json:"position"` + Size int `json:"size"` + // Attempt / MaxAttempts count the rebases the train has asked for on + // this PR behind this front; both 0 when the PR is merely queued. + Attempt int `json:"attempt,omitempty"` + MaxAttempts int `json:"max_attempts,omitempty"` + OverlapFiles []string `json:"overlap_files,omitempty"` +} + +// Reason encodes the train position as a lane reason. +func (m MergeTrain) Reason() string { + var b strings.Builder + b.WriteString(MergeTrainReasonPrefix) + b.WriteString(m.State) + b.WriteByte('#') + b.WriteString(strconv.Itoa(m.Front)) + b.WriteByte(':') + b.WriteString(strconv.Itoa(m.Position)) + b.WriteByte('/') + b.WriteString(strconv.Itoa(m.Size)) + b.WriteByte(':') + b.WriteString(strconv.Itoa(m.Attempt)) + b.WriteByte('/') + b.WriteString(strconv.Itoa(m.MaxAttempts)) + b.WriteByte(':') + b.WriteString(strings.Join(mergeTrainFileNames(m.OverlapFiles), ",")) + return b.String() +} + +// mergeTrainFileNames shortens overlapping paths to base names, capped at +// MergeTrainReasonMaxFiles with a "+N" tail, de-duplicated and in order. +func mergeTrainFileNames(files []string) []string { + seen := map[string]bool{} + out := make([]string, 0, len(files)) + for _, f := range files { + name := path.Base(strings.TrimSpace(f)) + if name == "" || name == "." || seen[name] { + continue + } + seen[name] = true + out = append(out, name) + } + if len(out) > MergeTrainReasonMaxFiles { + rest := len(out) - MergeTrainReasonMaxFiles + out = append(out[:MergeTrainReasonMaxFiles:MergeTrainReasonMaxFiles], "+"+strconv.Itoa(rest)) + } + return out +} + +// ParseMergeTrain decodes a merge-train reason (with or without the +// `:human-approved@login` suffix). ok is false for any other reason. +func ParseMergeTrain(reason string) (m MergeTrain, ok bool) { + rest, found := strings.CutPrefix(strings.TrimSpace(reason), MergeTrainReasonPrefix) + if !found { + return MergeTrain{}, false + } + if i := strings.Index(rest, ":human-approved@"); i >= 0 { + rest = rest[:i] + } + state, rest, found := strings.Cut(rest, "#") + if !found || state == "" { + return MergeTrain{}, false + } + front, rest, found := strings.Cut(rest, ":") + if !found { + return MergeTrain{}, false + } + slot, rest, found := strings.Cut(rest, ":") + if !found { + return MergeTrain{}, false + } + pos, size, found := strings.Cut(slot, "/") + if !found { + return MergeTrain{}, false + } + tries, files, _ := strings.Cut(rest, ":") + attempt, maxAttempts, found := strings.Cut(tries, "/") + if !found { + return MergeTrain{}, false + } + var err error + if m.Front, err = strconv.Atoi(front); err != nil { + return MergeTrain{}, false + } + if m.Position, err = strconv.Atoi(pos); err != nil { + return MergeTrain{}, false + } + if m.Size, err = strconv.Atoi(size); err != nil { + return MergeTrain{}, false + } + if m.Attempt, err = strconv.Atoi(attempt); err != nil { + return MergeTrain{}, false + } + if m.MaxAttempts, err = strconv.Atoi(maxAttempts); err != nil { + return MergeTrain{}, false + } + m.State = state + for _, f := range strings.Split(files, ",") { + if f = strings.TrimSpace(f); f != "" { + m.OverlapFiles = append(m.OverlapFiles, f) + } + } + return m, true +} + +// IsMergeTrainReason reports whether reason carries a merge-train position. +func IsMergeTrainReason(reason string) bool { + return strings.HasPrefix(strings.TrimSpace(reason), MergeTrainReasonPrefix) +} + +// Ordinal renders a 1-based position as "1st", "2nd", "3rd", "4th", …. +func Ordinal(n int) string { + suffix := "th" + switch { + case n%100 >= 11 && n%100 <= 13: + case n%10 == 1: + suffix = "st" + case n%10 == 2: + suffix = "nd" + case n%10 == 3: + suffix = "rd" + } + return strconv.Itoa(n) + suffix +} + +// Sentence is the one-line explanation the dashboard gate cell and the +// sweep log share: "behind #15074 in merge train (3rd of 4, overlapping +// files: stage_tracker.go, index.html)". +func (m MergeTrain) Sentence() string { + files := "" + if len(m.OverlapFiles) > 0 { + files = ", overlapping files: " + strings.Join(m.OverlapFiles, ", ") + } + slot := Ordinal(m.Position) + " of " + strconv.Itoa(m.Size) + switch m.State { + case MergeTrainStateRebasing: + return "front of the merge train, conflicting; the hive is rebasing it onto the base (attempt " + strconv.Itoa(m.Attempt) + "/" + strconv.Itoa(m.MaxAttempts) + "; " + slot + files + ")" + case MergeTrainStateRebaseExhausted: + return "front of the merge train, still conflicting after " + strconv.Itoa(m.MaxAttempts) + " rebase attempts; a person decides (" + slot + files + ")" + case MergeTrainStateFront: + return "front of the merge train (" + slot + files + ")" + } + return "behind #" + strconv.Itoa(m.Front) + " in merge train (" + slot + files + ")" +} diff --git a/src/pkg/stageengine/prstage/merge_train_test.go b/src/pkg/stageengine/prstage/merge_train_test.go new file mode 100644 index 0000000000..f600816d84 --- /dev/null +++ b/src/pkg/stageengine/prstage/merge_train_test.go @@ -0,0 +1,105 @@ +package prstage + +import ( + "reflect" + "strconv" + "testing" +) + +func TestMergeTrainReasonRoundTrip(t *testing.T) { + for _, tc := range []struct { + name string + in MergeTrain + want string + }{ + {name: "queued", in: MergeTrain{State: MergeTrainStateQueuedBehind, Front: 15074, Position: 3, Size: 4, OverlapFiles: []string{"src/pkg/dashboard/stage_tracker.go", "src/pkg/dashboard/static/index.html"}}, + want: "merge-train:queued-behind#15074:3/4:0/0:stage_tracker.go,index.html"}, + {name: "rebasing", in: MergeTrain{State: MergeTrainStateRebasing, Front: 7, Position: 1, Size: 2, Attempt: 1, MaxAttempts: 2, OverlapFiles: []string{"a.go"}}, + want: "merge-train:rebasing#7:1/2:1/2:a.go"}, + {name: "files capped", in: MergeTrain{State: MergeTrainStateQueuedBehind, Front: 1, Position: 2, Size: 2, OverlapFiles: []string{"a", "b", "c", "d", "e", "f"}}, + want: "merge-train:queued-behind#1:2/2:0/0:a,b,c,d,+2"}, + } { + t.Run(tc.name, func(t *testing.T) { + got := tc.in.Reason() + if got != tc.want { + t.Fatalf("Reason() = %q, want %q", got, tc.want) + } + back, ok := ParseMergeTrain(got + ":human-approved@pat") + if !ok { + t.Fatalf("ParseMergeTrain(%q) failed", got) + } + want := tc.in + want.OverlapFiles = mergeTrainFileNames(tc.in.OverlapFiles) + if !reflect.DeepEqual(back, want) { + t.Fatalf("round trip = %+v, want %+v", back, want) + } + }) + } + for _, bad := range []string{"", "conflicting", "merge-train:", "merge-train:queued-behind", "merge-train:queued-behind#x:1/2:0/0:", "merge-train:queued-behind#1:1:0/0:"} { + if _, ok := ParseMergeTrain(bad); ok { + t.Fatalf("ParseMergeTrain(%q) must fail", bad) + } + } +} + +func TestMergeTrainSentence(t *testing.T) { + m := MergeTrain{State: MergeTrainStateQueuedBehind, Front: 15074, Position: 3, Size: 4, OverlapFiles: []string{"stage_tracker.go", "index.html"}} + if got, want := m.Sentence(), "behind #15074 in merge train (3rd of 4, overlapping files: stage_tracker.go, index.html)"; got != want { + t.Fatalf("Sentence() = %q, want %q", got, want) + } + for n, want := range map[int]string{1: "1st", 2: "2nd", 3: "3rd", 4: "4th", 11: "11th", 12: "12th", 13: "13th", 21: "21st", 22: "22nd", 103: "103rd", 111: "111th"} { + if got := Ordinal(n); got != want { + t.Fatalf("Ordinal(%d) = %q, want %q", n, got, want) + } + } +} + +func TestParseMergeTrainRejectsMalformed(t *testing.T) { + for _, reason := range []string{ + "held", + MergeTrainReasonPrefix + "queued-behind", + MergeTrainReasonPrefix + "#7:2/4:0/2", + MergeTrainReasonPrefix + "queued-behind#7", + MergeTrainReasonPrefix + "queued-behind#7:2", + MergeTrainReasonPrefix + "queued-behind#7:2:0/2", + MergeTrainReasonPrefix + "queued-behind#7:2/4:0", + MergeTrainReasonPrefix + "queued-behind#x:2/4:0/2", + MergeTrainReasonPrefix + "queued-behind#7:x/4:0/2", + MergeTrainReasonPrefix + "queued-behind#7:2/x:0/2", + MergeTrainReasonPrefix + "queued-behind#7:2/4:x/2", + MergeTrainReasonPrefix + "queued-behind#7:2/4:0/x", + } { + if m, ok := ParseMergeTrain(reason); ok { + t.Errorf("ParseMergeTrain(%q) = %+v, want !ok", reason, m) + } + if reason != "held" && !IsMergeTrainReason(reason) { + t.Errorf("IsMergeTrainReason(%q) must be true by prefix", reason) + } + } + if IsMergeTrainReason("held") { + t.Fatal("a plain reason is no train") + } + // The human-approved suffix is stripped, duplicate and empty file names dropped. + m, ok := ParseMergeTrain(MergeTrain{State: MergeTrainStateQueuedBehind, Front: 7, Position: 2, Size: 4, OverlapFiles: []string{"src/a.go", "pkg/a.go", " ", "."}}.Reason() + ":human-approved@pat") + if !ok || m.Front != 7 || len(m.OverlapFiles) != 1 || m.OverlapFiles[0] != "a.go" { + t.Fatalf("parsed %+v ok=%v", m, ok) + } + many := make([]string, 0, MergeTrainReasonMaxFiles+3) + for i := 0; i < MergeTrainReasonMaxFiles+3; i++ { + many = append(many, "f"+strconv.Itoa(i)+".go") + } + m, _ = ParseMergeTrain(MergeTrain{State: MergeTrainStateQueuedBehind, Front: 7, Position: 2, Size: 9, OverlapFiles: many}.Reason()) + if len(m.OverlapFiles) != MergeTrainReasonMaxFiles+1 || m.OverlapFiles[MergeTrainReasonMaxFiles] != "+3" { + t.Fatalf("files capped wrong: %v", m.OverlapFiles) + } + for state, want := range map[string]string{ + MergeTrainStateRebasing: "front of the merge train, conflicting; the hive is rebasing it onto the base (attempt 1/2; 1st of 3)", + MergeTrainStateRebaseExhausted: "front of the merge train, still conflicting after 2 rebase attempts; a person decides (1st of 3)", + MergeTrainStateFront: "front of the merge train (1st of 3)", + MergeTrainStateQueuedBehind: "behind #7 in merge train (1st of 3)", + } { + if got := (MergeTrain{State: state, Front: 7, Position: 1, Size: 3, Attempt: 1, MaxAttempts: 2}).Sentence(); got != want { + t.Errorf("Sentence(%s) = %q, want %q", state, got, want) + } + } +}