diff --git a/changelog.d/added-merge-train-front.md b/changelog.d/added-merge-train-front.md index 26736b6c10..654848e1f7 100644 --- a/changelog.d/added-merge-train-front.md +++ b/changelog.d/added-merge-train-front.md @@ -1 +1 @@ -- Automerge merge train: approved PRs whose changed files overlap are grouped per repo and base branch, and only one front per group (mergeable first, then oldest) merges per sweep pass, so parallel approvals no longer dirty each other; `auto_merge.merge_train.enabled` (default `true`) turns it off (#15141). +- Conflict-aware automerge merge train ([#15117](https://github.com/hivecommons/hive/issues/15117), #15141): in every automerge lane (hive self-authored, trusted-author, contributor) approved PRs to one base whose changed files overlap are grouped per repo, and only one front per group (a head not last seen conflicting, red or held first, then the oldest) merges per sweep pass, so parallel approvals no longer dirty each other. Siblings wait at stage/080 as `merge_gate.state=queued-behind` ("In merge train · 3rd of 4", never a human stage); after the front merges the lane updates `behind` siblings 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 `true`) turns it off. diff --git a/src/cmd/hive/main.go b/src/cmd/hive/main.go index bef18159f9..153ee5015a 100644 --- a/src/cmd/hive/main.go +++ b/src/cmd/hive/main.go @@ -2548,6 +2548,10 @@ func (b *boot) bootAgentsWith(deps bootAgentsDeps) { 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() } + // Merge train rebase loop guard (#15117). 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.MergeTrainMaxRebaseAttempts = func() int { return b.cfg.AutoMerge.MergeTrain.EffectiveMaxRebaseAttempts() } // 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 @@ -9410,6 +9414,7 @@ func runAutoMergeSweepIfDue(ctx context.Context, ghClient *github.Client, cfg *c 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.MergeTrainMaxRebaseAttempts = func() int { return 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..0541ebdda0 100644 --- a/src/docs/operator-reference.md +++ b/src/docs/operator-reference.md @@ -434,7 +434,8 @@ advanced into the merge path. | `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.merge_train.enabled` | `true` | Conflict-aware merge train for approved PRs whose changed files overlap, in every automerge lane (hive self-authored, trusted-author, contributor) (#15141, [#15117](https://github.com/hivecommons/hive/issues/15117)). Each sweep groups the lane's candidate PRs per repo and base branch by overlapping changed files (`GET /pulls/{n}/files`, cached per head SHA) and lets 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 wait at stage/080 with lane reason `merge-train:queued-behind#:/:…` (tracker pill "In merge train · 3rd of 4", never a human stage); a PR whose files cannot be read stays out of every train. 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 a conflicting PR takes the plain `conflicting` path, logged once, and stops fronting its train). A group whose best member is itself blocked imposes no ordering (fail-open), so nobody waits behind a PR that is not moving. One INFO `automerge merge train` line per slot change, not per cycle. `false` merges every green PR in parallel as before. | +| `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", and it stops fronting its train so a sibling that can merge goes next. | | `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..68f4a59172 100644 --- a/src/pkg/config/automerge_merge_train_test.go +++ b/src/pkg/config/automerge_merge_train_test.go @@ -23,3 +23,28 @@ func TestMergeTrainAutoMergeConfigDefaultsOn(t *testing.T) { t.Fatal("zero AutoMergeConfig disables the merge train; want default on") } } + +func TestMergeTrainAutoMergeConfigDefaults(t *testing.T) { + off, on := false, true + tests := []struct { + name string + cfg MergeTrainAutoMergeConfig + wantEnabled bool + wantMax int + }{ + {"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 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..e89c3a7cc6 100644 --- a/src/pkg/config/config.go +++ b/src/pkg/config/config.go @@ -7938,8 +7938,10 @@ 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 + // MergeTrain orders approved PRs on the same base whose changed files + // overlap so they merge one at a time instead of dirtying each other: + // the siblings wait as "queued-behind" (stage 080, never a human stage) + // and are updated or rebased right after the front lands // (hivecommons/hive#15117). MergeTrain MergeTrainAutoMergeConfig `yaml:"merge_train,omitempty" json:"merge_train,omitempty"` } @@ -7951,13 +7953,28 @@ type MergeTrainAutoMergeConfig struct { // pass: the mergeable one first, then the oldest. nil/unset means ON; // an explicit `enabled: false` merges every green PR as before. 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..fd7b9f0091 100644 --- a/src/pkg/dashboard/settings_parity_allowlist.txt +++ b/src/pkg/dashboard/settings_parity_allowlist.txt @@ -157,6 +157,7 @@ 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.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..a683c59d63 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.mergeTrainSlots(ctx, stageLaneContributor, displayRepo, owner, repoName, prs, func(pr *gh.PullRequest) bool { + return c.mergeTrainCandidate(c.prefilterContributorLanePR(pr, policy)) + }) for _, pr := range prs { if len(result.Merged) >= maxMerges { break @@ -429,14 +432,29 @@ 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++ + c.noteMergeTrainFrontSkip(train, pr, reason) stage(reason, false, nil) continue } 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 +472,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..2a1be7ccfb 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,9 +11,35 @@ 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). +// +// Approved PRs to the same base that touch the same files were all merged in +// parallel; the first to land turned every sibling dirty and each lane then +// logged them `conflicting` / `label:needs-rebase` every cycle with nothing +// ordering them. The train makes the lane order them instead: +// +// - candidates on the same base whose changed files overlap (transitively) +// form one train (GET /pulls/{n}/files, cached by head SHA); +// - the train has one front — a head not last seen conflicting, red or +// held first, then the oldest — that the lane evaluates and merges as +// before; every sibling is parked as +// `merge-train:queued-behind#:/:…` (stage 080, never a +// human stage); a group whose best member is itself blocked imposes no +// ordering (fail-open), so nobody waits behind a PR that is not moving; +// - 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 +// conflicting front is sent to the same hook, and past the guard it +// parks at 085 for a person and stops fronting its train. +// +// Train state (files cache, blocked heads, attempt counters, last merged +// front, transition log) is in-memory per process; a restart re-plans. + // 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 @@ -19,6 +47,65 @@ import ( // front per group and leaves the rest for the next pass against the new base. const autoMergeReasonMergeTrainQueued = "merge-train-queued" +// autoMergeReasonMergeTrainRebasing is the tick-stat bucket of a conflicting +// front the train sent for a rebase (or gave up on); the per-PR stage reason +// is the full prstage.MergeTrain reason with the attempt count. +const autoMergeReasonMergeTrainRebasing = "merge-train-rebasing" + +// MergeTrainPolicy is the live auto_merge.merge_train.* policy. +type MergeTrainPolicy struct { + Enabled bool + MaxRebaseAttempts int +} + +// DefaultMergeTrainPolicy is the policy without Options hooks: 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 +} + +// 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) +} + +// 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 (which then stops it fronting its train) 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) +} + // mergeTrainMember is one candidate PR as the train planner sees it. type mergeTrainMember struct { Number int @@ -29,6 +116,12 @@ type mergeTrainMember struct { // mergeable member exists, so it cannot stall its siblings. Blocked bool Files []string + // Head and HeadSHA are the head branch and commit; MergeableState is + // GitHub's live mergeable_state, read for a front only (the list API + // does not carry it) to decide whether the train must rebase it. + Head string + HeadSHA string + MergeableState string } // mergeTrainSlot is a PR's place in its overlap group. Position 1 is the @@ -125,14 +218,139 @@ func uniqueStrings(in []string) []string { return out } +// mergeTrainGroup is one train as the lanes and the stage reasons see it: +// the members in slot order (front first) and the files at least two of them +// touch. It is a view over planMergeTrains' slots, not a second planner. +type mergeTrainGroup struct { + Base string + Order []int + OverlapFiles []string + members map[int]mergeTrainMember +} + +// 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 + } + } + return 0 +} + +// Siblings are the members behind the front, in order. +func (g mergeTrainGroup) Siblings() []int { + if len(g.Order) <= 1 { + return nil + } + return g.Order[1:] +} + +// mergeTrainGroups folds slots into one group per front, ordered by base and +// then front number so callers iterate deterministically. +func mergeTrainGroups(members []mergeTrainMember, slots map[int]mergeTrainSlot) []mergeTrainGroup { + byNumber := make(map[int]mergeTrainMember, len(members)) + for _, m := range members { + byNumber[m.Number] = m + } + byFront := make(map[int]*mergeTrainGroup) + var fronts []int + for n, slot := range slots { + g, ok := byFront[slot.Front] + if !ok { + g = &mergeTrainGroup{Base: byNumber[slot.Front].Base, Order: make([]int, slot.Size), members: make(map[int]mergeTrainMember, slot.Size)} + byFront[slot.Front] = g + fronts = append(fronts, slot.Front) + } + if slot.Position >= 1 && slot.Position <= len(g.Order) { + g.Order[slot.Position-1] = n + } + g.members[n] = byNumber[n] + g.OverlapFiles = append(g.OverlapFiles, slot.OverlapFiles...) + } + sort.Slice(fronts, func(a, b int) bool { + ga, gb := byFront[fronts[a]], byFront[fronts[b]] + if ga.Base != gb.Base { + return ga.Base < gb.Base + } + return fronts[a] < fronts[b] + }) + out := make([]mergeTrainGroup, 0, len(fronts)) + for _, f := range fronts { + g := byFront[f] + g.OverlapFiles = uniqueStrings(g.OverlapFiles) + sort.Strings(g.OverlapFiles) + out = append(out, *g) + } + return out +} + +// 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" +} + +// mergeTrainPlan is one lane's trains for one repository this pass. +type mergeTrainPlan struct { + lane, displayRepo, owner, repo string + slots map[int]mergeTrainSlot + groups []mergeTrainGroup + byNumber map[int]*mergeTrainGroup +} + +// 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 +} + +// isSibling reports whether number waits behind a front this pass. +func (p *mergeTrainPlan) isSibling(number int) bool { + if p == nil { + return false + } + _, queued := mergeTrainQueued(p.slots, number) + return queued +} + +// isFront reports whether number leads a train this pass. +func (p *mergeTrainPlan) isFront(number int) bool { + if p == nil { + return false + } + slot, ok := p.slots[number] + return ok && slot.Position == 1 +} + +// 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 +} + // 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. +// PR cached by head SHA, heads last seen blocked, the slot last logged per +// PR so a transition logs once rather than every pass, the rebase attempts +// per PR behind a front and the front that last merged per base. type mergeTrainState struct { - mu sync.Mutex - files map[string]mergeTrainFiles - blocked map[string]string - logged map[string]string + mu sync.Mutex + files map[string]mergeTrainFiles + blocked map[string]string + logged map[string]string + attempts map[string]int + merged map[string]int } type mergeTrainFiles struct { @@ -144,11 +362,36 @@ func (c *Engine) mergeTrainEnabled() bool { return c.mergeTrainEnabledFn == nil || c.mergeTrainEnabledFn() } +// mergeTrainPolicyNow reads the live policy. +func (c *Engine) mergeTrainPolicyNow() MergeTrainPolicy { + if c == nil { + return DefaultMergeTrainPolicy + } + p := MergeTrainPolicy{Enabled: c.mergeTrainEnabled()} + if c.mergeTrainMaxRebaseAttemptsFn != nil { + p.MaxRebaseAttempts = c.mergeTrainMaxRebaseAttemptsFn() + } + if p.MaxRebaseAttempts <= 0 { + p.MaxRebaseAttempts = DefaultMergeTrainPolicy.MaxRebaseAttempts + } + return p +} + +// mergeTrainCandidate reports whether a lane's prefilter verdict admits a PR +// to the train: a clean pass, or — when a rebase resolver can repair it — a +// `needs-rebase` label, the very PR the train exists to order and repair. +func (c *Engine) mergeTrainCandidate(prefilterReason string) bool { + if prefilterReason == "" { + return true + } + return prefilterReason == "label:needs-rebase" && c != nil && c.rebaseResolver != nil +} + // 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: + case "conflicting", "not-mergeable", "label:needs-rebase", autoMergeReasonForkNotEditable, autoMergeReasonClosedSuperseded: return true } return isTerminalRequiredCheckHold(reason) || isPermanentMergeRefusal(reason) || classifyCIReason(reason) == ciReasonRed @@ -173,6 +416,25 @@ func (c *Engine) noteMergeTrainOutcome(owner, repo string, number int, sha, reas c.mergeTrain.blocked[key] = sha } +// noteMergeTrainFrontSkip records a lane skip that stopped a train's front +// before its merge attempt, so a front that cannot move stops fronting. +func (c *Engine) noteMergeTrainFrontSkip(plan *mergeTrainPlan, pr *gh.PullRequest, reason string) { + if pr == nil || !plan.isFront(pr.GetNumber()) { + return + } + c.noteMergeTrainOutcome(plan.owner, plan.repo, pr.GetNumber(), pr.GetHead().GetSHA(), reason) +} + +// 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 || strings.TrimSpace(sha) == "" { + return false + } + c.mergeTrain.mu.Lock() + defer c.mergeTrain.mu.Unlock() + return c.mergeTrain.blocked[c.evaluatedHeadKey(owner, repo, number)] == sha +} + // 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) { @@ -201,13 +463,16 @@ func (c *Engine) mergeTrainChangedFiles(ctx context.Context, owner, repo string, return files, nil } -// 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 { +// mergeTrainSlots plans the merge trains for one lane's listed PRs on one +// repo. 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, and each +// train's front one live read of its mergeable state. A PR whose files cannot +// be read stays out of every train, so a lookup failure never holds a merge +// back. The plan is never nil; a disabled train plans nothing. +func (c *Engine) mergeTrainSlots(ctx context.Context, lane, displayRepo, owner, repo string, prs []*gh.PullRequest, eligible func(*gh.PullRequest) bool) *mergeTrainPlan { + plan := &mergeTrainPlan{lane: lane, displayRepo: displayRepo, owner: owner, repo: repo, slots: map[int]mergeTrainSlot{}, byNumber: map[int]*mergeTrainGroup{}} if c == nil || c.gh == nil || !c.mergeTrainEnabled() { - return nil + return plan } perBase := make(map[string][]*gh.PullRequest) for _, pr := range prs { @@ -237,27 +502,82 @@ func (c *Engine) mergeTrainSlots(ctx context.Context, displayRepo, owner, repo s } 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, + Created: pr.GetCreatedAt().Time, + Blocked: c.mergeTrainHeadBlocked(owner, repo, pr.GetNumber(), pr.GetHead().GetSHA()), + Files: files, + Head: pr.GetHead().GetRef(), + HeadSHA: pr.GetHead().GetSHA(), + MergeableState: pr.GetMergeableState(), }) } } slots := planMergeTrains(members) - c.logMergeTrainTransitions(displayRepo, owner, repo, slots, live, listed) - return slots + // Each front's live mergeable state decides whether the train must + // rebase it; a front closed since the listing leaves and the group + // re-plans. Bounded: every re-plan has one member fewer. + read := make(map[int]bool) + for { + closed := false + for n, slot := range slots { + if slot.Position != 1 || read[n] { + continue + } + read[n] = true + i := mergeTrainMemberIndex(members, n) + if i < 0 || members[i].MergeableState != "" { + continue + } + pr, _, err := c.gh.PullRequests.Get(hgithub.WithRESTCaller(ctx, "hive:automerge_merge_train"), owner, repo, n) + if err != nil { + c.warn("merge train could not read the front's state; treated as unknown", "repo", displayRepo, "pr", n, "error", err) + continue + } + if !strings.EqualFold(pr.GetState(), "open") { + members = append(members[:i], members[i+1:]...) + closed = true + continue + } + members[i].MergeableState = pr.GetMergeableState() + if sha := pr.GetHead().GetSHA(); sha != "" { + members[i].HeadSHA = sha + } + if ref := pr.GetHead().GetRef(); ref != "" { + members[i].Head = ref + } + } + if !closed { + break + } + slots = planMergeTrains(members) + } + plan.slots = slots + plan.groups = mergeTrainGroups(members, slots) + for i := range plan.groups { + g := &plan.groups[i] + for _, n := range g.Order { + plan.byNumber[n] = g + } + } + c.logMergeTrainTransitions(displayRepo, owner, repo, lane, slots, live, listed) + return plan +} + +func mergeTrainMemberIndex(members []mergeTrainMember, number int) int { + for i, m := range members { + if m.Number == number { + return i + } + } + return -1 } // 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) { +func (c *Engine) logMergeTrainTransitions(displayRepo, owner, repo, lane 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 { @@ -279,7 +599,7 @@ func (c *Engine) logMergeTrainTransitions(displayRepo, owner, repo string, slots 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, ",")}) + lines = append(lines, []any{"repo", displayRepo, "lane", lane, "pr", n, "front", slot.Front, "position", slot.Position, "size", slot.Size, "overlap_files", strings.Join(slot.OverlapFiles, ",")}) } for key := range c.mergeTrain.logged { if strings.HasPrefix(key, prefix) && !current[key] { @@ -302,8 +622,202 @@ func (c *Engine) logMergeTrainTransitions(displayRepo, owner, repo string, slots } } -// 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 +// mergeTrainAttemptKey keys the rebase loop guard: one PR behind one front. +func (c *Engine) mergeTrainAttemptKey(owner, repo string, number, front int) string { + return c.evaluatedHeadKey(owner, repo, number) + "@" + strconv.Itoa(front) +} + +func (c *Engine) mergeTrainAttempts(owner, repo string, number, front int) int { + c.mergeTrain.mu.Lock() + defer c.mergeTrain.mu.Unlock() + return c.mergeTrain.attempts[c.mergeTrainAttemptKey(owner, repo, number, front)] +} + +func (c *Engine) bumpMergeTrainAttempts(owner, repo string, number, front int) int { + c.mergeTrain.mu.Lock() + defer c.mergeTrain.mu.Unlock() + if c.mergeTrain.attempts == nil { + c.mergeTrain.attempts = make(map[string]int) + } + key := c.mergeTrainAttemptKey(owner, repo, number, front) + c.mergeTrain.attempts[key]++ + return c.mergeTrain.attempts[key] +} + +func mergeTrainBaseKey(owner, repo, base string) string { + return strings.ToLower(strings.TrimSpace(owner)) + "/" + strings.ToLower(strings.TrimSpace(repo)) + ":" + base +} + +// 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.mergeTrain.mu.Lock() + defer c.mergeTrain.mu.Unlock() + return c.mergeTrain.merged[mergeTrainBaseKey(owner, repo, base)] +} + +func (c *Engine) noteMergeTrainFrontMerged(owner, repo, base string, front int) { + c.mergeTrain.mu.Lock() + defer c.mergeTrain.mu.Unlock() + if c.mergeTrain.merged == nil { + c.mergeTrain.merged = make(map[string]int) + } + c.mergeTrain.merged[mergeTrainBaseKey(owner, repo, base)] = front +} + +// markMergeTrainHeadBlocked stops a head fronting its train until it changes. +func (c *Engine) markMergeTrainHeadBlocked(owner, repo string, number int, sha string) { + if strings.TrimSpace(sha) == "" { + return + } + c.mergeTrain.mu.Lock() + defer c.mergeTrain.mu.Unlock() + if c.mergeTrain.blocked == nil { + c.mergeTrain.blocked = make(map[string]string) + } + c.mergeTrain.blocked[c.evaluatedHeadKey(owner, repo, number)] = sha +} + +// 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 and +// is marked blocked, so the next pass fronts a sibling that can move. +// handled=false hands a clean or behind front to the lane's normal path, and +// a dirty one too when no resolver is wired (its `conflicting` verdict then +// marks it blocked through noteMergeTrainOutcome). +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 { + c.markMergeTrainHeadBlocked(plan.owner, plan.repo, number, m.HeadSHA) + 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) + } + 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 || c == nil || c.gh == 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 reason: 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, ok := plan.group(number) + if !ok { + return autoMergeReasonMergeTrainQueued + } + reason := mergeTrainSiblingReason(g, number) + c.debug("automerge skip", "repo", plan.displayRepo, "pr", number, "reason", reason, "lane", plan.lane) + return reason } diff --git a/src/pkg/github/automerge/automerge_merge_train_test.go b/src/pkg/github/automerge/automerge_merge_train_test.go index 0f1b5b80f8..92f9f00482 100644 --- a/src/pkg/github/automerge/automerge_merge_train_test.go +++ b/src/pkg/github/automerge/automerge_merge_train_test.go @@ -1,11 +1,23 @@ 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) { @@ -251,3 +263,464 @@ func TestSweepSelfAuthoredMergeTrainCachesFilesByHead(t *testing.T) { t.Fatalf("files fetches = #11:%d #12:%d, want one per unchanged head", fetches11, fetches12) } } + +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, Created: created, MergeableState: state} +} + +func TestMergeTrainGroups(t *testing.T) { + t0 := time.Date(2026, 10, 20, 9, 0, 0, 0, time.UTC) + cases := []struct { + name string + members []mergeTrainMember + want [][]int + overlap []string + }{ + { + name: "disjoint files form no train", + members: []mergeTrainMember{ + mtMember(1, "clean", t0, "a.go"), + mtMember(2, "clean", t0.Add(time.Minute), "b.go"), + }, + }, + { + name: "oldest approved leads", + members: []mergeTrainMember{ + 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: "a live dirty state does not reorder; the lane's blocked verdict does", + members: []mergeTrainMember{ + 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{{1, 2, 3}}, + overlap: []string{"tracker.go"}, + }, + { + name: "transitive overlap joins one train, a stranger stays out", + members: []mergeTrainMember{ + 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: "different bases never share a train", + members: []mergeTrainMember{ + mtMember(1, "clean", t0, "a.go"), + {Number: 2, Base: "main", Files: []string{"a.go"}, MergeableState: "clean", Created: t0}, + }, + }, + { + name: "equal ages tie on number", + members: []mergeTrainMember{ + mtMember(5, "", t0, "a.go"), + mtMember(4, "behind", t0, "a.go"), + mtMember(6, "", t0, "a.go"), + }, + want: [][]int{{4, 5, 6}}, + overlap: []string{"a.go"}, + }, + { + name: "a mergeable head fronts over an older blocked one", + members: []mergeTrainMember{ + blocked(mtMember(1, "clean", t0, "x.go")), + mtMember(2, "clean", t0.Add(time.Minute), "x.go"), + }, + want: [][]int{{2, 1}}, + overlap: []string{"x.go"}, + }, + { + name: "every member blocked holds nobody back (fail-open)", + members: []mergeTrainMember{ + 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) { + groups := mergeTrainGroups(tc.members, planMergeTrains(tc.members)) + var got [][]int + for _, g := range groups { + got = append(got, g.Order) + } + if !reflect.DeepEqual(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.Base != "v5" || 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("base/front/position/siblings wrong: %q %d %v", g.Base, g.Front(), g.Siblings()) + } + } + }) + } +} + +// 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.mergeTrainEnabledFn = func() bool { return policy.Enabled } + c.mergeTrainMaxRebaseAttemptsFn = func() int { return policy.MaxRebaseAttempts } + c.rebaseResolver = resolver + return c, &logs +} + +func admitAll(*gh.PullRequest) bool { return true } + +func plan0(c *Engine, prs []*gh.PullRequest) *mergeTrainPlan { + return c.mergeTrainSlots(context.Background(), "self", "acme/widget", "acme", "widget", prs, admitAll) +} + +func TestMergeTrainSlotsSiblingsAndFront(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: "behind", 12: "clean"}, + } + 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 the front's. + 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), "")} + plan := plan0(c, prs) + if !plan.isFront(11) || !plan.isSibling(10) || !plan.isSibling(12) || plan.isSibling(13) || plan.isFront(13) { + t.Fatalf("front/siblings wrong: front11=%v sib10=%v sib12=%v 13 in train=%v", plan.isFront(11), plan.isSibling(10), plan.isSibling(12), plan.isSibling(13) || plan.isFront(13)) + } + g, _ := plan.group(11) + if !reflect.DeepEqual(g.Order, []int{11, 10, 12}) { + t.Fatalf("order = %v, want oldest first", g.Order) + } + reason := c.mergeTrainSkip(plan, 12) + mt, ok := prstage.ParseMergeTrain(reason) + if !ok || mt.State != prstage.MergeTrainStateQueuedBehind || mt.Front != 11 || mt.Position != 3 || mt.Size != 3 { + t.Fatalf("sibling reason %q parsed %+v", reason, mt) + } + if got := mt.Sentence(); !strings.Contains(got, "behind #11 in merge train (3rd of 3, overlapping files: index.html, tracker.go)") { + t.Fatalf("sentence = %q", got) + } + // A front that is not conflicting is the lane's to merge on its normal path. + if r, handled := c.mergeTrainFront(context.Background(), plan, prs[1]); handled || r != "" { + t.Fatalf("behind front handled=%v reason=%q", handled, r) + } + // One INFO line per PR joining the train; a replan with the same slots + // adds none, and the files are served from the head-SHA cache. + lists, gets := api.lists, api.gets + _ = plan0(c, prs) + if n := strings.Count(logs.String(), `msg="automerge merge train"`); n != 3 { + t.Fatalf("transition lines = %d, want 3\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("the front's mergeable state must be re-read each pass") + } + // The lane saw the front conflict: the next pass fronts #10 and every + // slot change logs once. + c.noteMergeTrainOutcome("acme", "widget", 11, "sha11", "conflicting") + plan = plan0(c, prs) + if !plan.isFront(10) || !plan.isSibling(11) { + t.Fatalf("expected #10 to lead once #11 was seen conflicting; groups %v", plan.groups) + } + if n := strings.Count(logs.String(), `msg="automerge merge train"`); n != 6 { + t.Fatalf("transition lines = %d, want 6", n) + } +} + +func TestMergeTrainSlotsDisabledAndSingletons(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 := plan0(c, prs) + 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 = plan0(c, prs[:1]) + 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.mergeTrainSlots(context.Background(), "self", "acme/widget", "acme", "widget", prs, func(*gh.PullRequest) bool { return false }) + if len(plan.groups) != 0 { + t.Fatalf("prefiltered PRs joined a train") + } + if c.mergeTrainCandidate("label:needs-rebase") || c.mergeTrainCandidate("label:hold") || !c.mergeTrainCandidate("") { + t.Fatal("candidate gate without a resolver: only a clean prefilter admits") + } + c.rebaseResolver = RebaseResolverFunc(func(context.Context, RebaseRequest) error { return nil }) + if !c.mergeTrainCandidate("label:needs-rebase") || c.mergeTrainCandidate("label:hold") { + t.Fatal("candidate gate with a resolver: needs-rebase admits, hold does not") + } +} + +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"}, + } + 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 := plan0(c, prs) + 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 < 3; pass++ { + plan := plan0(c, prs) + 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"} + if !reflect.DeepEqual(states, want) { + t.Fatalf("states = %v, want %v", states, want) + } + // Past the guard the head stops fronting, so it cannot stall its train. + plan := plan0(c, prs) + if !plan.isFront(31) || !plan.isSibling(30) { + t.Fatalf("exhausted front still leads: groups %v", plan.groups) + } + 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(&plan.groups[0], prstage.MergeTrainStateRebaseExhausted, 2, 2)) + if mt.Sentence() == "" || !strings.Contains(mt.Sentence(), "after 2 rebase attempts") { + t.Fatalf("exhausted sentence = %q", mt.Sentence()) + } +} + +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()) + } + // 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 p := (&Engine{}).mergeTrainPolicyNow(); p != DefaultMergeTrainPolicy { + t.Fatalf("unhooked engine policy = %+v", p) + } + c = &Engine{mergeTrainMaxRebaseAttemptsFn: func() int { return 0 }, mergeTrainEnabledFn: func() bool { return false }} + if p := c.mergeTrainPolicyNow(); p.MaxRebaseAttempts != 2 || p.Enabled { + t.Fatalf("zero max attempts must default to 2 and the switch must read live, got %+v", p) + } + var plan *mergeTrainPlan + if plan.isFront(1) || plan.isSibling(1) { + t.Fatal("nil plan has no members") + } +} + +func blocked(m mergeTrainMember) mergeTrainMember { + m.Blocked = true + return m +} + +// TestMergeTrainBlockedHeadMemory: the lane's verdict per head decides +// whether it may front; a conflicting/red/terminal verdict marks the exact +// head, any other verdict clears it, and a new head starts clean. +func TestMergeTrainBlockedHeadMemory(t *testing.T) { + for reason, want := range map[string]bool{ + "": false, + "conflicting": true, + "label:needs-rebase": true, + "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) + } + } + 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..abf17426c8 100644 --- a/src/pkg/github/automerge/automerge_sweep.go +++ b/src/pkg/github/automerge/automerge_sweep.go @@ -97,6 +97,10 @@ type Options struct { // MergeTrainEnabled returns the live auto_merge.merge_train.enabled // switch (hivecommons/hive#15117). nil means on. MergeTrainEnabled func() bool + // MergeTrainMaxRebaseAttempts returns the live + // auto_merge.merge_train.max_rebase_attempts loop guard; nil or a + // non-positive value means DefaultMergeTrainPolicy.MaxRebaseAttempts. + MergeTrainMaxRebaseAttempts func() int // SelfAuthorizationHoldEnabled returns the live per-repo #5117 hold switch. // nil preserves the default-on behavior. SelfAuthorizationHoldEnabled func(repo string) bool @@ -184,6 +188,9 @@ type Options struct { // SweepTrigger is the trigger webhook deliveries notify; nil with OpenPRs // set builds one, reachable through Engine.Trigger. SweepTrigger *SweepTrigger + // 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. @@ -224,6 +231,10 @@ type Engine struct { requireCurrentBase func(repo string) bool mergeForwardTrustedHuman func(repo string) bool mergeTrainEnabledFn func() bool + // mergeTrainMaxRebaseAttemptsFn is the train's rebase loop guard. + mergeTrainMaxRebaseAttemptsFn func() int + // rebaseResolver receives the train's rebase-resolve requests. + rebaseResolver RebaseResolver // mergeTrain is the merge-train memory (automerge_merge_train.go). mergeTrain mergeTrainState @@ -326,6 +337,7 @@ func New(transport Transport, opts Options) *Engine { requireCurrentBase: opts.RequireCurrentBase, mergeForwardTrustedHuman: opts.MergeForwardTrustedHuman, mergeTrainEnabledFn: opts.MergeTrainEnabled, + mergeTrainMaxRebaseAttemptsFn: opts.MergeTrainMaxRebaseAttempts, selfAuthorizationHoldEnabled: opts.SelfAuthorizationHoldEnabled, selfAuthorizationHoldReleaseLimit: opts.SelfAuthorizationHoldReleaseLimit, repoAutoMergeEnabled: opts.RepoAutoMergeEnabled, @@ -351,6 +363,7 @@ func New(transport Transport, opts Options) *Engine { checkSummary: opts.CheckSummary, openPRs: opts.OpenPRs, sweepTrigger: opts.SweepTrigger, + rebaseResolver: opts.RebaseResolver, evaluatedHeads: make(map[string]string), requiredChecksFallbackWarned: make(map[string]bool), } @@ -1041,8 +1054,8 @@ 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) == "" + train := c.mergeTrainSlots(ctx, stageLaneSelf, repo, owner, repoName, prs, func(pr *gh.PullRequest) bool { + return c.mergeTrainCandidate(c.prefilterSelfAuthoredPR(pr)) }) repoSeen := 0 repoSkipped := 0 @@ -1062,6 +1075,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. @@ -1120,6 +1148,7 @@ func (c *Engine) SweepSelfAuthoredAutoMerges(ctx context.Context, opts AutoMerge result.Skipped++ repoSkipped++ repoSkipReasons[reason]++ + c.noteMergeTrainFrontSkip(train, pr, reason) stage(reason, false, nil) continue } @@ -1128,17 +1157,10 @@ func (c *Engine) SweepSelfAuthoredAutoMerges(ctx context.Context, opts AutoMerge result.Skipped++ repoSkipped++ repoSkipReasons[reason]++ + c.noteMergeTrainFrontSkip(train, pr, reason) 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) @@ -1178,6 +1200,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 +1213,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,7 +1277,7 @@ 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 { + train := c.mergeTrainSlots(ctx, stageLaneTrusted, displayRepo, owner, repoName, prs, func(pr *gh.PullRequest) bool { author := hgithub.SafeGetLogin(pr.GetUser()) if opts.SelfAuthoredLaneActive && c.isTrustedBotLaneAuthor(author) { return false @@ -1260,7 +1285,7 @@ func (c *Engine) SweepTrustedAuthorAutoMerges(ctx context.Context, opts AutoMerg if lane, _ := c.sweepLaneForAuthor(author); lane == stageLaneSelf { return false } - return c.prefilterTrustedAuthorPR(pr, policy) == "" + return c.mergeTrainCandidate(c.prefilterTrustedAuthorPR(pr, policy)) }) for _, pr := range prs { if len(result.Merged) >= maxMerges { @@ -1274,6 +1299,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 @@ -1298,14 +1334,10 @@ func (c *Engine) SweepTrustedAuthorAutoMerges(ctx context.Context, opts AutoMerg if reason := c.prefilterTrustedAuthorPR(pr, policy); reason != "" { c.info("automerge skip", "repo", displayRepo, "pr", number, "reason", reason, "tier", "trusted-author") result.Skipped++ + c.noteMergeTrainFrontSkip(train, pr, reason) 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 +1360,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 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) + } + } +}