From 3a85cb84967e0f0699d429a228581527d9ef01f1 Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Wed, 28 Oct 2026 18:35:31 -0400 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20feat(automerge):=20pick=20one?= =?UTF-8?q?=20merge-train=20front=20per=20group=20of=20file-overlapping=20?= =?UTF-8?q?approved=20PRs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 the sweep then logged them as conflicting every cycle with nothing ordering them. The self-authored and trusted-author sweeps now group lane-eligible PRs per repo and base branch by transitively overlapping changed files (GET /pulls/{n}/files, cached per head SHA) and let one front per group proceed to the merge path per pass: a head not last seen conflicting, red or held first, then the oldest. Siblings skip as merge-train-queued and are re-evaluated against the new base on later passes. A group whose best member is itself blocked imposes no ordering, and a PR whose files cannot be read stays out of every train, so the train never holds back a merge that would have happened before. One INFO line per slot change. auto_merge.merge_train.enabled defaults on; an explicit false restores the previous parallel behaviour. Sibling update/rebase follow-through and the tracker queued-behind state follow in #15143 and #15144. Part of #15117 Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Co-authored-by: clubanderson <407614+clubanderson@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- changelog.d/added-merge-train-front.md | 1 + src/cmd/hive/main.go | 2 + src/docs/operator-reference.md | 1 + src/pkg/config/automerge_merge_train_test.go | 25 ++ src/pkg/config/config.go | 18 + .../dashboard/settings_parity_allowlist.txt | 1 + .../github/automerge/automerge_merge_train.go | 309 ++++++++++++++++++ .../automerge/automerge_merge_train_test.go | 253 ++++++++++++++ src/pkg/github/automerge/automerge_sweep.go | 41 ++- .../github/automerge/automerge_sweep_test.go | 36 +- 10 files changed, 684 insertions(+), 3 deletions(-) create mode 100644 changelog.d/added-merge-train-front.md create mode 100644 src/pkg/config/automerge_merge_train_test.go create mode 100644 src/pkg/github/automerge/automerge_merge_train.go create mode 100644 src/pkg/github/automerge/automerge_merge_train_test.go diff --git a/changelog.d/added-merge-train-front.md b/changelog.d/added-merge-train-front.md new file mode 100644 index 0000000000..26736b6c10 --- /dev/null +++ b/changelog.d/added-merge-train-front.md @@ -0,0 +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). diff --git a/src/cmd/hive/main.go b/src/cmd/hive/main.go index f6babe84f5..e6301d9990 100644 --- a/src/cmd/hive/main.go +++ b/src/cmd/hive/main.go @@ -2547,6 +2547,7 @@ func (b *boot) bootAgentsWith(deps bootAgentsDeps) { // auto_merge.trusted_bot_authors takes effect without a restart. autoMergeOpts.TrustedBotAuthors = func() map[string]bool { return b.cfg.AutoMerge.TrustedBotAuthorSet() } autoMergeOpts.BotTakeoverEnabled = func() bool { return b.cfg.AutoMerge.BotTakeover.IsEnabled() } + autoMergeOpts.MergeTrainEnabled = func() bool { return b.cfg.AutoMerge.MergeTrain.IsEnabled() } // Intent tier gate (#6258): the human lane only queues PRs that // survive writeMergeEligible's intent check, but this sweep lists // the App's PRs on its own, so it carries the same policy (same @@ -9372,6 +9373,7 @@ func runAutoMergeSweepIfDue(ctx context.Context, ghClient *github.Client, cfg *c opts.HumanMergePaths = func(repo string) []string { return humanMergePathsFor(cfg, repo) } opts.RequireCurrentBase = func(repo string) bool { return cfg.AutoMerge.RequireCurrentBaseFor(repo) } opts.MergeForwardTrustedHuman = func(repo string) bool { return cfg.AutoMerge.MergeForwardTrustedHumanFor(repo) } + opts.MergeTrainEnabled = func() bool { return cfg.AutoMerge.MergeTrain.IsEnabled() } opts.TrustedBotAuthors = func() map[string]bool { return cfg.AutoMerge.TrustedBotAuthorSet() } } audit := func(event automerge.AutoMergeSweepEvent) { diff --git a/src/docs/operator-reference.md b/src/docs/operator-reference.md index 8c7dd318c5..d9eef3ef26 100644 --- a/src/docs/operator-reference.md +++ b/src/docs/operator-reference.md @@ -434,6 +434,7 @@ 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.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 new file mode 100644 index 0000000000..bb6c1be167 --- /dev/null +++ b/src/pkg/config/automerge_merge_train_test.go @@ -0,0 +1,25 @@ +package config + +import "testing" + +func TestMergeTrainAutoMergeConfigDefaultsOn(t *testing.T) { + off, on := false, true + cases := []struct { + name string + cfg MergeTrainAutoMergeConfig + want bool + }{ + {"unset", MergeTrainAutoMergeConfig{}, true}, + {"explicit on", MergeTrainAutoMergeConfig{Enabled: &on}, true}, + {"explicit off", MergeTrainAutoMergeConfig{Enabled: &off}, false}, + } + for _, tc := range cases { + if got := tc.cfg.IsEnabled(); got != tc.want { + t.Errorf("%s: IsEnabled() = %v, want %v", tc.name, got, tc.want) + } + } + var a AutoMergeConfig + if !a.MergeTrain.IsEnabled() { + t.Fatal("zero AutoMergeConfig disables the merge train; want default on") + } +} diff --git a/src/pkg/config/config.go b/src/pkg/config/config.go index f527ee6a70..0990c9034a 100644 --- a/src/pkg/config/config.go +++ b/src/pkg/config/config.go @@ -7918,6 +7918,24 @@ type AutoMergeConfig struct { // open-time attribution. nil/unset means ON; `credit_issue_authors: // false` sends the merge with GitHub's default message as before. CreditIssueAuthors *bool `yaml:"credit_issue_authors,omitempty" json:"credit_issue_authors,omitempty"` + // MergeTrain orders approved PRs whose changed files overlap so they + // merge one at a time instead of dirtying each other + // (hivecommons/hive#15117). + MergeTrain MergeTrainAutoMergeConfig `yaml:"merge_train,omitempty" json:"merge_train,omitempty"` +} + +// MergeTrainAutoMergeConfig is auto_merge.merge_train.*. +type MergeTrainAutoMergeConfig struct { + // Enabled groups each sweep's candidate PRs per repo and base branch by + // overlapping changed files and lets only one front per group merge per + // pass: the mergeable one first, then the oldest. nil/unset means ON; + // an explicit `enabled: false` merges every green PR as before. + Enabled *bool `yaml:"enabled,omitempty" json:"enabled,omitempty"` +} + +// IsEnabled reports the auto_merge.merge_train.enabled switch; unset is on. +func (m MergeTrainAutoMergeConfig) IsEnabled() bool { + return m.Enabled == nil || *m.Enabled } // CreditIssueAuthorsEnabled reports the auto_merge.credit_issue_authors diff --git a/src/pkg/dashboard/settings_parity_allowlist.txt b/src/pkg/dashboard/settings_parity_allowlist.txt index 7748d8f786..c213dfca6b 100644 --- a/src/pkg/dashboard/settings_parity_allowlist.txt +++ b/src/pkg/dashboard/settings_parity_allowlist.txt @@ -153,6 +153,7 @@ agents.session_recycle.max_age # TODO #11854 agents.session_recycle.max_rss_mb # TODO #11854 agents.session_recycle.min_idle # TODO #11854 auto_merge.credit_issue_authors # merge-time Co-authored-by credit (#14581); yaml-only +auto_merge.merge_train.enabled # merge-train front selection (#15141); yaml-only auto_merge.allow_unprotected_base[] # TODO #11854 auto_merge.human_merge_paths. # TODO #11854 auto_merge.merge_forward_trusted_human[] # TODO #11854 diff --git a/src/pkg/github/automerge/automerge_merge_train.go b/src/pkg/github/automerge/automerge_merge_train.go new file mode 100644 index 0000000000..1e8df9cead --- /dev/null +++ b/src/pkg/github/automerge/automerge_merge_train.go @@ -0,0 +1,309 @@ +package automerge + +import ( + "context" + "sort" + "strconv" + "strings" + "sync" + "time" + + gh "github.com/google/go-github/v72/github" + hgithub "github.com/hivecommons/hive/pkg/github" +) + +// autoMergeReasonMergeTrainQueued is the skip reason of an approved PR that +// shares changed files with another candidate on the same base and is not +// that group's merge-train front (hivecommons/hive#15117). Merging every +// overlapping PR in parallel dirties all but the first; the train merges one +// front per group and leaves the rest for the next pass against the new base. +const autoMergeReasonMergeTrainQueued = "merge-train-queued" + +// mergeTrainMember is one candidate PR as the train planner sees it. +type mergeTrainMember struct { + Number int + Base string + Created time.Time + // Blocked marks a head the sweep last saw unable to merge on its own + // (conflicting, red, or a terminal hold): it never fronts a train while a + // mergeable member exists, so it cannot stall its siblings. + Blocked bool + Files []string +} + +// mergeTrainSlot is a PR's place in its overlap group. Position 1 is the +// front; OverlapFiles are the member's files that another member also changes. +type mergeTrainSlot struct { + Front int + Position int + Size int + OverlapFiles []string +} + +// planMergeTrains groups members per base branch by transitively overlapping +// changed files and orders each group of two or more: mergeable before +// blocked, then oldest, then lowest number. A group whose best member is +// itself blocked gets no slots, so a train never holds back siblings behind a +// front that cannot merge. +func planMergeTrains(members []mergeTrainMember) map[int]mergeTrainSlot { + parent := make([]int, len(members)) + for i := range parent { + parent[i] = i + } + var find func(int) int + find = func(i int) int { + if parent[i] != i { + parent[i] = find(parent[i]) + } + return parent[i] + } + owner := make(map[string]int) + for i, m := range members { + for _, f := range m.Files { + key := m.Base + "\x00" + f + if j, ok := owner[key]; ok { + parent[find(i)] = find(j) + continue + } + owner[key] = i + } + } + groups := make(map[int][]int) + for i := range members { + root := find(i) + groups[root] = append(groups[root], i) + } + slots := make(map[int]mergeTrainSlot) + for _, group := range groups { + if len(group) < 2 { + continue + } + sort.Slice(group, func(a, b int) bool { + ma, mb := members[group[a]], members[group[b]] + if ma.Blocked != mb.Blocked { + return !ma.Blocked + } + if !ma.Created.IsZero() && !mb.Created.IsZero() && !ma.Created.Equal(mb.Created) { + return ma.Created.Before(mb.Created) + } + return ma.Number < mb.Number + }) + front := members[group[0]] + if front.Blocked { + continue + } + touches := make(map[string]int) + for _, i := range group { + for _, f := range uniqueStrings(members[i].Files) { + touches[f]++ + } + } + for pos, i := range group { + var overlap []string + for _, f := range uniqueStrings(members[i].Files) { + if touches[f] > 1 { + overlap = append(overlap, f) + } + } + sort.Strings(overlap) + slots[members[i].Number] = mergeTrainSlot{Front: front.Number, Position: pos + 1, Size: len(group), OverlapFiles: overlap} + } + } + return slots +} + +func uniqueStrings(in []string) []string { + seen := make(map[string]bool, len(in)) + out := make([]string, 0, len(in)) + for _, s := range in { + if s == "" || seen[s] { + continue + } + seen[s] = true + out = append(out, s) + } + return out +} + +// mergeTrainState is the engine's per-process train memory: changed files per +// PR cached by head SHA, heads last seen blocked, and the slot last logged per +// PR so a transition logs once rather than every pass. +type mergeTrainState struct { + mu sync.Mutex + files map[string]mergeTrainFiles + blocked map[string]string + logged map[string]string +} + +type mergeTrainFiles struct { + sha string + files []string +} + +func (c *Engine) mergeTrainEnabled() bool { + return c.mergeTrainEnabledFn == nil || c.mergeTrainEnabledFn() +} + +// mergeTrainBlockingReason reports whether a lane's skip reason means the head +// cannot merge without someone changing it, so it must not front a train. +func mergeTrainBlockingReason(reason string) bool { + switch reason { + case "conflicting", "not-mergeable", autoMergeReasonForkNotEditable, autoMergeReasonClosedSuperseded: + return true + } + return isTerminalRequiredCheckHold(reason) || isPermanentMergeRefusal(reason) || classifyCIReason(reason) == ciReasonRed +} + +// noteMergeTrainOutcome records whether the lane's verdict for a head blocks +// it from fronting a train. A merged or non-blocking verdict clears the mark. +func (c *Engine) noteMergeTrainOutcome(owner, repo string, number int, sha, reason string) { + if c == nil || number <= 0 { + return + } + key := c.evaluatedHeadKey(owner, repo, number) + c.mergeTrain.mu.Lock() + defer c.mergeTrain.mu.Unlock() + if !mergeTrainBlockingReason(reason) || strings.TrimSpace(sha) == "" { + delete(c.mergeTrain.blocked, key) + return + } + if c.mergeTrain.blocked == nil { + c.mergeTrain.blocked = make(map[string]string) + } + c.mergeTrain.blocked[key] = 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) { + key := c.evaluatedHeadKey(owner, repo, pr.GetNumber()) + sha := pr.GetHead().GetSHA() + c.mergeTrain.mu.Lock() + cached, ok := c.mergeTrain.files[key] + c.mergeTrain.mu.Unlock() + if ok && cached.sha == sha { + return cached.files, nil + } + changed, err := ListChangedFiles(hgithub.WithRESTCaller(ctx, "hive:automerge_merge_train"), c.gh, owner, repo, pr) + if err != nil { + return nil, err + } + files := make([]string, 0, len(changed)) + for _, f := range changed { + files = append(files, f.Filename) + } + c.mergeTrain.mu.Lock() + if c.mergeTrain.files == nil { + c.mergeTrain.files = make(map[string]mergeTrainFiles) + } + c.mergeTrain.files[key] = mergeTrainFiles{sha: sha, files: files} + c.mergeTrain.mu.Unlock() + return files, nil +} + +// mergeTrainSlots plans the merge trains for one repo's listed PRs. eligible +// is the lane's cheap list-payload prefilter; only PRs it admits, on a base +// with at least two of them, cost a files lookup. A PR whose files cannot be +// read stays out of every train, so a lookup failure never holds a merge back. +func (c *Engine) mergeTrainSlots(ctx context.Context, displayRepo, owner, repo string, prs []*gh.PullRequest, eligible func(*gh.PullRequest) bool) map[int]mergeTrainSlot { + if c == nil || c.gh == nil || !c.mergeTrainEnabled() { + return nil + } + perBase := make(map[string][]*gh.PullRequest) + for _, pr := range prs { + if pr == nil || pr.GetNumber() <= 0 || !eligible(pr) { + continue + } + base := pr.GetBase().GetRef() + perBase[base] = append(perBase[base], pr) + } + var members []mergeTrainMember + live := make(map[string]bool) + listed := make(map[string]bool) + for _, group := range perBase { + for _, pr := range group { + listed[c.evaluatedHeadKey(owner, repo, pr.GetNumber())] = true + } + } + for base, group := range perBase { + if len(group) < 2 { + continue + } + for _, pr := range group { + files, err := c.mergeTrainChangedFiles(ctx, owner, repo, pr) + if err != nil { + c.warn("merge train could not list PR files; PR stays out of the train", "repo", displayRepo, "pr", pr.GetNumber(), "error", err) + continue + } + key := c.evaluatedHeadKey(owner, repo, pr.GetNumber()) + live[key] = true + c.mergeTrain.mu.Lock() + blocked := c.mergeTrain.blocked[key] == pr.GetHead().GetSHA() + c.mergeTrain.mu.Unlock() + members = append(members, mergeTrainMember{ + Number: pr.GetNumber(), + Base: base, + Created: pr.GetCreatedAt().Time, + Blocked: blocked, + Files: files, + }) + } + } + slots := planMergeTrains(members) + c.logMergeTrainTransitions(displayRepo, owner, repo, slots, live, listed) + return slots +} + +// logMergeTrainTransitions writes one INFO line when a PR joins a train or its +// slot changes, and forgets cached files and blocked marks of this repo's PRs +// that are no longer listed (live: files were read; listed: lane-eligible). +func (c *Engine) logMergeTrainTransitions(displayRepo, owner, repo string, slots map[int]mergeTrainSlot, live, listed map[string]bool) { + prefix := strings.ToLower(strings.TrimSpace(owner)) + "/" + strings.ToLower(strings.TrimSpace(repo)) + "#" + numbers := make([]int, 0, len(slots)) + for n := range slots { + numbers = append(numbers, n) + } + sort.Ints(numbers) + c.mergeTrain.mu.Lock() + if c.mergeTrain.logged == nil { + c.mergeTrain.logged = make(map[string]string) + } + var lines [][]any + current := make(map[string]bool, len(slots)) + for _, n := range numbers { + slot := slots[n] + key := c.evaluatedHeadKey(owner, repo, n) + current[key] = true + summary := strconv.Itoa(slot.Front) + "/" + strconv.Itoa(slot.Position) + "/" + strconv.Itoa(slot.Size) + if c.mergeTrain.logged[key] == summary { + 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, ",")}) + } + for key := range c.mergeTrain.logged { + if strings.HasPrefix(key, prefix) && !current[key] { + delete(c.mergeTrain.logged, key) + } + } + for key := range c.mergeTrain.files { + if strings.HasPrefix(key, prefix) && !live[key] { + delete(c.mergeTrain.files, key) + } + } + for key := range c.mergeTrain.blocked { + if strings.HasPrefix(key, prefix) && !listed[key] { + delete(c.mergeTrain.blocked, key) + } + } + c.mergeTrain.mu.Unlock() + for _, args := range lines { + c.info("automerge merge train", args...) + } +} + +// 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 +} diff --git a/src/pkg/github/automerge/automerge_merge_train_test.go b/src/pkg/github/automerge/automerge_merge_train_test.go new file mode 100644 index 0000000000..0f1b5b80f8 --- /dev/null +++ b/src/pkg/github/automerge/automerge_merge_train_test.go @@ -0,0 +1,253 @@ +package automerge + +import ( + "context" + "reflect" + "sort" + "testing" + "time" +) + +func TestPlanMergeTrains(t *testing.T) { + day := func(d int) time.Time { return time.Date(2026, 10, d, 0, 0, 0, 0, time.UTC) } + cases := []struct { + name string + members []mergeTrainMember + want map[int]mergeTrainSlot + }{ + { + name: "disjoint PRs form no train", + members: []mergeTrainMember{ + {Number: 1, Base: "v5", Files: []string{"a.go"}}, + {Number: 2, Base: "v5", Files: []string{"b.go"}}, + }, + want: map[int]mergeTrainSlot{}, + }, + { + name: "overlap picks the lowest number when ages tie", + members: []mergeTrainMember{ + {Number: 2, Base: "v5", Files: []string{"stage_tracker.go"}}, + {Number: 1, Base: "v5", Files: []string{"stage_tracker.go", "a.go"}}, + }, + want: map[int]mergeTrainSlot{ + 1: {Front: 1, Position: 1, Size: 2, OverlapFiles: []string{"stage_tracker.go"}}, + 2: {Front: 1, Position: 2, Size: 2, OverlapFiles: []string{"stage_tracker.go"}}, + }, + }, + { + name: "oldest fronts over a lower number", + members: []mergeTrainMember{ + {Number: 1, Base: "v5", Created: day(3), Files: []string{"x.go"}}, + {Number: 2, Base: "v5", Created: day(1), Files: []string{"x.go"}}, + }, + want: map[int]mergeTrainSlot{ + 2: {Front: 2, Position: 1, Size: 2, OverlapFiles: []string{"x.go"}}, + 1: {Front: 2, Position: 2, Size: 2, OverlapFiles: []string{"x.go"}}, + }, + }, + { + name: "mergeable fronts over an older blocked head", + members: []mergeTrainMember{ + {Number: 1, Base: "v5", Created: day(1), Blocked: true, Files: []string{"x.go"}}, + {Number: 2, Base: "v5", Created: day(2), Files: []string{"x.go"}}, + }, + want: map[int]mergeTrainSlot{ + 2: {Front: 2, Position: 1, Size: 2, OverlapFiles: []string{"x.go"}}, + 1: {Front: 2, Position: 2, Size: 2, OverlapFiles: []string{"x.go"}}, + }, + }, + { + name: "every member blocked holds nobody back", + members: []mergeTrainMember{ + {Number: 1, Base: "v5", Blocked: true, Files: []string{"x.go"}}, + {Number: 2, Base: "v5", Blocked: true, Files: []string{"x.go"}}, + }, + want: map[int]mergeTrainSlot{}, + }, + { + name: "transitive overlap is one train", + members: []mergeTrainMember{ + {Number: 1, Base: "v5", Files: []string{"a.go", "b.go"}}, + {Number: 2, Base: "v5", Files: []string{"b.go", "c.go"}}, + {Number: 3, Base: "v5", Files: []string{"c.go", "d.go"}}, + {Number: 4, Base: "v5", Files: []string{"e.go"}}, + }, + want: map[int]mergeTrainSlot{ + 1: {Front: 1, Position: 1, Size: 3, OverlapFiles: []string{"b.go"}}, + 2: {Front: 1, Position: 2, Size: 3, OverlapFiles: []string{"b.go", "c.go"}}, + 3: {Front: 1, Position: 3, Size: 3, OverlapFiles: []string{"c.go"}}, + }, + }, + { + name: "different bases never share a train", + members: []mergeTrainMember{ + {Number: 1, Base: "v5", Files: []string{"x.go"}}, + {Number: 2, Base: "main", Files: []string{"x.go"}}, + }, + want: map[int]mergeTrainSlot{}, + }, + { + name: "two separate trains on one base", + members: []mergeTrainMember{ + {Number: 1, Base: "v5", Files: []string{"x.go"}}, + {Number: 2, Base: "v5", Files: []string{"y.go"}}, + {Number: 3, Base: "v5", Files: []string{"x.go"}}, + {Number: 4, Base: "v5", Files: []string{"y.go"}}, + }, + want: map[int]mergeTrainSlot{ + 1: {Front: 1, Position: 1, Size: 2, OverlapFiles: []string{"x.go"}}, + 3: {Front: 1, Position: 2, Size: 2, OverlapFiles: []string{"x.go"}}, + 2: {Front: 2, Position: 1, Size: 2, OverlapFiles: []string{"y.go"}}, + 4: {Front: 2, Position: 2, Size: 2, OverlapFiles: []string{"y.go"}}, + }, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + got := planMergeTrains(tc.members) + if !reflect.DeepEqual(got, tc.want) { + t.Fatalf("planMergeTrains = %#v, want %#v", got, tc.want) + } + }) + } +} + +func TestMergeTrainBlockingReason(t *testing.T) { + cases := []struct { + reason string + want bool + }{ + {"", false}, + {"conflicting", true}, + {"not-mergeable", true}, + {autoMergeReasonForkNotEditable, true}, + {"behind", false}, + {"updated-branch", false}, + {autoMergeReasonMergeTrainQueued, false}, + } + for _, tc := range cases { + if got := mergeTrainBlockingReason(tc.reason); got != tc.want { + t.Errorf("mergeTrainBlockingReason(%q) = %v, want %v", tc.reason, got, tc.want) + } + } +} + +func greenTrainPR(number int, files ...string) selfAuthoredPR { + return selfAuthoredPR{ + number: number, author: testHiveAppBotLogin, mergeableState: "clean", + statusState: "success", checkStatus: "completed", checkConclusion: "success", + files: files, + } +} + +func TestSweepSelfAuthoredMergeTrain(t *testing.T) { + cases := []struct { + name string + disabled bool + prs []selfAuthoredPR + wantMerged []int + wantQueued int + }{ + { + name: "only the front of an overlapping group merges", + prs: []selfAuthoredPR{ + greenTrainPR(11, "src/pkg/dashboard/stage_tracker.go", "static/index.html"), + greenTrainPR(12, "src/pkg/dashboard/stage_tracker.go"), + greenTrainPR(13, "README.md"), + }, + wantMerged: []int{11, 13}, + wantQueued: 1, + }, + { + name: "the oldest PR fronts the train", + prs: func() []selfAuthoredPR { + newer, older := greenTrainPR(11, "static/index.html"), greenTrainPR(12, "static/index.html") + newer.createdAt, older.createdAt = "2026-10-28T10:00:00Z", "2026-10-27T10:00:00Z" + return []selfAuthoredPR{newer, older} + }(), + wantMerged: []int{12}, + wantQueued: 1, + }, + { + name: "disabled merges every green PR as before", + disabled: true, + prs: []selfAuthoredPR{ + greenTrainPR(11, "src/pkg/dashboard/stage_tracker.go"), + greenTrainPR(12, "src/pkg/dashboard/stage_tracker.go"), + }, + wantMerged: []int{11, 12}, + }, + { + name: "disjoint PRs all merge", + prs: []selfAuthoredPR{ + greenTrainPR(11, "a.go"), + greenTrainPR(12, "b.go"), + }, + wantMerged: []int{11, 12}, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + var merged []int + api := newSelfAuthoredAutoMergeAPI(t, tc.prs, &merged) + defer api.Close() + c := newAutoMergeSweepClient(api.URL) + if tc.disabled { + c.mergeTrainEnabledFn = func() bool { return false } + } + result, err := c.SweepSelfAuthoredAutoMerges(context.Background(), AutoMergeSweepOptions{}) + if err != nil { + t.Fatalf("SweepSelfAuthoredAutoMerges returned error: %v", err) + } + sort.Ints(merged) + if !reflect.DeepEqual(merged, tc.wantMerged) { + t.Fatalf("merged = %v, want %v", merged, tc.wantMerged) + } + if queued := result.Skipped; queued != tc.wantQueued { + t.Fatalf("skipped = %d, want %d train-queued sibling(s)", queued, tc.wantQueued) + } + }) + } +} + +// A conflicting front must not stall its train: once the sweep has seen the +// head conflict, the next pass fronts the mergeable sibling instead. +func TestSweepSelfAuthoredMergeTrainSkipsConflictingFront(t *testing.T) { + var merged []int + dirty := greenTrainPR(11, "static/index.html") + dirty.mergeableState = "dirty" + api := newSelfAuthoredAutoMergeAPI(t, []selfAuthoredPR{dirty, greenTrainPR(12, "static/index.html")}, &merged) + defer api.Close() + c := newAutoMergeSweepClient(api.URL) + + if _, err := c.SweepSelfAuthoredAutoMerges(context.Background(), AutoMergeSweepOptions{}); err != nil { + t.Fatalf("first pass: %v", err) + } + if len(merged) != 0 { + t.Fatalf("first pass merged %v, want the sibling queued behind the untried front", merged) + } + if _, err := c.SweepSelfAuthoredAutoMerges(context.Background(), AutoMergeSweepOptions{}); err != nil { + t.Fatalf("second pass: %v", err) + } + if !reflect.DeepEqual(merged, []int{12}) { + t.Fatalf("second pass merged %v, want the mergeable sibling #12 as the new front", merged) + } +} + +func TestSweepSelfAuthoredMergeTrainCachesFilesByHead(t *testing.T) { + var merged []int + fetches11, fetches12 := 0, 0 + a, b := greenTrainPR(11, "x.go"), greenTrainPR(12, "x.go") + a.filesFetches, b.filesFetches = &fetches11, &fetches12 + api := newSelfAuthoredAutoMergeAPI(t, []selfAuthoredPR{a, b}, &merged) + defer api.Close() + c := newAutoMergeSweepClient(api.URL) + for i := 0; i < 3; i++ { + if _, err := c.SweepSelfAuthoredAutoMerges(context.Background(), AutoMergeSweepOptions{}); err != nil { + t.Fatalf("pass %d: %v", i, err) + } + } + if fetches11 != 1 || fetches12 != 1 { + t.Fatalf("files fetches = #11:%d #12:%d, want one per unchanged head", fetches11, fetches12) + } +} diff --git a/src/pkg/github/automerge/automerge_sweep.go b/src/pkg/github/automerge/automerge_sweep.go index 5010d46851..8f3e7b1592 100644 --- a/src/pkg/github/automerge/automerge_sweep.go +++ b/src/pkg/github/automerge/automerge_sweep.go @@ -94,6 +94,9 @@ type Options struct { // auto_merge.merge_forward_trusted_human membership for owner/repo // (hivecommons/hive#13636); nil means off. MergeForwardTrustedHuman func(repo string) bool + // MergeTrainEnabled returns the live auto_merge.merge_train.enabled + // switch (hivecommons/hive#15117). nil means on. + MergeTrainEnabled func() bool // SelfAuthorizationHoldEnabled returns the live per-repo #5117 hold switch. // nil preserves the default-on behavior. SelfAuthorizationHoldEnabled func(repo string) bool @@ -220,6 +223,9 @@ type Engine struct { requireCurrentBase func(repo string) bool mergeForwardTrustedHuman func(repo string) bool + mergeTrainEnabledFn func() bool + // mergeTrain is the merge-train memory (automerge_merge_train.go). + mergeTrain mergeTrainState selfAuthorizationHoldEnabled func(repo string) bool selfAuthorizationHoldReleaseLimit int @@ -319,6 +325,7 @@ func New(transport Transport, opts Options) *Engine { humanMergePaths: opts.HumanMergePaths, requireCurrentBase: opts.RequireCurrentBase, mergeForwardTrustedHuman: opts.MergeForwardTrustedHuman, + mergeTrainEnabledFn: opts.MergeTrainEnabled, selfAuthorizationHoldEnabled: opts.SelfAuthorizationHoldEnabled, selfAuthorizationHoldReleaseLimit: opts.SelfAuthorizationHoldReleaseLimit, repoAutoMergeEnabled: opts.RepoAutoMergeEnabled, @@ -1034,6 +1041,9 @@ func (c *Engine) SweepSelfAuthoredAutoMerges(ctx context.Context, opts AutoMerge c.info("self-authored automerge sweep skipped repo", "repo", repo, "reason", skipReason) continue } + trains := c.mergeTrainSlots(ctx, repo, owner, repoName, prs, func(pr *gh.PullRequest) bool { + return c.prefilterSelfAuthoredPR(pr) == "" + }) repoSeen := 0 repoSkipped := 0 repoCandidates := 0 @@ -1121,12 +1131,23 @@ func (c *Engine) SweepSelfAuthoredAutoMerges(ctx context.Context, opts AutoMerge stage(reason, false, nil) continue } + if _, queued := mergeTrainQueued(trains, number); queued { + // The stage is left as last recorded: the train does not + // re-evaluate a waiting sibling, so it has no new verdict. + result.Skipped++ + repoSkipped++ + repoSkipReasons[autoMergeReasonMergeTrainQueued]++ + continue + } result.Candidates++ repoCandidates++ event, reason, err := c.trySweepSelfAuthoredPR(ctx, repo, owner, repoName, number, branchUpdateAttempts < selfAuthoredSweepMaxBranchUpdates) if event.BranchUpdated || reason == "updated-branch" || reason == "update-branch" { branchUpdateAttempts++ } + if err == nil { + c.noteMergeTrainOutcome(owner, repoName, number, pr.GetHead().GetSHA(), reason) + } if err != nil { c.warnSelfAuthoredSkipOnce(repo, number, reason, err) result.Skipped++ @@ -1167,7 +1188,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} + 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} if sentinelLabel := c.sentinelLabelName(); sentinelLabel != "" { skipReasonOrder = append([]string{sentinelLabel}, skipReasonOrder...) } @@ -1231,6 +1252,16 @@ func (c *Engine) SweepTrustedAuthorAutoMerges(ctx context.Context, opts AutoMerg c.info("trusted-author automerge sweep skipped repo", "repo", displayRepo, "reason", skipReason) continue } + trains := c.mergeTrainSlots(ctx, displayRepo, owner, repoName, prs, func(pr *gh.PullRequest) bool { + author := hgithub.SafeGetLogin(pr.GetUser()) + if opts.SelfAuthoredLaneActive && c.isTrustedBotLaneAuthor(author) { + return false + } + if lane, _ := c.sweepLaneForAuthor(author); lane == stageLaneSelf { + return false + } + return c.prefilterTrustedAuthorPR(pr, policy) == "" + }) for _, pr := range prs { if len(result.Merged) >= maxMerges { break @@ -1270,8 +1301,16 @@ func (c *Engine) SweepTrustedAuthorAutoMerges(ctx context.Context, opts AutoMerg stage(reason, false, nil) continue } + if _, queued := mergeTrainQueued(trains, number); queued { + // See the self-authored lane: a waiting sibling keeps its stage. + result.Skipped++ + continue + } result.Candidates++ event, reason, err := c.trySweepTrustedAuthorPR(ctx, displayRepo, owner, repoName, number, policy) + if err == nil { + c.noteMergeTrainOutcome(owner, repoName, number, pr.GetHead().GetSHA(), reason) + } if err != nil { c.warn("trusted-author automerge sweep skipped PR", "repo", displayRepo, "pr", number, "reason", reason, "error", err) result.Skipped++ diff --git a/src/pkg/github/automerge/automerge_sweep_test.go b/src/pkg/github/automerge/automerge_sweep_test.go index cddad44735..39bf1d71f0 100644 --- a/src/pkg/github/automerge/automerge_sweep_test.go +++ b/src/pkg/github/automerge/automerge_sweep_test.go @@ -1760,6 +1760,25 @@ type selfAuthoredPR struct { // okToTestWorkflowStatus is the status GET /contents/.github/workflows/hive-ok-to-test.yml // answers with (0 = 404). okToTestWorkflowStatus int + // files is GET /pulls/{n}/files; unset is one file no other PR touches. + files []string + // createdAt, when set, is the PR's created_at (the merge train's age order). + createdAt string + // filesFetches, when set, counts GET /pulls/{n}/files calls. + filesFetches *int +} + +// prFilesResponse renders a fake GET /pulls/{n}/files payload; no files means +// one path unique to the PR, so the merge train never groups it. +func prFilesResponse(number int, files []string) []map[string]any { + if len(files) == 0 { + files = []string{"pr" + strconv.Itoa(number) + ".go"} + } + out := make([]map[string]any, 0, len(files)) + for _, f := range files { + out = append(out, map[string]any{"filename": f, "status": "modified"}) + } + return out } func newSelfAuthoredAutoMergeAPI(t *testing.T, prs []selfAuthoredPR, merged *[]int, getCounts ...*map[int]int) *httptest.Server { @@ -1777,20 +1796,31 @@ func newSelfAuthoredAutoMergeAPI(t *testing.T, prs []selfAuthoredPR, merged *[]i case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/pulls": var out []map[string]any for _, pr := range prs { - out = append(out, map[string]any{ + item := map[string]any{ "number": pr.number, "state": "open", "draft": pr.draft, "user": map[string]string{"login": pr.author}, "head": map[string]string{"sha": "sha" + strconv.Itoa(pr.number)}, "labels": issueLabels("", pr.extraLabels), - }) + } + if pr.createdAt != "" { + item["created_at"] = pr.createdAt + } + out = append(out, item) } json.NewEncoder(w).Encode(out) case r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/repos/acme/widget/pulls/") && strings.HasSuffix(r.URL.Path, "/reviews"): // The self lane reads reviews on every pass for a trusted // human's changes_requested (hivecommons/hive#13041). json.NewEncoder(w).Encode([]map[string]any{}) + case r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/repos/acme/widget/pulls/") && strings.HasSuffix(r.URL.Path, "/files"): + number := pathNumber(t, r.URL.Path, "/repos/acme/widget/pulls/", "/files") + pr := byNumber[number] + if pr.filesFetches != nil { + (*pr.filesFetches)++ + } + json.NewEncoder(w).Encode(prFilesResponse(number, pr.files)) case r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/repos/acme/widget/pulls/"): number := pathNumber(t, r.URL.Path, "/repos/acme/widget/pulls/", "") pr := byNumber[number] @@ -2914,6 +2944,8 @@ func newAutoMergeSweepAPI(t *testing.T, expectedLabel string, prs []sweepPR, mer "commit_id": reviewCommitID, "user": map[string]string{"login": reviewAuthor}, }}) + case r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/repos/acme/widget/pulls/") && strings.HasSuffix(r.URL.Path, "/files"): + json.NewEncoder(w).Encode(prFilesResponse(pathNumber(t, r.URL.Path, "/repos/acme/widget/pulls/", "/files"), nil)) case r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/repos/acme/widget/pulls/"): number := pathNumber(t, r.URL.Path, "/repos/acme/widget/pulls/", "") pr := byNumber[number] From 561120aa7932b23747e0d6c6b476499205772e84 Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Wed, 28 Oct 2026 19:41:52 -0400 Subject: [PATCH 2/2] test(config): add auto_merge.merge_train to the agent-runtime golden The merge_train sub-struct marshals as an empty object like its auto_merge siblings; the golden must carry it or TestAgentRuntimeConfigGolden reports the key as unexpected. Part of #15117 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- src/pkg/config/testdata/agent_runtime_golden.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pkg/config/testdata/agent_runtime_golden.json b/src/pkg/config/testdata/agent_runtime_golden.json index e04a7a1ec2..4007dc2c05 100644 --- a/src/pkg/config/testdata/agent_runtime_golden.json +++ b/src/pkg/config/testdata/agent_runtime_golden.json @@ -710,7 +710,8 @@ "bot_takeover": {}, "trusted_authors": {}, "contributor_lane": {}, - "contributor_tier": {} + "contributor_tier": {}, + "merge_train": {} }, "decisions": { "delegate": {}