From 41fc378758b9e2de90875df26e849d3906c9fa13 Mon Sep 17 00:00:00 2001 From: thatguywob Date: Thu, 29 Oct 2026 01:28:52 +0000 Subject: [PATCH 1/8] =?UTF-8?q?=E2=9C=A8=20feature:=20enqueue=20eligible?= =?UTF-8?q?=20PRs=20into=20GitHub=20native=20merge=20queues?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use the App installation transport to detect and enqueue native merge queues after the existing automerge gates, pinning the validated head. Reconcile Hive-owned queue entries when gates break and report queue position/state without recording enqueue as a completed merge. Co-authored-by: jeefy <1394552+jeefy@users.noreply.github.com> Hive-Run: hivecommons/hive#15198 Hive-Plan: native-merge-queue Hive-Spec: issue-15198#proposed-solution Signed-off-by: thatguywob --- changelog.d/added-native-merge-queue.md | 1 + src/docs/native-merge-queue.md | 38 +++ .../automerge/automerge_contributor_lane.go | 5 + .../automerge_contributor_lane_test.go | 2 + .../automerge/automerge_native_queue.go | 192 ++++++++++++ .../automerge/automerge_native_queue_test.go | 290 ++++++++++++++++++ src/pkg/github/automerge/automerge_sweep.go | 28 +- .../github/automerge/automerge_sweep_test.go | 11 +- src/pkg/github/native_merge_queue.go | 95 ++++++ src/pkg/github/native_merge_queue_test.go | 101 ++++++ src/pkg/stageengine/derive_pr_classify.go | 2 +- .../derive_pr_merge_refusal_test.go | 2 + 12 files changed, 758 insertions(+), 9 deletions(-) create mode 100644 changelog.d/added-native-merge-queue.md create mode 100644 src/docs/native-merge-queue.md create mode 100644 src/pkg/github/automerge/automerge_native_queue.go create mode 100644 src/pkg/github/automerge/automerge_native_queue_test.go create mode 100644 src/pkg/github/native_merge_queue.go create mode 100644 src/pkg/github/native_merge_queue_test.go diff --git a/changelog.d/added-native-merge-queue.md b/changelog.d/added-native-merge-queue.md new file mode 100644 index 0000000000..e622f71e3b --- /dev/null +++ b/changelog.d/added-native-merge-queue.md @@ -0,0 +1 @@ +- Automerge sweeps enqueue eligible PRs into GitHub's native merge queue with the validated head pinned, report queue position and state, and withdraw Hive-enqueued entries when their merge gates break while preserving entries queued by people. diff --git a/src/docs/native-merge-queue.md b/src/docs/native-merge-queue.md new file mode 100644 index 0000000000..35a566d0f2 --- /dev/null +++ b/src/docs/native-merge-queue.md @@ -0,0 +1,38 @@ +# Native GitHub merge queues + +Automerge sweeps detect a PR's target queue with GraphQL +`PullRequest.isMergeQueueEnabled`. Once the lane's existing approvals, trust, +holds, review, CI and soak gates allow a merge, Hive calls +`enqueuePullRequest` with `expectedHeadOid` set to the validated head. GitHub +runs the merge group's checks and performs the eventual merge. Branches without +a queue keep the direct REST merge path. + +Hive uses its GitHub App installation transport. Static tokens (including a +workflow's `GITHUB_TOKEN`) retain the existing direct-merge behavior and +`merge-queue-required` refusal, since workflow-token events cannot reliably +start `merge_group` workflows. The App installation must grant the existing +merge permissions (`contents: write` and `pull_requests: write`). Permission +or GraphQL failures are surfaced as queue failures; Hive never falls back to +bypassing a detected queue. This change does not request wider App permissions. + +Already queued PRs are left in place. The sweep reason reports +`native-merge-queued: queued (position N, STATE)` and maps to +`stage/100-merge-queue`; enqueueing does not emit a completed-merge event or +close an issue. The stage observation carries the reason to the dashboard. + +When a lane rejects an entry after a hold, withdrawn approval, changes request, +or stale approval, Hive rechecks the entry's enqueuer and withdraws it only if +it belongs to this App. Entries from a person or another App are preserved. +The queued sweep also withdraws this App's entries on paused repositories or +repositories with automerge disabled. Every enqueue and dequeue uses the +existing mutation boundary, including its write denials. Reconciliation is +periodic/event-driven, so a gate change can race GitHub's eventual merge. + +Queue rules choose the merge method. Where Hive elected a merge commit to +preserve mixed authorship and DCO attribution, the queue must use `MERGE`; +otherwise Hive reports a queue-method failure rather than enqueueing. + +Required workflows must subscribe to `merge_group` in addition to their PR +triggers. Workflow-trigger diagnosis is not automatic in this implementation. +See GitHub's [merge queue guide](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/configuring-pull-request-merges/managing-a-merge-queue) +and [GraphQL pull request reference](https://docs.github.com/en/graphql/reference/pulls). diff --git a/src/pkg/github/automerge/automerge_contributor_lane.go b/src/pkg/github/automerge/automerge_contributor_lane.go index f78a190dc9..6651bb4f71 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane.go +++ b/src/pkg/github/automerge/automerge_contributor_lane.go @@ -443,6 +443,7 @@ func (c *Engine) SweepContributorLaneAutoMerges(ctx context.Context, opts AutoMe } result.Seen++ stage := func(reason string, merged bool, err error) { + c.reconcileNativeQueueGate(ctx, stageLaneContributor, owner, repoName, pr, reason) c.recordStage(stageLaneContributor, displayRepo, owner, repoName, pr, reason, merged, err) } if _, held := train.duplicateOf(number); held { @@ -1476,6 +1477,10 @@ func (c *Engine) trySweepContributorLanePR(ctx context.Context, displayRepo, own } author, headSHA, claim, verdict, method, mergeClaim := eval.author, eval.headSHA, eval.claim, eval.verdict, eval.method, eval.mergeClaim fullRepo := owner + "/" + repo + if reason, err := c.tryNativeQueue(ctx, eval.pr, mergeClaim); reason != "" || err != nil { + return AutoMergeSweepEvent{}, reason, err + } + attempt := mergeAttempt{owner: owner, repo: repo, base: eval.pr.GetBase().GetRef(), number: number, head: headSHA, state: eval.pr.GetMergeableState(), method: method} if refused := c.knownMergeRefusal(attempt); refused != "" { return AutoMergeSweepEvent{}, refused, nil diff --git a/src/pkg/github/automerge/automerge_contributor_lane_test.go b/src/pkg/github/automerge/automerge_contributor_lane_test.go index db17b8a562..d888405bd3 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane_test.go +++ b/src/pkg/github/automerge/automerge_contributor_lane_test.go @@ -49,6 +49,7 @@ func contributorClaimMarkerTier(who, kind, hive, fields string) string { } type contributorLaneFixture struct { + nodeID string author string body string labels []string @@ -175,6 +176,7 @@ func newContributorLaneAPI(t *testing.T, fx *contributorLaneFixture) *httptest.S } pull := func(sha string) map[string]any { p := map[string]any{ + "node_id": fx.nodeID, "number": 7, "state": "open", "draft": fx.draft, diff --git a/src/pkg/github/automerge/automerge_native_queue.go b/src/pkg/github/automerge/automerge_native_queue.go new file mode 100644 index 0000000000..d5b3c142e4 --- /dev/null +++ b/src/pkg/github/automerge/automerge_native_queue.go @@ -0,0 +1,192 @@ +package automerge + +import ( + "context" + "fmt" + "strconv" + "strings" + + gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/effects" + hgithub "github.com/hivecommons/hive/pkg/github" +) + +const nativeQueueReason = "native-merge-queued" + +type nativeQueueTransport interface { + NativeMergeQueueAvailable() bool + ReadNativeMergeQueue(context.Context, string) (hgithub.NativeMergeQueueState, error) + EnqueueNativePullRequest(context.Context, string, string) (*hgithub.NativeMergeQueueEntry, error) + DequeueNativePullRequest(context.Context, string) error +} + +func (c *Engine) nativeQueue() nativeQueueTransport { + t, ok := c.transport.(nativeQueueTransport) + if !ok || !t.NativeMergeQueueAvailable() { + return nil + } + return t +} + +// tryNativeQueue runs only after the lane's existing merge gates passed. A +// queued PR is not a merged PR: no merged audit, issue closure or merge event. +func (c *Engine) tryNativeQueue(ctx context.Context, pr *gh.PullRequest, claim effects.Claim) (string, error) { + t := c.nativeQueue() + if t == nil || pr.GetNodeID() == "" { + return "", nil + } + state, err := t.ReadNativeMergeQueue(ctx, pr.GetNodeID()) + if err != nil { + return "native-merge-queue-read", err + } + if !state.IsMergeQueueEnabled && !state.IsInMergeQueue { + return "", nil + } + if state.HeadRefOid != claim.Inputs["expect_sha"] { + return "head-changed-since-eval", nil + } + // A queue chooses its own merge method. Preserve the attribution gate + // that elected a merge commit to retain each author's signed commits. + if claim.Inputs["method_reason"] == mergeMethodReasonMixedAuthorship { + mt, ok := c.transport.(interface { + NativeMergeQueueMethod(context.Context, string, string) (string, error) + }) + if !ok { + return "native-merge-queue-method", fmt.Errorf("queue merge method cannot be verified") + } + method, err := mt.NativeMergeQueueMethod(ctx, state.ID, pr.GetBase().GetRef()) + if err != nil { + return "native-merge-queue-method", err + } + if !strings.EqualFold(method, "MERGE") { + return "native-merge-queue-method", fmt.Errorf("mixed authorship requires a merge commit; queue uses %s", method) + } + } + if !state.IsInMergeQueue { + // Each removal starts a new queue attempt, even at the same head. + // The previous applied journal entry must never suppress re-enqueue. + cycle := "" + if nodes := state.TimelineItems.Nodes; len(nodes) > 0 { + cycle = nodes[len(nodes)-1].ID + } + claim.Inputs = map[string]string{"operation": "enqueue", "expected_head_sha": state.HeadRefOid, "after_removal": cycle} + observe := func(ctx context.Context) (effects.ExternalState, error) { + current, err := t.ReadNativeMergeQueue(ctx, pr.GetNodeID()) + if err != nil { + return effects.ExternalState{}, err + } + result := effects.ExternalState{Applied: current.IsInMergeQueue} + if current.MergeQueueEntry != nil { + result.Provenance = current.MergeQueueEntry.ID + } + return result, nil + } + _, err = effects.ExecuteReconciling(ctx, c.mutation, claim, observe, effects.DefaultUnresolvedTTL, func(ctx context.Context) (effects.Result, error) { + entry, err := t.EnqueueNativePullRequest(ctx, state.ID, state.HeadRefOid) + if err != nil { + return effects.Result{}, err + } + state.MergeQueueEntry = entry + return effects.Result{Provenance: entry.ID}, nil + }) + if err != nil { + return "native-merge-queue-enqueue", err + } + } + if e := state.MergeQueueEntry; e != nil { + c.info("automerge queued", "repo", claim.Repo, "pr", pr.GetNumber(), "position", e.Position, "state", e.State) + return fmt.Sprintf("%s: queued (position %d, %s)", nativeQueueReason, e.Position, e.State), nil + } + return nativeQueueReason, nil +} + +// reconcileNativeQueueGate runs for the owning lane's rejected candidates, +// including cheap prefilter holds. Never withdraw another actor's entry. +func (c *Engine) reconcileNativeQueueGate(ctx context.Context, lane, owner, repo string, pr *gh.PullRequest, reason string) { + t := c.nativeQueue() + if t == nil || pr == nil || pr.GetNodeID() == "" || reason == "" || strings.HasPrefix(reason, nativeQueueReason) { + return + } + // Routing observations are not eligibility decisions by the owning lane. + switch reason { + case "not-app-authored", trustedAuthorReasonAppAuthored, trustedBotLaneSkipReason, contributorLaneReasonAppAuthored: + return + } + author := hgithub.SafeGetLogin(pr.GetUser()) + if _, own := c.sweepLaneForAuthor(author); own && lane != stageLaneSelf && lane != "inactive" { + return + } + if lane == stageLaneTrusted && reason == "untrusted-author-role" { + return + } + if lane == stageLaneContributor && c.trustedAuthorDecision(author, c.currentTrustedAuthorPolicy().RequireRole).Allowed { + return + } + login := strings.TrimSpace(c.transport.AppBotLogin()) + if login == "" { + return + } + state, err := t.ReadNativeMergeQueue(ctx, pr.GetNodeID()) + if err == nil && state.IsInMergeQueue && state.MergeQueueEntry != nil && strings.EqualFold(state.MergeQueueEntry.Enqueuer.Login, login) { + entryID := state.MergeQueueEntry.ID + claim := effects.Claim{Repo: owner + "/" + repo, Kind: effects.KindPullRequestMerge, Target: strconv.Itoa(pr.GetNumber()), Actor: "automerge", Inputs: map[string]string{"operation": "dequeue", "entry": entryID}} + observe := func(ctx context.Context) (effects.ExternalState, error) { + current, err := t.ReadNativeMergeQueue(ctx, pr.GetNodeID()) + if err != nil { + return effects.ExternalState{}, err + } + return effects.ExternalState{Applied: !current.IsInMergeQueue || current.MergeQueueEntry == nil || current.MergeQueueEntry.ID != entryID, Provenance: entryID}, nil + } + _, err = effects.ExecuteReconciling(ctx, c.mutation, claim, observe, effects.DefaultUnresolvedTTL, func(ctx context.Context) (effects.Result, error) { + // Recheck provenance immediately before the write; a person may have + // removed and re-enqueued the PR since the lane evaluated it. + current, err := t.ReadNativeMergeQueue(ctx, pr.GetNodeID()) + if err != nil { + return effects.Result{}, err + } + if !current.IsInMergeQueue || current.MergeQueueEntry == nil || current.MergeQueueEntry.ID != entryID || !strings.EqualFold(current.MergeQueueEntry.Enqueuer.Login, login) { + return effects.Result{}, nil + } + if err := t.DequeueNativePullRequest(ctx, state.ID); err != nil { + return effects.Result{}, err + } + return effects.Result{Provenance: entryID}, nil + }) + } + if err != nil { + c.warn("native merge queue gate reconciliation failed", "repo", owner+"/"+repo, "pr", pr.GetNumber(), "reason", reason, "error", err) + } +} + +// Pausing a repo stops new merges, but GitHub may still hold entries from a +// previous pass. Withdraw only this App's entries through the usual boundary. +func (c *Engine) reconcileInactiveNativeQueues(ctx context.Context) { + if c.nativeQueue() == nil { + return + } + active := map[string]bool{} + for _, repo := range c.activeRepos() { + active[repo] = true + } + for _, display := range c.transport.Repositories() { + if (active[display] && c.repoAutoMergeAllowed(display)) || (c.repoOnGitHub != nil && !c.repoOnGitHub(display)) { + continue + } + owner, repo := c.transport.SplitRepo(display) + opts := &gh.PullRequestListOptions{State: "open", ListOptions: gh.ListOptions{PerPage: 100}} + for { + prs, resp, err := c.gh.PullRequests.List(ctx, owner, repo, opts) + if err != nil { + c.warn("native merge queue pause reconciliation failed", "repo", display, "error", err) + break + } + for _, pr := range prs { + c.reconcileNativeQueueGate(ctx, "inactive", owner, repo, pr, "repo-paused-or-disabled") + } + if resp == nil || resp.NextPage == 0 { + break + } + opts.Page = resp.NextPage + } + } +} diff --git a/src/pkg/github/automerge/automerge_native_queue_test.go b/src/pkg/github/automerge/automerge_native_queue_test.go new file mode 100644 index 0000000000..6033dae0be --- /dev/null +++ b/src/pkg/github/automerge/automerge_native_queue_test.go @@ -0,0 +1,290 @@ +package automerge + +import ( + "context" + "errors" + "strings" + "testing" + "time" + + gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/effects" + hgithub "github.com/hivecommons/hive/pkg/github" +) + +type nativeQueueFake struct { + Transport + state hgithub.NativeMergeQueueState + readErr, enqueueErr error + enqueues, dequeues int + expectedHead string +} + +func (f *nativeQueueFake) NativeMergeQueueAvailable() bool { return true } +func (f *nativeQueueFake) ReadNativeMergeQueue(context.Context, string) (hgithub.NativeMergeQueueState, error) { + return f.state, f.readErr +} +func (f *nativeQueueFake) EnqueueNativePullRequest(_ context.Context, _, head string) (*hgithub.NativeMergeQueueEntry, error) { + f.enqueues++ + f.expectedHead = head + if f.enqueueErr != nil { + return nil, f.enqueueErr + } + f.state.IsInMergeQueue = true + f.state.MergeQueueEntry = queueEntry(testHiveAppBotLogin) + return f.state.MergeQueueEntry, nil +} +func (f *nativeQueueFake) DequeueNativePullRequest(context.Context, string) error { + f.dequeues++ + f.state.IsInMergeQueue = false + f.state.MergeQueueEntry = nil + return nil +} +func queueEntry(login string) *hgithub.NativeMergeQueueEntry { + e := &hgithub.NativeMergeQueueEntry{ID: "entry1", Position: 2, State: "AWAITING_CHECKS"} + e.Enqueuer.Login = login + return e +} +func nativeQueueEngine() (*Engine, *nativeQueueFake, *gh.PullRequest, effects.Claim) { + c := newAutoMergeSweepClient("http://unused.invalid") + f := &nativeQueueFake{Transport: c.transport, state: hgithub.NativeMergeQueueState{ID: "PR_7", HeadRefOid: "sha7", IsMergeQueueEnabled: true}} + c.transport = f + pr := &gh.PullRequest{Number: gh.Ptr(7), NodeID: gh.Ptr("PR_7"), User: &gh.User{Login: gh.Ptr(testHiveAppBotLogin)}} + claim := effects.Claim{Repo: "acme/widget", Kind: effects.KindPullRequestMerge, Target: "7", Actor: "automerge", Inputs: mergeClaimInputs("squash", "", "sha7", stageLaneSelf)} + return c, f, pr, claim +} + +func TestNativeQueueEnqueueIsPinnedAndIdempotent(t *testing.T) { + c, f, pr, claim := nativeQueueEngine() + b := &recordingBoundary{} + c.mutation = b + for i := 0; i < 2; i++ { + reason, err := c.tryNativeQueue(context.Background(), pr, claim) + if err != nil || !strings.Contains(reason, "queued (position 2, AWAITING_CHECKS)") { + t.Fatalf("reason=%q error=%v", reason, err) + } + } + if f.enqueues != 1 || f.expectedHead != "sha7" || len(b.claims) != 1 || b.claims[0].Inputs["operation"] != "enqueue" { + t.Fatalf("enqueues=%d head=%q claims=%+v", f.enqueues, f.expectedHead, b.claims) + } +} + +func TestNativeQueueFailureModes(t *testing.T) { + for _, mode := range []string{"no-queue", "head-moved", "lookup-error", "enqueue-error", "denied", "human-queued"} { + t.Run(mode, func(t *testing.T) { + c, f, pr, claim := nativeQueueEngine() + switch mode { + case "no-queue": + f.state.IsMergeQueueEnabled = false + case "head-moved": + f.state.HeadRefOid = "new-head" + case "lookup-error": + f.readErr = errors.New("unavailable") + case "enqueue-error": + f.enqueueErr = errors.New("expectedHeadOid mismatch") + case "denied": + c.mutation = denyNativeQueueBoundary{} + case "human-queued": + f.state.IsInMergeQueue = true + f.state.MergeQueueEntry = queueEntry("maintainer") + } + reason, err := c.tryNativeQueue(context.Background(), pr, claim) + if mode == "no-queue" && (reason != "" || err != nil) { + t.Fatalf("direct merge should proceed: %q %v", reason, err) + } + if mode == "head-moved" && reason != "head-changed-since-eval" { + t.Fatalf("reason=%q", reason) + } + if (mode == "lookup-error" || mode == "enqueue-error" || mode == "denied") && err == nil { + t.Fatal("expected failure") + } + wantCalls := 0 + if mode == "enqueue-error" { + wantCalls = 1 + } + if f.enqueues != wantCalls || f.dequeues != 0 { + t.Fatalf("enqueues=%d dequeues=%d", f.enqueues, f.dequeues) + } + }) + } +} + +type denyNativeQueueBoundary struct{} + +func (denyNativeQueueBoundary) Execute(context.Context, effects.Claim, func(context.Context) (effects.Result, error)) (effects.Result, error) { + return effects.Result{}, effects.ErrDenied +} + +func TestNativeQueueGateWithdrawsOnlyHiveEntries(t *testing.T) { + for _, reason := range []string{"held", "changes-requested", "no-approval", "stale-approval", "label:hold"} { + for _, actor := range []string{testHiveAppBotLogin, "maintainer", "another-app[bot]", ""} { + t.Run(reason+"/"+actor, func(t *testing.T) { + c, f, pr, _ := nativeQueueEngine() + f.state.IsInMergeQueue = true + f.state.MergeQueueEntry = queueEntry(actor) + c.reconcileNativeQueueGate(context.Background(), stageLaneSelf, "acme", "widget", pr, reason) + want := 0 + if actor == testHiveAppBotLogin { + want = 1 + } + if f.dequeues != want { + t.Fatalf("dequeues=%d want=%d", f.dequeues, want) + } + }) + } + } +} + +func TestNativeQueueDequeueHonorsBoundaryAndLaneOwnership(t *testing.T) { + for _, mode := range []string{"denied", "different-lane", "eligible"} { + t.Run(mode, func(t *testing.T) { + c, f, pr, _ := nativeQueueEngine() + f.state.IsInMergeQueue = true + f.state.MergeQueueEntry = queueEntry(testHiveAppBotLogin) + lane, reason := stageLaneSelf, "held" + if mode == "denied" { + c.mutation = denyNativeQueueBoundary{} + } + if mode == "different-lane" { + lane = stageLaneContributor + } + if mode == "eligible" { + reason = nativeQueueReason + } + c.reconcileNativeQueueGate(context.Background(), lane, "acme", "widget", pr, reason) + if f.dequeues != 0 { + t.Fatal("must preserve queued entry") + } + }) + } +} + +// Exercise the real lane and its cheap prefilter, not only the queue helper. +func TestSelfAuthoredSweepNativeQueueAndHold(t *testing.T) { + for _, held := range []bool{false, true} { + t.Run(map[bool]string{false: "eligible", true: "held"}[held], func(t *testing.T) { + var merged []int + labels := []string(nil) + if held { + labels = []string{"hold"} + } + api := newAutoMergeSweepAPI(t, "", []sweepPR{{number: 7, nodeID: "PR_7", author: testHiveAppBotLogin, mergeableState: "clean", statusState: "success", checkStatus: "completed", checkConclusion: "success", extraLabels: labels}}, &merged) + defer api.Close() + c := newAutoMergeSweepClient(api.URL) + f := &nativeQueueFake{Transport: c.transport, state: hgithub.NativeMergeQueueState{ID: "PR_7", HeadRefOid: "sha7", IsMergeQueueEnabled: true}} + c.transport = f + if held { + f.state.IsInMergeQueue = true + f.state.MergeQueueEntry = queueEntry(testHiveAppBotLogin) + } + for pass := 0; pass < 2; pass++ { + res, err := c.SweepSelfAuthoredAutoMerges(context.Background(), AutoMergeSweepOptions{}) + if err != nil || len(res.Merged) != 0 || len(merged) != 0 { + t.Fatalf("res=%+v direct=%v error=%v", res, merged, err) + } + } + if held && (f.enqueues != 0 || f.dequeues != 1) { + t.Fatalf("held enqueues=%d dequeues=%d", f.enqueues, f.dequeues) + } + if !held && f.enqueues != 1 { + t.Fatalf("enqueues=%d", f.enqueues) + } + }) + } +} + +func TestHumanMergeLanesEnqueueWithoutDirectMerge(t *testing.T) { + for _, lane := range []string{"queued", "trusted-author"} { + t.Run(lane, func(t *testing.T) { + var merged []int + api := newAutoMergeSweepAPI(t, hgithub.AutoMergeQueuedLabel, []sweepPR{{number: 7, nodeID: "PR_7", author: "alice", queuedBy: "bob", label: true, mergeableState: "clean", statusState: "success", checkStatus: "completed", checkConclusion: "success"}}, &merged) + defer api.Close() + c := newAutoMergeSweepClient(api.URL) + f := &nativeQueueFake{Transport: c.transport, state: hgithub.NativeMergeQueueState{ID: "PR_7", HeadRefOid: "sha7", IsMergeQueueEnabled: true}} + c.transport = f + c.trustedAuthorizer = func(string, string) TrustedAuthorDecision { + return TrustedAuthorDecision{Allowed: true, Role: "merger"} + } + var result *AutoMergeSweepResult + var err error + if lane == "queued" { + result, err = c.SweepQueuedAutoMerges(context.Background(), AutoMergeSweepOptions{}) + } else { + c.trustedAuthorPolicy = func() TrustedAuthorPolicy { return TrustedAuthorPolicy{Enabled: true, RequireRole: "merger"} } + result, err = c.SweepTrustedAuthorAutoMerges(context.Background(), AutoMergeSweepOptions{}) + } + if err != nil || len(merged) != 0 || len(result.Merged) != 0 || f.enqueues != 1 { + t.Fatalf("result=%+v merged=%v enqueues=%d error=%v", result, merged, f.enqueues, err) + } + }) + } +} + +func (f *nativeQueueFake) ActiveRepositories() []string { + if p, ok := f.Transport.(interface{ ActiveRepositories() []string }); ok { + return p.ActiveRepositories() + } + return f.Repositories() +} + +func TestNativeQueuePauseWithdrawsOwnEntry(t *testing.T) { + var merged []int + api := newAutoMergeSweepAPI(t, "", []sweepPR{{number: 7, nodeID: "PR_7", author: testHiveAppBotLogin}}, &merged) + defer api.Close() + c := newAutoMergeSweepClient(api.URL) + client := c.transport.(*hgithub.Client) + client.SetRepoPausedFunc(func(string) bool { return true }) + f := &nativeQueueFake{Transport: client, state: hgithub.NativeMergeQueueState{ID: "PR_7", IsMergeQueueEnabled: true, IsInMergeQueue: true, MergeQueueEntry: queueEntry(testHiveAppBotLogin)}} + c.transport = f + c.reconcileInactiveNativeQueues(context.Background()) + if f.dequeues != 1 || f.enqueues != 0 || len(merged) != 0 { + t.Fatalf("dequeues=%d enqueues=%d merged=%v", f.dequeues, f.enqueues, merged) + } +} + +func (f *nativeQueueFake) NativeMergeQueueMethod(context.Context, string, string) (string, error) { + return "SQUASH", nil +} + +func TestNativeQueuePreservesMixedAuthorshipGate(t *testing.T) { + c, f, pr, claim := nativeQueueEngine() + claim.Inputs["method_reason"] = mergeMethodReasonMixedAuthorship + reason, err := c.tryNativeQueue(context.Background(), pr, claim) + if reason != "native-merge-queue-method" || err == nil || f.enqueues != 0 { + t.Fatalf("reason=%q error=%v enqueues=%d", reason, err, f.enqueues) + } +} + +func TestContributorLaneEnqueuesAfterGates(t *testing.T) { + fx := validContributorFixture() + fx.nodeID = "PR_7" + api := newContributorLaneAPI(t, &fx) + defer api.Close() + policy := defaultContributorLanePolicy() + clock := &contributorLaneClock{at: time.Date(2026, 10, 15, 13, 0, 0, 0, time.UTC)} + c := newContributorLaneEngine(api.URL, policy, clock, Options{ContributorSoak: soakedTracker(clock)}) + f := &nativeQueueFake{Transport: c.transport, state: hgithub.NativeMergeQueueState{ID: "PR_7", HeadRefOid: "sha7", IsMergeQueueEnabled: true}} + c.transport = f + event, reason, err := c.trySweepContributorLanePR(context.Background(), "widget", "acme", "widget", 7, policy) + if err != nil || !strings.HasPrefix(reason, nativeQueueReason) || f.enqueues != 1 || fx.merges != 0 || event.MergeSHA != "" { + t.Fatalf("event=%+v reason=%q error=%v enqueues=%d merges=%d", event, reason, err, f.enqueues, fx.merges) + } +} + +func TestNativeQueueNewCycleAfterRemoval(t *testing.T) { + c, f, pr, claim := nativeQueueEngine() + b := &recordingBoundary{} + c.mutation = b + if _, err := c.tryNativeQueue(context.Background(), pr, claim); err != nil { + t.Fatal(err) + } + f.state.IsInMergeQueue = false + f.state.MergeQueueEntry = nil + f.state.TimelineItems.Nodes = append(f.state.TimelineItems.Nodes, struct{ ID string }{ID: "removal1"}) + if _, err := c.tryNativeQueue(context.Background(), pr, claim); err != nil { + t.Fatal(err) + } + if f.enqueues != 2 || len(b.claims) != 2 || b.claims[0].Inputs["after_removal"] == b.claims[1].Inputs["after_removal"] { + t.Fatalf("enqueues=%d claims=%+v", f.enqueues, b.claims) + } +} diff --git a/src/pkg/github/automerge/automerge_sweep.go b/src/pkg/github/automerge/automerge_sweep.go index 2c9cfff8e1..7a449baa03 100644 --- a/src/pkg/github/automerge/automerge_sweep.go +++ b/src/pkg/github/automerge/automerge_sweep.go @@ -962,6 +962,7 @@ func (c *Engine) SweepQueuedAutoMerges(ctx context.Context, opts AutoMergeSweepO if !c.ready() { return nil, hgithub.ErrNoGitHubClient } + c.reconcileInactiveNativeQueues(ctx) maxMerges := opts.MaxMerges if maxMerges <= 0 { maxMerges = DefaultAutoMergeSweepMaxMerges @@ -998,6 +999,11 @@ func (c *Engine) SweepQueuedAutoMerges(ctx context.Context, opts AutoMergeSweepO } result.Seen++ if reason := c.prefilterQueuedIssue(issue, label); reason != "" { + if c.nativeQueue() != nil { + if pr, _, err := c.gh.PullRequests.Get(ctx, owner, repoName, issue.GetNumber()); err == nil { + c.reconcileNativeQueueGate(ctx, "queued", owner, repoName, pr, reason) + } + } if reason == "held" || reason == "exempt-label" { c.debug("automerge sweep skipped PR", "repo", repo, "pr", issue.GetNumber(), "reason", reason) } else { @@ -1130,6 +1136,7 @@ func (c *Engine) SweepSelfAuthoredAutoMerges(ctx context.Context, opts AutoMerge result.Seen++ repoSeen++ stage := func(reason string, merged bool, err error) { + c.reconcileNativeQueueGate(ctx, stageLaneSelf, owner, repoName, pr, reason) c.recordStage(stageLaneSelf, repo, owner, repoName, pr, reason, merged, err) } if _, held := train.duplicateOf(number); held { @@ -1366,6 +1373,7 @@ func (c *Engine) SweepTrustedAuthorAutoMerges(ctx context.Context, opts AutoMerg } result.Seen++ stage := func(reason string, merged bool, err error) { + c.reconcileNativeQueueGate(ctx, stageLaneTrusted, owner, repoName, pr, reason) c.recordStage(stageLaneTrusted, displayRepo, owner, repoName, pr, reason, merged, err) } if _, held := train.duplicateOf(number); held { @@ -2011,9 +2019,12 @@ func (c *Engine) trySweepSelfAuthoredPR(ctx context.Context, displayRepo, owner, return AutoMergeSweepEvent{}, reviewReason, nil } - // The App has no merge-queue enqueue path, and the direct merge on a - // merge-queue-only base is refused every time (hivecommons/hive#14068); - // so is one that needs an approving review or a disabled merge method. + // Queue-enabled bases use GitHub's native queue after the same gates. + // Other bases retain the direct merge and remembered refusal handling. + if reason, err := c.tryNativeQueue(ctx, current, mergeClaim); reason != "" || err != nil { + return AutoMergeSweepEvent{}, reason, err + } + attempt := mergeAttempt{owner: owner, repo: repo, base: baseBranch, number: number, head: evaluatedHeadSHA, state: current.GetMergeableState(), method: method} if refused := c.knownMergeRefusal(attempt); refused != "" { return AutoMergeSweepEvent{}, refused, nil @@ -2456,6 +2467,10 @@ func (c *Engine) trySweepTrustedAuthorPR(ctx context.Context, displayRepo, owner if reviewReason := c.mergeReviewGate(ctx, displayRepo, owner, repo, stageLaneTrusted, number, current, headSHA, priorApprover, priorChecked); reviewReason != "" { return AutoMergeSweepEvent{}, reviewReason, nil } + if reason, err := c.tryNativeQueue(ctx, current, mergeClaim); reason != "" || err != nil { + return AutoMergeSweepEvent{}, reason, err + } + attempt := mergeAttempt{owner: owner, repo: repo, base: baseBranch, number: number, head: headSHA, state: current.GetMergeableState(), method: method} if refused := c.knownMergeRefusal(attempt); refused != "" { return AutoMergeSweepEvent{}, refused, nil @@ -2671,7 +2686,7 @@ func (c *Engine) commentTrustedAuthorMerge(ctx context.Context, owner, repo stri return err } -func (c *Engine) trySweepQueuedPR(ctx context.Context, displayRepo, owner, repo string, number int, label string) (AutoMergeSweepEvent, string, error) { +func (c *Engine) trySweepQueuedPR(ctx context.Context, displayRepo, owner, repo string, number int, label string) (eventOut AutoMergeSweepEvent, reasonOut string, errOut error) { pr, _, err := c.gh.PullRequests.Get(hgithub.WithRESTCaller(ctx, "hive:automerge_sweep"), owner, repo, number) if err != nil { if isGitHubStatus(err, http.StatusNotFound) { @@ -2679,6 +2694,7 @@ func (c *Engine) trySweepQueuedPR(ctx context.Context, displayRepo, owner, repo } return AutoMergeSweepEvent{}, "fetch-pr", err } + defer func() { c.reconcileNativeQueueGate(ctx, "queued", owner, repo, pr, reasonOut) }() c.noteStagePR(owner, repo, number, pr) if !strings.EqualFold(pr.GetState(), "open") { return AutoMergeSweepEvent{}, "closed", nil @@ -2813,6 +2829,10 @@ func (c *Engine) trySweepQueuedPR(ctx context.Context, displayRepo, owner, repo return AutoMergeSweepEvent{}, deskReason, nil } + if reason, err := c.tryNativeQueue(ctx, pr, mergeClaim); reason != "" || err != nil { + return AutoMergeSweepEvent{}, reason, err + } + attempt := mergeAttempt{owner: owner, repo: repo, base: baseBranch, number: number, head: headSHA, state: pr.GetMergeableState(), method: method} if refused := c.knownMergeRefusal(attempt); refused != "" { return AutoMergeSweepEvent{}, refused, nil diff --git a/src/pkg/github/automerge/automerge_sweep_test.go b/src/pkg/github/automerge/automerge_sweep_test.go index e89027eca7..d59bfe07b0 100644 --- a/src/pkg/github/automerge/automerge_sweep_test.go +++ b/src/pkg/github/automerge/automerge_sweep_test.go @@ -21,6 +21,7 @@ import ( const testHiveAppBotLogin = "kubestellar-hive[bot]" type sweepPR struct { + nodeID string number int author string queuedBy string @@ -2930,10 +2931,11 @@ func newAutoMergeSweepAPI(t *testing.T, expectedLabel string, prs []sweepPR, mer headRepo = "acme/widget" } pulls = append(pulls, map[string]any{ - "number": pr.number, - "state": "open", - "draft": pr.draft, - "user": map[string]string{"login": pr.author}, + "node_id": pr.nodeID, + "number": pr.number, + "state": "open", + "draft": pr.draft, + "user": map[string]string{"login": pr.author}, "head": map[string]any{ "sha": headSHA, "repo": map[string]string{"full_name": headRepo}, @@ -2987,6 +2989,7 @@ func newAutoMergeSweepAPI(t *testing.T, expectedLabel string, prs []sweepPR, mer headRepo = "acme/widget" } json.NewEncoder(w).Encode(map[string]any{ + "node_id": pr.nodeID, "number": pr.number, "state": "open", "draft": pr.draft, diff --git a/src/pkg/github/native_merge_queue.go b/src/pkg/github/native_merge_queue.go new file mode 100644 index 0000000000..f49e26bdd7 --- /dev/null +++ b/src/pkg/github/native_merge_queue.go @@ -0,0 +1,95 @@ +package github + +import ( + "context" + "fmt" +) + +// NativeMergeQueueEntry is GitHub's current queue position and provenance. +type NativeMergeQueueEntry struct { + ID string + Position int + State string + Enqueuer struct{ Login string } +} + +// NativeMergeQueueState describes the queue on a PR's target branch. +type NativeMergeQueueState struct { + ID string + HeadRefOid string + IsMergeQueueEnabled bool + IsInMergeQueue bool + MergeQueueEntry *NativeMergeQueueEntry + TimelineItems struct{ Nodes []struct{ ID string } } +} + +// NativeMergeQueueAvailable excludes static tokens, notably workflow tokens: +// only App installation events reliably start merge_group workflows. +func (c *Client) NativeMergeQueueAvailable() bool { return c != nil && c.appAuth != nil } + +func (c *Client) ReadNativeMergeQueue(ctx context.Context, id string) (NativeMergeQueueState, error) { + var out struct{ Node *NativeMergeQueueState } + err := c.graphQL(ctx, `query($id: ID!) { node(id: $id) { ... on PullRequest { + id headRefOid isMergeQueueEnabled isInMergeQueue + mergeQueueEntry { id position state enqueuer { login } } + timelineItems(last: 1, itemTypes: [REMOVED_FROM_MERGE_QUEUE_EVENT]) { nodes { ... on RemovedFromMergeQueueEvent { id } } } + } } }`, map[string]any{"id": id}, &out) + if err != nil { + return NativeMergeQueueState{}, err + } + if out.Node == nil || out.Node.ID == "" { + return NativeMergeQueueState{}, fmt.Errorf("merge queue: pull request %s unavailable", id) + } + return *out.Node, nil +} + +// EnqueueNativePullRequest pins the exact head that passed Hive's gates. +func (c *Client) EnqueueNativePullRequest(ctx context.Context, id, head string) (*NativeMergeQueueEntry, error) { + if !c.NativeMergeQueueAvailable() { + return nil, fmt.Errorf("native merge queue requires App installation authentication") + } + var out struct { + EnqueuePullRequest struct{ MergeQueueEntry *NativeMergeQueueEntry } + } + err := c.graphQL(ctx, `mutation($input: EnqueuePullRequestInput!) { + enqueuePullRequest(input: $input) { mergeQueueEntry { id position state enqueuer { login } } } + }`, map[string]any{"input": map[string]any{"pullRequestId": id, "expectedHeadOid": head}}, &out) + if err != nil { + return nil, err + } + if out.EnqueuePullRequest.MergeQueueEntry == nil { + return nil, fmt.Errorf("enqueue returned no merge queue entry") + } + return out.EnqueuePullRequest.MergeQueueEntry, nil +} + +func (c *Client) DequeueNativePullRequest(ctx context.Context, id string) error { + if !c.NativeMergeQueueAvailable() { + return fmt.Errorf("native merge queue requires App installation authentication") + } + return c.graphQL(ctx, `mutation($input: DequeuePullRequestInput!) { + dequeuePullRequest(input: $input) { clientMutationId } + }`, map[string]any{"input": map[string]any{"pullRequestId": id}}, nil) +} + +// NativeMergeQueueMethod reads the branch-wide merge method: enqueue has no +// per-PR method override, so mixed-authorship PRs must verify this first. +func (c *Client) NativeMergeQueueMethod(ctx context.Context, id, branch string) (string, error) { + var out struct { + Node struct { + Repository struct { + MergeQueue *struct{ Configuration struct{ MergeMethod string } } + } + } + } + err := c.graphQL(ctx, `query($id: ID!, $branch: String!) { node(id: $id) { + ... on PullRequest { repository { mergeQueue(branch: $branch) { configuration { mergeMethod } } } } + } }`, map[string]any{"id": id, "branch": branch}, &out) + if err != nil { + return "", err + } + if out.Node.Repository.MergeQueue == nil { + return "", fmt.Errorf("merge queue configuration unavailable") + } + return out.Node.Repository.MergeQueue.Configuration.MergeMethod, nil +} diff --git a/src/pkg/github/native_merge_queue_test.go b/src/pkg/github/native_merge_queue_test.go new file mode 100644 index 0000000000..f96cc4b6fb --- /dev/null +++ b/src/pkg/github/native_merge_queue_test.go @@ -0,0 +1,101 @@ +package github + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestNativeMergeQueueGraphQL(t *testing.T) { + for _, mode := range []string{"read", "enqueue", "dequeue", "graphql-error", "missing-entry"} { + t.Run(mode, func(t *testing.T) { + calls := 0 + api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls++ + if r.Method != "POST" || r.URL.Path != "/graphql" { + t.Errorf("request=%s %s", r.Method, r.URL.Path) + } + var body struct { + Query string + Variables map[string]json.RawMessage + } + if err := json.NewDecoder(r.Body).Decode(&body); err != nil { + t.Error(err) + } + w.Header().Set("Content-Type", "application/json") + switch mode { + case "read": + if !strings.Contains(body.Query, "isMergeQueueEnabled") || string(body.Variables["id"]) != `"PR_7"` { + t.Errorf("query=%+v", body) + } + _, _ = w.Write([]byte(`{"data":{"node":{"id":"PR_7","headRefOid":"validated","isMergeQueueEnabled":true,"isInMergeQueue":true,"mergeQueueEntry":{"id":"entry","position":3,"state":"AWAITING_CHECKS","enqueuer":{"login":"hive[bot]"}}}}}`)) + case "graphql-error": + _, _ = w.Write([]byte(`{"errors":[{"message":"expectedHeadOid mismatch"}],"data":null}`)) + case "missing-entry": + _, _ = w.Write([]byte(`{"data":{"enqueuePullRequest":{"mergeQueueEntry":null}}}`)) + case "enqueue", "dequeue": + var input map[string]any + if err := json.Unmarshal(body.Variables["input"], &input); err != nil { + t.Error(err) + } + if input["pullRequestId"] != "PR_7" { + t.Errorf("input=%v", input) + } + if mode == "enqueue" { + if !strings.Contains(body.Query, "enqueuePullRequest") || input["expectedHeadOid"] != "validated" { + t.Errorf("query=%s input=%v", body.Query, input) + } + _, _ = w.Write([]byte(`{"data":{"enqueuePullRequest":{"mergeQueueEntry":{"id":"entry","position":3,"state":"AWAITING_CHECKS"}}}}`)) + } else { + if !strings.Contains(body.Query, "dequeuePullRequest") { + t.Errorf("query=%s", body.Query) + } + _, _ = w.Write([]byte(`{"data":{"dequeuePullRequest":{"clientMutationId":null}}}`)) + } + } + })) + defer api.Close() + c := NewClient("token", "acme", []string{"widget"}, nil, api.URL) + // Test transport only: real clients obtain this from NewAppClient. + c.appAuth = &AppAuth{} + switch mode { + case "read": + state, err := c.ReadNativeMergeQueue(context.Background(), "PR_7") + if err != nil || !state.IsMergeQueueEnabled || state.MergeQueueEntry == nil || state.MergeQueueEntry.Enqueuer.Login != "hive[bot]" { + t.Fatalf("state=%+v error=%v", state, err) + } + case "dequeue": + if err := c.DequeueNativePullRequest(context.Background(), "PR_7"); err != nil { + t.Fatal(err) + } + default: + entry, err := c.EnqueueNativePullRequest(context.Background(), "PR_7", "validated") + if mode == "enqueue" && (err != nil || entry == nil || entry.Position != 3) { + t.Fatalf("entry=%+v error=%v", entry, err) + } + if mode != "enqueue" && err == nil { + t.Fatal("expected error") + } + } + if calls != 1 { + t.Fatalf("requests=%d", calls) + } + }) + } +} + +func TestNativeMergeQueueRejectsStaticToken(t *testing.T) { + c := NewClient("token", "acme", []string{"widget"}, nil, "http://unused.invalid") + if c.NativeMergeQueueAvailable() { + t.Fatal("static token must not enqueue") + } + if _, err := c.EnqueueNativePullRequest(context.Background(), "PR_7", "head"); err == nil { + t.Fatal("static token accepted") + } + if err := c.DequeueNativePullRequest(context.Background(), "PR_7"); err == nil { + t.Fatal("static token accepted") + } +} diff --git a/src/pkg/stageengine/derive_pr_classify.go b/src/pkg/stageengine/derive_pr_classify.go index 9f875b983c..934b6145a7 100644 --- a/src/pkg/stageengine/derive_pr_classify.go +++ b/src/pkg/stageengine/derive_pr_classify.go @@ -490,7 +490,7 @@ func ClassifyStage(o stageObservation) stageVerdict { // 095, not 100 — the merge has not been attempted yet. return WithHumanApprovalReason(stageVerdict{Stage: stagelabels.PreMergeSoak, Rank: stageRankOwned}, humanApprovalReason) } - if stageMergeQueueReasons[reason] || strings.HasPrefix(reason, "release-hold") { + if stageMergeQueueReasons[reason] || strings.HasPrefix(reason, "native-merge-queue") || strings.HasPrefix(reason, "release-hold") { return WithHumanApprovalReason(stageVerdict{Stage: stagelabels.MergeQueue, Rank: stageRankOwned}, humanApprovalReason) } if reason == contributorLaneReasonNoVerdict { diff --git a/src/pkg/stageengine/derive_pr_merge_refusal_test.go b/src/pkg/stageengine/derive_pr_merge_refusal_test.go index 73cc1faa34..40993df985 100644 --- a/src/pkg/stageengine/derive_pr_merge_refusal_test.go +++ b/src/pkg/stageengine/derive_pr_merge_refusal_test.go @@ -23,6 +23,8 @@ func TestClassifyStagePermanentMergeRefusals(t *testing.T) { {name: "method not allowed", reason: autoMergeReasonMergeRefusedMethod, want: stagelabels.NeedsDecision, wantHuman: true}, {name: "review required but needs-rebase", reason: autoMergeReasonMergeRefusedReview, labels: []string{"needs-rebase"}, want: stagelabels.Fixing}, {name: "merge queue required", reason: autoMergeReasonMergeQueueRequired, want: stagelabels.MergeQueue}, + {name: "native queued position", reason: "native-merge-queued: queued (position 2, AWAITING_CHECKS)", want: stagelabels.MergeQueue}, + {name: "native enqueue failure", reason: "native-merge-queue-enqueue", want: stagelabels.MergeQueue}, {name: "unclassified merge failure", reason: "merge-failed", want: stagelabels.MergeQueue}, } { t.Run(tc.name, func(t *testing.T) { From 2fb208074e58c78514c8b017dcf321e103f8129b Mon Sep 17 00:00:00 2001 From: architect <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Thu, 29 Oct 2026 01:22:36 -0400 Subject: [PATCH 2/8] =?UTF-8?q?=F0=9F=90=9B=20fix:=20throttle=20native=20m?= =?UTF-8?q?erge=20queue=20reconciliation=20on=20paused=20repos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bound per-sweep API fan-out by re-reading a paused repo's open PRs at most once per interval. Hive-Fix-Attempt: 1/6 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: architect <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- .../automerge/automerge_native_queue.go | 22 +++++++++++++++++++ .../automerge/automerge_native_queue_test.go | 7 ++++++ src/pkg/github/automerge/automerge_sweep.go | 3 +++ 3 files changed, 32 insertions(+) diff --git a/src/pkg/github/automerge/automerge_native_queue.go b/src/pkg/github/automerge/automerge_native_queue.go index d5b3c142e4..05eaf68ca9 100644 --- a/src/pkg/github/automerge/automerge_native_queue.go +++ b/src/pkg/github/automerge/automerge_native_queue.go @@ -5,6 +5,7 @@ import ( "fmt" "strconv" "strings" + "time" gh "github.com/google/go-github/v72/github" "github.com/hivecommons/hive/pkg/effects" @@ -13,6 +14,10 @@ import ( const nativeQueueReason = "native-merge-queued" +// inactiveQueueInterval bounds how often a paused repo's open PRs are re-read; +// each read costs GraphQL points per PR. +const inactiveQueueInterval = 10 * time.Minute + type nativeQueueTransport interface { NativeMergeQueueAvailable() bool ReadNativeMergeQueue(context.Context, string) (hgithub.NativeMergeQueueState, error) @@ -172,6 +177,9 @@ func (c *Engine) reconcileInactiveNativeQueues(ctx context.Context) { if (active[display] && c.repoAutoMergeAllowed(display)) || (c.repoOnGitHub != nil && !c.repoOnGitHub(display)) { continue } + if !c.inactiveQueueDue(display) { + continue + } owner, repo := c.transport.SplitRepo(display) opts := &gh.PullRequestListOptions{State: "open", ListOptions: gh.ListOptions{PerPage: 100}} for { @@ -190,3 +198,17 @@ func (c *Engine) reconcileInactiveNativeQueues(ctx context.Context) { } } } + +func (c *Engine) inactiveQueueDue(repo string) bool { + c.inactiveQueueMu.Lock() + defer c.inactiveQueueMu.Unlock() + now := c.now() + if last, ok := c.inactiveQueueLast[repo]; ok && now.Sub(last) < inactiveQueueInterval { + return false + } + if c.inactiveQueueLast == nil { + c.inactiveQueueLast = map[string]time.Time{} + } + c.inactiveQueueLast[repo] = now + return true +} diff --git a/src/pkg/github/automerge/automerge_native_queue_test.go b/src/pkg/github/automerge/automerge_native_queue_test.go index 6033dae0be..780c70b8bc 100644 --- a/src/pkg/github/automerge/automerge_native_queue_test.go +++ b/src/pkg/github/automerge/automerge_native_queue_test.go @@ -288,3 +288,10 @@ func TestNativeQueueNewCycleAfterRemoval(t *testing.T) { t.Fatalf("enqueues=%d claims=%+v", f.enqueues, b.claims) } } + +func TestInactiveQueueReconcileIsThrottled(t *testing.T) { + c := &Engine{now: time.Now} + if !c.inactiveQueueDue("o/r") || c.inactiveQueueDue("o/r") || !c.inactiveQueueDue("o/other") { + t.Fatal("expected first call due, immediate repeat throttled, other repo due") + } +} diff --git a/src/pkg/github/automerge/automerge_sweep.go b/src/pkg/github/automerge/automerge_sweep.go index 7a449baa03..2d3f804e92 100644 --- a/src/pkg/github/automerge/automerge_sweep.go +++ b/src/pkg/github/automerge/automerge_sweep.go @@ -239,6 +239,9 @@ type Engine struct { gh *gh.Client logger *slog.Logger + inactiveQueueMu sync.Mutex + inactiveQueueLast map[string]time.Time + selfSkipWarnedMu sync.Mutex selfSkipWarned map[string]struct{} From 07557aec8e0b64a2f56383ef36a47976eebd67a9 Mon Sep 17 00:00:00 2001 From: architect <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Thu, 29 Oct 2026 02:37:07 -0400 Subject: [PATCH 3/8] =?UTF-8?q?=F0=9F=90=9B=20fix:=20match=20native=20queu?= =?UTF-8?q?e=20enqueuer=20by=20bot=20slug=20and=20share=20queue=20reasons?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hive-Fix-Attempt: 2/6 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: architect <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- src/docs/documentation-map.md | 1 + .../stage-labels.md | 2 +- .../automerge/automerge_native_queue.go | 46 ++++++++++++------- .../automerge/automerge_native_queue_test.go | 3 +- src/pkg/github/automerge/automerge_sweep.go | 4 +- src/pkg/stageengine/derive_pr_classify.go | 2 +- src/pkg/stageengine/prstage/reasons.go | 2 + 7 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/docs/documentation-map.md b/src/docs/documentation-map.md index 96fd74ab5a..b2addf2d82 100644 --- a/src/docs/documentation-map.md +++ b/src/docs/documentation-map.md @@ -96,6 +96,7 @@ Documentation for the current Hive line (branch `v5`; the code and docs live und ### Agent writes and network reference - [`hive-open-pr`](hive-open-pr.md) — how agents open pull requests as the App bot instead of via `gh pr create`: the flags, the UID-ownership anchor that makes the request forge-resistant, and the asynchronous contract (exit `0` means requested, not opened). - [`hive-merge`](hive-merge.md) — how agents merge pull requests as the App bot instead of the GitHub MCP `merge_pull_request` tool: the flags, the F4 target-binding (pinned head SHA + governor merge-eligible list), and the retry/re-engagement behavior when required checks are still red. +- [Native merge queue](native-merge-queue.md) — how the auto-merge lanes enqueue eligible PRs into GitHub's native merge queue (App auth only), the `native-merge-queued`/`native-merge-queue-*` stage reasons, and how Hive withdraws only its own entries when a gate breaks. - [`hive-open-issue`](hive-open-issue.md) — **Agent/developer reference:** request issue creation, comments, or claims through the App bot, with deduplication and retries. - [Review-bot threads on hive-mediated PRs](review-bot-threads.md) — route bot feedback back to the authoring agent while protecting human review threads. - [Network and port requirements](network-requirements.md) — inbound ports, proxy paths, egress, and firewall guidance. diff --git a/src/docs/labels-and-control-signals/stage-labels.md b/src/docs/labels-and-control-signals/stage-labels.md index 3dfe08df09..f9c9952435 100644 --- a/src/docs/labels-and-control-signals/stage-labels.md +++ b/src/docs/labels-and-control-signals/stage-labels.md @@ -365,7 +365,7 @@ used to admit the PR: | a trusted maintainer answered that question on the same head — `/hive decision ` or a CHANGES_REQUESTED review (recorded in `review-human-change-requests.json`, head-scoped) | `070-fixing`, reason `contributor-lane:decision-answered`: the answer is folded into the reviewer verdict for that head on every tick and after a restart, so the stage never re-derives 085 from the stale `requires_human` verdict. A bare `/hive decision approve` instead answers that `requires_human` question; it does not remove a `hold` label or provide the broad GitHub-review override for `changes_requested`/`reject`. A new head draws a new verdict with no answer and asks again ([#14679](https://github.com/hivecommons/hive/issues/14679)) | | CI red (`check-failing:*`, `required-check-failing:*`, `status-*`), `conflicting`, `not-mergeable`, `changes-requested`, verdict changes_requested/reject, `needs-rebase` present | `070-fixing` (self-authored lane or `author/hive`) / `071-needs-author` (`author/human`) / `072-returned-to-contributor` (`author/clanker`) — see [Stage 07x by author kind](#stage-07x-by-author-kind). A head whose App-linked commits carry a `Signed-off-by` the squash would misattribute to the PR author (`hive-signoff-attribution`) follows the same split: the branch owner is the only one who can rewrite them. | | `contributor-lane:soaking(...)` — and no red check on the head | `095-pre-merge-soak` | -| `head-changed-since-eval`, `merge-failed`, `merge-queue-required` (the base branch's ruleset only accepts merges through GitHub's merge queue, so the direct REST merge is refused; logged as a skip rather than a merge error, [#14018](https://github.com/hivecommons/hive/issues/14018); the self-authored sweep then skips the direct merge on that base for 30 minutes and logs the skip once per PR head, [#14068](https://github.com/hivecommons/hive/issues/14068); the trusted-author, queued and contributor-lane sweeps skip it the same way), `required-checks-expected`, `behind`, `updated-branch`, `behind-update-budget-exhausted` (a behind self-authored PR the sweep did not update only because this tick's branch-update budget was spent; retried next tick, waits on the hive, [#12607](https://github.com/hivecommons/hive/issues/12607)), `release-hold*` — and no red check on the head | `100-merge-queue` | +| `head-changed-since-eval`, `merge-failed`, `merge-queue-required` (the base branch's ruleset only accepts merges through GitHub's merge queue and the transport is a static token that cannot enqueue, so the direct REST merge is refused; with App auth these bases are enqueued instead, see [native-merge-queue.md](../native-merge-queue.md); logged as a skip rather than a merge error, [#14018](https://github.com/hivecommons/hive/issues/14018); the self-authored sweep then skips the direct merge on that base for 30 minutes and logs the skip once per PR head, [#14068](https://github.com/hivecommons/hive/issues/14068); the trusted-author, queued and contributor-lane sweeps skip it the same way), `required-checks-expected`, `behind`, `updated-branch`, `behind-update-budget-exhausted` (a behind self-authored PR the sweep did not update only because this tick's branch-update budget was spent; retried next tick, waits on the hive, [#12607](https://github.com/hivecommons/hive/issues/12607)), `release-hold*`, `native-merge-queued: queued (position N, STATE)` (the PR was enqueued into GitHub's native merge queue) and the failures `native-merge-queue-read`, `native-merge-queue-enqueue`, `native-merge-queue-method` — and no red check on the head | `100-merge-queue` | | CI red (required or optional check) while the PR is deferred to an open `[shared-ci]` incident (``, the governor's `deferred_incident`) for the same head | `050-review-pending` / `060-reviewed`, reason `shared-ci-incident:`: the base or a shared check is what is red, so the PR is never sent to 070/071/072, never nudged and never stalled; once the incident closes a still-red head is handed back as usual. Conflicts and requested changes stay author-facing ([#12606](https://github.com/hivecommons/hive/issues/12606)) | | CI red (required or optional) while **every** failing check on the head is also red on the base branch's tip — the governor's `base_red_checks` / `base_red`, set during CI enrichment when a same-named latest check run on the base ref (meta checks excluded) concluded failure / timed_out / cancelled / action_required / startup_failure / stale **and** the PR's changed files include none of `.github/workflows/*`, `Dockerfile` / `Dockerfile.*` / `*.Dockerfile`, `go.mod` / `go.sum` (a PR touching those plausibly owns the check and stays author-facing; a lookup error or a diff beyond the file-list page budget also stays author-facing) | `050-review-pending` / `060-reviewed`, reason `base-branch-red:,` ("waiting for the base branch's CI to recover (…) — not this PR's fault"): never 070/071/072, never nudged, never stalled, still not merged (the lane's skip reason is the same token). The fix loop counts no attempt, kicks no fixer, and posts one `` hold comment per head; an escalation the hive itself raised (ledger `label_applied`) over such a head has its `needs-human` removed with the hold wording. The next evaluation after the base goes green re-evaluates the head: a still-red PR is handed back as usual. Conflicts and requested changes stay author-facing (seen on [#13270](https://github.com/hivecommons/hive/pull/13270) and [#13258](https://github.com/hivecommons/hive/pull/13258), 2026-10-20) | | CI red (required or optional) on a **fork** PR while **every** failing check's annotation says the check refuses forks (case-insensitive `fork pull request(s) cannot`, `maintainer must push this branch`, or `cannot publish` together with `fork` — a fork-publication / fork-secrets guard) | `085-needs-decision`, reason `fork-guard:,`: never 070/071/072, never nudged, never adopted (074) — neither the author nor the hive can turn the check green by pushing to the fork. The entry comment names the check and says a maintainer must push the branch to the repository (or approve the run as the repository requires). A same-repo PR with the same message, or a fork PR with any other red check, is handed back as usual ([#14273](https://github.com/hivecommons/hive/issues/14273)) | diff --git a/src/pkg/github/automerge/automerge_native_queue.go b/src/pkg/github/automerge/automerge_native_queue.go index 05eaf68ca9..aa6eeae5c3 100644 --- a/src/pkg/github/automerge/automerge_native_queue.go +++ b/src/pkg/github/automerge/automerge_native_queue.go @@ -10,9 +10,20 @@ import ( gh "github.com/google/go-github/v72/github" "github.com/hivecommons/hive/pkg/effects" hgithub "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/stageengine/prstage" ) -const nativeQueueReason = "native-merge-queued" +const ( + nativeQueueReason = prstage.AutoMergeReasonNativeQueued + nativeQueueReasonRead = prstage.AutoMergeReasonNativeQueuePrefix + "-read" + nativeQueueReasonEnqueue = prstage.AutoMergeReasonNativeQueuePrefix + "-enqueue" + nativeQueueReasonMethod = prstage.AutoMergeReasonNativeQueuePrefix + "-method" + + // Lanes passed to reconcileNativeQueueGate by callers outside the three + // authoring lanes. + nativeQueueLaneQueued = "queued" + nativeQueueLaneInactive = "inactive" +) // inactiveQueueInterval bounds how often a paused repo's open PRs are re-read; // each read costs GraphQL points per PR. @@ -23,6 +34,13 @@ type nativeQueueTransport interface { ReadNativeMergeQueue(context.Context, string) (hgithub.NativeMergeQueueState, error) EnqueueNativePullRequest(context.Context, string, string) (*hgithub.NativeMergeQueueEntry, error) DequeueNativePullRequest(context.Context, string) error + NativeMergeQueueMethod(context.Context, string, string) (string, error) +} + +// botSlug normalises a bot login: REST reports "[bot]" while GraphQL's +// Bot.login omits the suffix. +func botSlug(login string) string { + return strings.TrimSuffix(strings.TrimSpace(login), "[bot]") } func (c *Engine) nativeQueue() nativeQueueTransport { @@ -42,7 +60,7 @@ func (c *Engine) tryNativeQueue(ctx context.Context, pr *gh.PullRequest, claim e } state, err := t.ReadNativeMergeQueue(ctx, pr.GetNodeID()) if err != nil { - return "native-merge-queue-read", err + return nativeQueueReasonRead, err } if !state.IsMergeQueueEnabled && !state.IsInMergeQueue { return "", nil @@ -53,18 +71,12 @@ func (c *Engine) tryNativeQueue(ctx context.Context, pr *gh.PullRequest, claim e // A queue chooses its own merge method. Preserve the attribution gate // that elected a merge commit to retain each author's signed commits. if claim.Inputs["method_reason"] == mergeMethodReasonMixedAuthorship { - mt, ok := c.transport.(interface { - NativeMergeQueueMethod(context.Context, string, string) (string, error) - }) - if !ok { - return "native-merge-queue-method", fmt.Errorf("queue merge method cannot be verified") - } - method, err := mt.NativeMergeQueueMethod(ctx, state.ID, pr.GetBase().GetRef()) + method, err := t.NativeMergeQueueMethod(ctx, state.ID, pr.GetBase().GetRef()) if err != nil { - return "native-merge-queue-method", err + return nativeQueueReasonMethod, err } if !strings.EqualFold(method, "MERGE") { - return "native-merge-queue-method", fmt.Errorf("mixed authorship requires a merge commit; queue uses %s", method) + return nativeQueueReasonMethod, fmt.Errorf("mixed authorship requires a merge commit; queue uses %s", method) } } if !state.IsInMergeQueue { @@ -95,7 +107,7 @@ func (c *Engine) tryNativeQueue(ctx context.Context, pr *gh.PullRequest, claim e return effects.Result{Provenance: entry.ID}, nil }) if err != nil { - return "native-merge-queue-enqueue", err + return nativeQueueReasonEnqueue, err } } if e := state.MergeQueueEntry; e != nil { @@ -118,7 +130,7 @@ func (c *Engine) reconcileNativeQueueGate(ctx context.Context, lane, owner, repo return } author := hgithub.SafeGetLogin(pr.GetUser()) - if _, own := c.sweepLaneForAuthor(author); own && lane != stageLaneSelf && lane != "inactive" { + if _, own := c.sweepLaneForAuthor(author); own && lane != stageLaneSelf && lane != nativeQueueLaneInactive { return } if lane == stageLaneTrusted && reason == "untrusted-author-role" { @@ -127,12 +139,12 @@ func (c *Engine) reconcileNativeQueueGate(ctx context.Context, lane, owner, repo if lane == stageLaneContributor && c.trustedAuthorDecision(author, c.currentTrustedAuthorPolicy().RequireRole).Allowed { return } - login := strings.TrimSpace(c.transport.AppBotLogin()) + login := botSlug(c.transport.AppBotLogin()) if login == "" { return } state, err := t.ReadNativeMergeQueue(ctx, pr.GetNodeID()) - if err == nil && state.IsInMergeQueue && state.MergeQueueEntry != nil && strings.EqualFold(state.MergeQueueEntry.Enqueuer.Login, login) { + if err == nil && state.IsInMergeQueue && state.MergeQueueEntry != nil && strings.EqualFold(botSlug(state.MergeQueueEntry.Enqueuer.Login), login) { entryID := state.MergeQueueEntry.ID claim := effects.Claim{Repo: owner + "/" + repo, Kind: effects.KindPullRequestMerge, Target: strconv.Itoa(pr.GetNumber()), Actor: "automerge", Inputs: map[string]string{"operation": "dequeue", "entry": entryID}} observe := func(ctx context.Context) (effects.ExternalState, error) { @@ -149,7 +161,7 @@ func (c *Engine) reconcileNativeQueueGate(ctx context.Context, lane, owner, repo if err != nil { return effects.Result{}, err } - if !current.IsInMergeQueue || current.MergeQueueEntry == nil || current.MergeQueueEntry.ID != entryID || !strings.EqualFold(current.MergeQueueEntry.Enqueuer.Login, login) { + if !current.IsInMergeQueue || current.MergeQueueEntry == nil || current.MergeQueueEntry.ID != entryID || !strings.EqualFold(botSlug(current.MergeQueueEntry.Enqueuer.Login), login) { return effects.Result{}, nil } if err := t.DequeueNativePullRequest(ctx, state.ID); err != nil { @@ -189,7 +201,7 @@ func (c *Engine) reconcileInactiveNativeQueues(ctx context.Context) { break } for _, pr := range prs { - c.reconcileNativeQueueGate(ctx, "inactive", owner, repo, pr, "repo-paused-or-disabled") + c.reconcileNativeQueueGate(ctx, nativeQueueLaneInactive, owner, repo, pr, "repo-paused-or-disabled") } if resp == nil || resp.NextPage == 0 { break diff --git a/src/pkg/github/automerge/automerge_native_queue_test.go b/src/pkg/github/automerge/automerge_native_queue_test.go index 780c70b8bc..22b05d3ae4 100644 --- a/src/pkg/github/automerge/automerge_native_queue_test.go +++ b/src/pkg/github/automerge/automerge_native_queue_test.go @@ -42,7 +42,8 @@ func (f *nativeQueueFake) DequeueNativePullRequest(context.Context, string) erro } func queueEntry(login string) *hgithub.NativeMergeQueueEntry { e := &hgithub.NativeMergeQueueEntry{ID: "entry1", Position: 2, State: "AWAITING_CHECKS"} - e.Enqueuer.Login = login + // GraphQL Bot.login omits the REST "[bot]" suffix. + e.Enqueuer.Login = strings.TrimSuffix(login, "[bot]") return e } func nativeQueueEngine() (*Engine, *nativeQueueFake, *gh.PullRequest, effects.Claim) { diff --git a/src/pkg/github/automerge/automerge_sweep.go b/src/pkg/github/automerge/automerge_sweep.go index 2d3f804e92..95b228911e 100644 --- a/src/pkg/github/automerge/automerge_sweep.go +++ b/src/pkg/github/automerge/automerge_sweep.go @@ -1004,7 +1004,7 @@ func (c *Engine) SweepQueuedAutoMerges(ctx context.Context, opts AutoMergeSweepO if reason := c.prefilterQueuedIssue(issue, label); reason != "" { if c.nativeQueue() != nil { if pr, _, err := c.gh.PullRequests.Get(ctx, owner, repoName, issue.GetNumber()); err == nil { - c.reconcileNativeQueueGate(ctx, "queued", owner, repoName, pr, reason) + c.reconcileNativeQueueGate(ctx, nativeQueueLaneQueued, owner, repoName, pr, reason) } } if reason == "held" || reason == "exempt-label" { @@ -2697,7 +2697,7 @@ func (c *Engine) trySweepQueuedPR(ctx context.Context, displayRepo, owner, repo } return AutoMergeSweepEvent{}, "fetch-pr", err } - defer func() { c.reconcileNativeQueueGate(ctx, "queued", owner, repo, pr, reasonOut) }() + defer func() { c.reconcileNativeQueueGate(ctx, nativeQueueLaneQueued, owner, repo, pr, reasonOut) }() c.noteStagePR(owner, repo, number, pr) if !strings.EqualFold(pr.GetState(), "open") { return AutoMergeSweepEvent{}, "closed", nil diff --git a/src/pkg/stageengine/derive_pr_classify.go b/src/pkg/stageengine/derive_pr_classify.go index 934b6145a7..07f0714b51 100644 --- a/src/pkg/stageengine/derive_pr_classify.go +++ b/src/pkg/stageengine/derive_pr_classify.go @@ -490,7 +490,7 @@ func ClassifyStage(o stageObservation) stageVerdict { // 095, not 100 — the merge has not been attempted yet. return WithHumanApprovalReason(stageVerdict{Stage: stagelabels.PreMergeSoak, Rank: stageRankOwned}, humanApprovalReason) } - if stageMergeQueueReasons[reason] || strings.HasPrefix(reason, "native-merge-queue") || strings.HasPrefix(reason, "release-hold") { + if stageMergeQueueReasons[reason] || strings.HasPrefix(reason, prstage.AutoMergeReasonNativeQueuePrefix) || strings.HasPrefix(reason, "release-hold") { return WithHumanApprovalReason(stageVerdict{Stage: stagelabels.MergeQueue, Rank: stageRankOwned}, humanApprovalReason) } if reason == contributorLaneReasonNoVerdict { diff --git a/src/pkg/stageengine/prstage/reasons.go b/src/pkg/stageengine/prstage/reasons.go index 655b8674bb..3739a27fb6 100644 --- a/src/pkg/stageengine/prstage/reasons.go +++ b/src/pkg/stageengine/prstage/reasons.go @@ -31,6 +31,8 @@ const ( AutoMergeReasonMergeQueueRequired = "merge-queue-required" AutoMergeReasonMergeRefusedMethod = "merge-refused:method-not-allowed" AutoMergeReasonMergeRefusedReview = "merge-refused:review-required" + AutoMergeReasonNativeQueuePrefix = "native-merge-queue" + AutoMergeReasonNativeQueued = "native-merge-queued" AutoMergeReasonNoAppBotLogin = "no-app-bot-login" AutoMergeReasonReviewGatePrefix = "review-gate:" ContributorLaneReasonAdoptedByHive = "contributor-lane:adopted-by-hive" From 98301ecd1dcd5fdac27f8c89abff4a171a0c4a0a Mon Sep 17 00:00:00 2001 From: architect <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Thu, 29 Oct 2026 02:54:10 -0400 Subject: [PATCH 4/8] =?UTF-8?q?=F0=9F=90=9B=20fix:=20keep=20own=20native?= =?UTF-8?q?=20queue=20entry=20on=20queue=20error=20reasons?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Guard reconcileNativeQueueGate with the shared native-merge-queue prefix so read/enqueue/method error reasons do not dequeue Hive's own entry. Hive-Fix-Attempt: 3/6 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: architect <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- .../automerge/automerge_native_queue.go | 2 +- .../automerge/automerge_native_queue_test.go | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/pkg/github/automerge/automerge_native_queue.go b/src/pkg/github/automerge/automerge_native_queue.go index aa6eeae5c3..dc6369bf17 100644 --- a/src/pkg/github/automerge/automerge_native_queue.go +++ b/src/pkg/github/automerge/automerge_native_queue.go @@ -121,7 +121,7 @@ func (c *Engine) tryNativeQueue(ctx context.Context, pr *gh.PullRequest, claim e // including cheap prefilter holds. Never withdraw another actor's entry. func (c *Engine) reconcileNativeQueueGate(ctx context.Context, lane, owner, repo string, pr *gh.PullRequest, reason string) { t := c.nativeQueue() - if t == nil || pr == nil || pr.GetNodeID() == "" || reason == "" || strings.HasPrefix(reason, nativeQueueReason) { + if t == nil || pr == nil || pr.GetNodeID() == "" || reason == "" || strings.HasPrefix(reason, prstage.AutoMergeReasonNativeQueuePrefix) { return } // Routing observations are not eligibility decisions by the owning lane. diff --git a/src/pkg/github/automerge/automerge_native_queue_test.go b/src/pkg/github/automerge/automerge_native_queue_test.go index 22b05d3ae4..6ab8ed2c1c 100644 --- a/src/pkg/github/automerge/automerge_native_queue_test.go +++ b/src/pkg/github/automerge/automerge_native_queue_test.go @@ -296,3 +296,32 @@ func TestInactiveQueueReconcileIsThrottled(t *testing.T) { t.Fatal("expected first call due, immediate repeat throttled, other repo due") } } + +func TestNativeQueueErrorReasonsPreserveOwnEntry(t *testing.T) { + for _, reason := range []string{nativeQueueReasonRead, nativeQueueReasonEnqueue, nativeQueueReasonMethod} { + t.Run(reason, func(t *testing.T) { + c, f, pr, _ := nativeQueueEngine() + f.state.IsInMergeQueue = true + f.state.MergeQueueEntry = queueEntry(testHiveAppBotLogin) + c.reconcileNativeQueueGate(context.Background(), stageLaneSelf, "acme", "widget", pr, reason) + if f.dequeues != 0 { + t.Fatalf("dequeues=%d, error reason must not withdraw own entry", f.dequeues) + } + }) + } +} + +func TestSelfAuthoredSweepReadErrorKeepsQueuedEntry(t *testing.T) { + var merged []int + api := newAutoMergeSweepAPI(t, "", []sweepPR{{number: 7, nodeID: "PR_7", author: testHiveAppBotLogin, mergeableState: "clean", statusState: "success", checkStatus: "completed", checkConclusion: "success"}}, &merged) + defer api.Close() + c := newAutoMergeSweepClient(api.URL) + f := &nativeQueueFake{Transport: c.transport, state: hgithub.NativeMergeQueueState{ID: "PR_7", HeadRefOid: "sha7", IsMergeQueueEnabled: true, IsInMergeQueue: true, MergeQueueEntry: queueEntry(testHiveAppBotLogin)}, readErr: errors.New("graphql 502")} + c.transport = f + if _, err := c.SweepSelfAuthoredAutoMerges(context.Background(), AutoMergeSweepOptions{}); err != nil { + t.Fatal(err) + } + if f.dequeues != 0 { + t.Fatalf("dequeues=%d, transient read error must not withdraw own entry", f.dequeues) + } +} From 7ff820a5f6d2a75c496dc6c988711591685957d9 Mon Sep 17 00:00:00 2001 From: architect <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Thu, 29 Oct 2026 03:09:50 -0400 Subject: [PATCH 5/8] =?UTF-8?q?=F0=9F=93=96=20docs:=20list=20real=20native?= =?UTF-8?q?=20queue=20withdrawal=20triggers=20and=20method-failure=20routi?= =?UTF-8?q?ng?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hive-Fix-Attempt: 4/6 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: architect <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- src/docs/native-merge-queue.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/docs/native-merge-queue.md b/src/docs/native-merge-queue.md index 35a566d0f2..76b140dba8 100644 --- a/src/docs/native-merge-queue.md +++ b/src/docs/native-merge-queue.md @@ -20,9 +20,13 @@ Already queued PRs are left in place. The sweep reason reports `stage/100-merge-queue`; enqueueing does not emit a completed-merge event or close an issue. The stage observation carries the reason to the dashboard. -When a lane rejects an entry after a hold, withdrawn approval, changes request, -or stale approval, Hive rechecks the entry's enqueuer and withdraws it only if -it belongs to this App. Entries from a person or another App are preserved. +When the owning lane rejects a PR for any reason other than its own +native-queue outcomes or author-routing skips (for example a hold, withdrawn +approval, changes request, stale approval, or a transient refusal such as +`not-mergeable`, `required-checks-expected`, `behind`, soak or `release-hold*`), +Hive rechecks the entry's enqueuer and withdraws it only if it belongs to this +App. A transient refusal therefore dequeues the entry, and a later sweep +re-enqueues it once the gates pass again. Entries from a person or another App are preserved. The queued sweep also withdraws this App's entries on paused repositories or repositories with automerge disabled. Every enqueue and dequeue uses the existing mutation boundary, including its write denials. Reconciliation is @@ -31,6 +35,9 @@ periodic/event-driven, so a gate change can race GitHub's eventual merge. Queue rules choose the merge method. Where Hive elected a merge commit to preserve mixed authorship and DCO attribution, the queue must use `MERGE`; otherwise Hive reports a queue-method failure rather than enqueueing. +This failure is permanent and is not routed to a human: the PR stays at +`stage/100-merge-queue` until an operator changes the queue's merge method to +`MERGE` (or the authorship no longer requires a merge commit). Required workflows must subscribe to `merge_group` in addition to their PR triggers. Workflow-trigger diagnosis is not automatic in this implementation. From 071f33cf5199d86dfe28c45e5d8f68c6db5de12a Mon Sep 17 00:00:00 2001 From: "hivecommons-hive[bot]" <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Thu, 29 Oct 2026 11:43:40 +0000 Subject: [PATCH 6/8] =?UTF-8?q?=F0=9F=90=9B=20fix:=20park=20mixed-authorsh?= =?UTF-8?q?ip=20PRs=20on=20a=20SQUASH=20native=20queue=20at=20stage/085=20?= =?UTF-8?q?(#15380)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: park mixed-authorship PRs on a SQUASH native queue at stage/085 Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Co-authored-by: Danathar <6772335+Danathar@users.noreply.github.com> * 🐛 fix: report queue method read failures as native-merge-queue-read A transient failure reading the native queue's merge method was reported as native-merge-queue-method, which the stage engine derives to stage/085-needs-decision with the merge-method explanation. Return the read reason instead so the PR stays at stage/100-merge-queue, and state that in the docs. Hive-Fix-Attempt: 1/6 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --------- Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Co-authored-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Co-authored-by: Danathar <6772335+Danathar@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../fixed-native-queue-method-refusal.md | 1 + src/docs/native-merge-queue.md | 13 +++-- .../automerge/automerge_merge_refusal.go | 50 +++++++++++++++++ .../automerge/automerge_native_queue.go | 10 +++- .../automerge/automerge_native_queue_test.go | 55 +++++++++++++++++++ src/pkg/stageengine/derive_pr_classify.go | 5 ++ .../derive_pr_merge_refusal_test.go | 2 + src/pkg/stageengine/prstage/reasons.go | 3 +- src/pkg/stageengine/prstage/reasons_test.go | 1 + 9 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 changelog.d/fixed-native-queue-method-refusal.md diff --git a/changelog.d/fixed-native-queue-method-refusal.md b/changelog.d/fixed-native-queue-method-refusal.md new file mode 100644 index 0000000000..cbd5b60ff3 --- /dev/null +++ b/changelog.d/fixed-native-queue-method-refusal.md @@ -0,0 +1 @@ +- A mixed-authorship PR whose base uses a native merge queue set to `SQUASH` no longer sits at `stage/100-merge-queue` forever: the `native-merge-queue-method` refusal now parks it at `stage/085-needs-decision` with the `merge-method` explanation, like a direct merge refused for a disabled merge method, and the refusal is remembered per head for 30 minutes instead of re-reading the queue's method every sweep ([#15377](https://github.com/hivecommons/hive/issues/15377)). diff --git a/src/docs/native-merge-queue.md b/src/docs/native-merge-queue.md index 76b140dba8..465b7acec1 100644 --- a/src/docs/native-merge-queue.md +++ b/src/docs/native-merge-queue.md @@ -34,10 +34,15 @@ periodic/event-driven, so a gate change can race GitHub's eventual merge. Queue rules choose the merge method. Where Hive elected a merge commit to preserve mixed authorship and DCO attribution, the queue must use `MERGE`; -otherwise Hive reports a queue-method failure rather than enqueueing. -This failure is permanent and is not routed to a human: the PR stays at -`stage/100-merge-queue` until an operator changes the queue's merge method to -`MERGE` (or the authorship no longer requires a merge commit). +otherwise Hive reports `native-merge-queue-method` rather than enqueueing. +Like a direct merge GitHub refuses because the merge method is disabled, the +PR parks at `stage/085-needs-decision` for a person, with the `merge-method` +explanation: change the queue's merge method to `MERGE`, or merge it by hand. +The refusal is remembered for 30 minutes per head, so the queue's method is +not re-read every sweep; a new head or the recheck window reads it again. +Read and enqueue failures (`native-merge-queue-read`, `-enqueue`) stay at +`stage/100-merge-queue`; a failure to read the queue's merge method is a +read failure too, not a refusal, and is reported as `native-merge-queue-read`. Required workflows must subscribe to `merge_group` in addition to their PR triggers. Workflow-trigger diagnosis is not automatic in this implementation. diff --git a/src/pkg/github/automerge/automerge_merge_refusal.go b/src/pkg/github/automerge/automerge_merge_refusal.go index 4ca3bb932a..3ed841df34 100644 --- a/src/pkg/github/automerge/automerge_merge_refusal.go +++ b/src/pkg/github/automerge/automerge_merge_refusal.go @@ -10,6 +10,7 @@ import ( gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/effects" "github.com/hivecommons/hive/pkg/stageengine/prstage" ) @@ -244,3 +245,52 @@ func (c *Engine) rememberMergeRefusal(key string, r mergeRefusal) { } c.mergeRefusals[key] = r } + +func nativeQueueMethodRefusalKey(claim effects.Claim) string { + return "queue-method|" + strings.ToLower(claim.Repo) + "#" + claim.Target +} + +// knownNativeQueueMethodRefusal reports whether this PR's head was already +// refused by a queue whose merge method cannot keep mixed authorship, so the +// sweep does not re-read the queue's method every pass. Like a remembered +// merge-method 405 it re-arms after mergeRefusalRecheck, and on a new head. +func (c *Engine) knownNativeQueueMethodRefusal(claim effects.Claim, head string) bool { + if c == nil { + return false + } + key := nativeQueueMethodRefusalKey(claim) + now := c.clock() + c.mergeRefusalsMu.Lock() + defer c.mergeRefusalsMu.Unlock() + r, ok := c.mergeRefusals[key] + if !ok { + return false + } + if now.Sub(r.at) >= mergeRefusalRecheck || !strings.EqualFold(r.head, strings.TrimSpace(head)) { + delete(c.mergeRefusals, key) + return false + } + return true +} + +// noteNativeQueueMethodRefusal remembers a queue-method refusal for +// knownNativeQueueMethodRefusal. +func (c *Engine) noteNativeQueueMethodRefusal(claim effects.Claim, head string) { + if c == nil { + return + } + r := mergeRefusal{head: strings.TrimSpace(head), at: c.clock()} + c.mergeRefusalsMu.Lock() + defer c.mergeRefusalsMu.Unlock() + if c.mergeRefusals == nil { + c.mergeRefusals = make(map[string]mergeRefusal) + } + if len(c.mergeRefusals) >= mergeRefusalPruneAt { + for k, old := range c.mergeRefusals { + if r.at.Sub(old.at) >= mergeRefusalRecheck { + delete(c.mergeRefusals, k) + } + } + } + c.mergeRefusals[nativeQueueMethodRefusalKey(claim)] = r +} diff --git a/src/pkg/github/automerge/automerge_native_queue.go b/src/pkg/github/automerge/automerge_native_queue.go index dc6369bf17..2be156d6cf 100644 --- a/src/pkg/github/automerge/automerge_native_queue.go +++ b/src/pkg/github/automerge/automerge_native_queue.go @@ -17,7 +17,7 @@ const ( nativeQueueReason = prstage.AutoMergeReasonNativeQueued nativeQueueReasonRead = prstage.AutoMergeReasonNativeQueuePrefix + "-read" nativeQueueReasonEnqueue = prstage.AutoMergeReasonNativeQueuePrefix + "-enqueue" - nativeQueueReasonMethod = prstage.AutoMergeReasonNativeQueuePrefix + "-method" + nativeQueueReasonMethod = prstage.AutoMergeReasonNativeQueueMethod // Lanes passed to reconcileNativeQueueGate by callers outside the three // authoring lanes. @@ -71,11 +71,17 @@ func (c *Engine) tryNativeQueue(ctx context.Context, pr *gh.PullRequest, claim e // A queue chooses its own merge method. Preserve the attribution gate // that elected a merge commit to retain each author's signed commits. if claim.Inputs["method_reason"] == mergeMethodReasonMixedAuthorship { + if c.knownNativeQueueMethodRefusal(claim, state.HeadRefOid) { + return nativeQueueReasonMethod, nil + } method, err := t.NativeMergeQueueMethod(ctx, state.ID, pr.GetBase().GetRef()) if err != nil { - return nativeQueueReasonMethod, err + // A transient read failure is not a merge-method refusal; it + // must not park the PR at stage/085. + return nativeQueueReasonRead, err } if !strings.EqualFold(method, "MERGE") { + c.noteNativeQueueMethodRefusal(claim, state.HeadRefOid) return nativeQueueReasonMethod, fmt.Errorf("mixed authorship requires a merge commit; queue uses %s", method) } } diff --git a/src/pkg/github/automerge/automerge_native_queue_test.go b/src/pkg/github/automerge/automerge_native_queue_test.go index 6ab8ed2c1c..6634c9fb41 100644 --- a/src/pkg/github/automerge/automerge_native_queue_test.go +++ b/src/pkg/github/automerge/automerge_native_queue_test.go @@ -16,7 +16,9 @@ type nativeQueueFake struct { Transport state hgithub.NativeMergeQueueState readErr, enqueueErr error + methodErr error enqueues, dequeues int + methodReads int expectedHead string } @@ -244,9 +246,30 @@ func TestNativeQueuePauseWithdrawsOwnEntry(t *testing.T) { } func (f *nativeQueueFake) NativeMergeQueueMethod(context.Context, string, string) (string, error) { + f.methodReads++ + if f.methodErr != nil { + return "", f.methodErr + } return "SQUASH", nil } +// A failure to read the queue's merge method is a read failure, not a +// merge-method refusal: it must keep the PR at stage/100 and must not be +// remembered as a refusal for the head. +func TestNativeQueueMethodReadFailureIsReadReason(t *testing.T) { + c, f, pr, claim := nativeQueueEngine() + claim.Inputs["method_reason"] = mergeMethodReasonMixedAuthorship + f.methodErr = errors.New("graphql: timeout") + reason, err := c.tryNativeQueue(context.Background(), pr, claim) + if reason != nativeQueueReasonRead || err == nil || f.enqueues != 0 { + t.Fatalf("reason=%q error=%v enqueues=%d, want %q with error", reason, err, f.enqueues, nativeQueueReasonRead) + } + f.methodErr = nil + if reason, _ := c.tryNativeQueue(context.Background(), pr, claim); reason != nativeQueueReasonMethod || f.methodReads != 2 { + t.Fatalf("after read failure: reason=%q method reads=%d, want a re-read reporting %q", reason, f.methodReads, nativeQueueReasonMethod) + } +} + func TestNativeQueuePreservesMixedAuthorshipGate(t *testing.T) { c, f, pr, claim := nativeQueueEngine() claim.Inputs["method_reason"] = mergeMethodReasonMixedAuthorship @@ -256,6 +279,38 @@ func TestNativeQueuePreservesMixedAuthorshipGate(t *testing.T) { } } +// A SQUASH queue's refusal of a mixed-authorship PR is remembered like a +// merge-method 405: the next sweep at the same head reports the same reason +// without re-reading the queue's method, and a new head or the recheck +// window re-reads it. +func TestNativeQueueMethodRefusalIsRemembered(t *testing.T) { + c, f, pr, claim := nativeQueueEngine() + now := time.Date(2026, 10, 29, 12, 0, 0, 0, time.UTC) + c.now = func() time.Time { return now } + claim.Inputs["method_reason"] = mergeMethodReasonMixedAuthorship + sweep := func() (string, error) { return c.tryNativeQueue(context.Background(), pr, claim) } + if reason, err := sweep(); reason != "native-merge-queue-method" || err == nil { + t.Fatalf("first sweep: reason=%q error=%v", reason, err) + } + if reason, err := sweep(); reason != "native-merge-queue-method" || err != nil { + t.Fatalf("second sweep: reason=%q error=%v", reason, err) + } + if f.methodReads != 1 || f.enqueues != 0 { + t.Fatalf("two sweeps at one head: method reads=%d enqueues=%d, want 1 and 0", f.methodReads, f.enqueues) + } + + f.state.HeadRefOid = "sha8" + claim.Inputs["expect_sha"] = "sha8" + if reason, _ := sweep(); reason != "native-merge-queue-method" || f.methodReads != 2 { + t.Fatalf("new head: reason=%q method reads=%d, want a re-read", reason, f.methodReads) + } + + now = now.Add(mergeRefusalRecheck) + if reason, _ := sweep(); reason != "native-merge-queue-method" || f.methodReads != 3 { + t.Fatalf("after recheck: reason=%q method reads=%d, want a re-read", reason, f.methodReads) + } +} + func TestContributorLaneEnqueuesAfterGates(t *testing.T) { fx := validContributorFixture() fx.nodeID = "PR_7" diff --git a/src/pkg/stageengine/derive_pr_classify.go b/src/pkg/stageengine/derive_pr_classify.go index 07f0714b51..5ed7ed1a6c 100644 --- a/src/pkg/stageengine/derive_pr_classify.go +++ b/src/pkg/stageengine/derive_pr_classify.go @@ -290,6 +290,11 @@ var stageNeedsHumanReasons = map[string]bool{ // the chosen merge method switched off. The merge is not imminent, and // a person has to act (approve, change the setting, or merge by hand). autoMergeReasonMergeRefusedReview: true, autoMergeReasonMergeRefusedMethod: true, + // The native queue's merge method would squash away a mixed-authorship + // PR's signed commits: the same decision as a disabled merge method. It + // is matched here, before the native-merge-queue prefix keeps the rest + // at 100. + prstage.AutoMergeReasonNativeQueueMethod: true, } // stageMergeQueueReasons are reasons reached only after every gate passed, diff --git a/src/pkg/stageengine/derive_pr_merge_refusal_test.go b/src/pkg/stageengine/derive_pr_merge_refusal_test.go index 40993df985..12b2d6abea 100644 --- a/src/pkg/stageengine/derive_pr_merge_refusal_test.go +++ b/src/pkg/stageengine/derive_pr_merge_refusal_test.go @@ -25,6 +25,8 @@ func TestClassifyStagePermanentMergeRefusals(t *testing.T) { {name: "merge queue required", reason: autoMergeReasonMergeQueueRequired, want: stagelabels.MergeQueue}, {name: "native queued position", reason: "native-merge-queued: queued (position 2, AWAITING_CHECKS)", want: stagelabels.MergeQueue}, {name: "native enqueue failure", reason: "native-merge-queue-enqueue", want: stagelabels.MergeQueue}, + {name: "native read failure", reason: "native-merge-queue-read", want: stagelabels.MergeQueue}, + {name: "native queue method refused", reason: "native-merge-queue-method", want: stagelabels.NeedsDecision, wantHuman: true}, {name: "unclassified merge failure", reason: "merge-failed", want: stagelabels.MergeQueue}, } { t.Run(tc.name, func(t *testing.T) { diff --git a/src/pkg/stageengine/prstage/reasons.go b/src/pkg/stageengine/prstage/reasons.go index 3739a27fb6..7c24a58b9a 100644 --- a/src/pkg/stageengine/prstage/reasons.go +++ b/src/pkg/stageengine/prstage/reasons.go @@ -32,6 +32,7 @@ const ( AutoMergeReasonMergeRefusedMethod = "merge-refused:method-not-allowed" AutoMergeReasonMergeRefusedReview = "merge-refused:review-required" AutoMergeReasonNativeQueuePrefix = "native-merge-queue" + AutoMergeReasonNativeQueueMethod = "native-merge-queue-method" AutoMergeReasonNativeQueued = "native-merge-queued" AutoMergeReasonNoAppBotLogin = "no-app-bot-login" AutoMergeReasonReviewGatePrefix = "review-gate:" @@ -152,7 +153,7 @@ func HumanMergeReason(reason string) string { return HumanMergeReasonBreakingChange case base == AutoMergeReasonMergeRefusedReview, strings.HasPrefix(base, AutoMergeReasonReviewGatePrefix): return HumanMergeReasonBranchProtection - case base == AutoMergeReasonMergeRefusedMethod: + case base == AutoMergeReasonMergeRefusedMethod, base == AutoMergeReasonNativeQueueMethod: return HumanMergeReasonMergeMethod case base == "repo-auto-merge-disabled", base == StageLaneContributor+":disabled", base == "disabled": return HumanMergeReasonAutoMergeOff diff --git a/src/pkg/stageengine/prstage/reasons_test.go b/src/pkg/stageengine/prstage/reasons_test.go index 085bcfb4a9..e7a922dd09 100644 --- a/src/pkg/stageengine/prstage/reasons_test.go +++ b/src/pkg/stageengine/prstage/reasons_test.go @@ -20,6 +20,7 @@ func TestHumanMergeReason(t *testing.T) { AutoMergeReasonMergeRefusedReview: HumanMergeReasonBranchProtection, AutoMergeReasonReviewGatePrefix + ReviewGateOutcomeAwaiting: HumanMergeReasonBranchProtection, AutoMergeReasonMergeRefusedMethod: HumanMergeReasonMergeMethod, + AutoMergeReasonNativeQueueMethod: HumanMergeReasonMergeMethod, "repo-auto-merge-disabled": HumanMergeReasonAutoMergeOff, "untrusted-author-role": HumanMergeReasonUntrustedAuthor, ContributorLaneReasonClaimTier: HumanMergeReasonUntrustedAuthor, From 6962078bead9e53b5916c3a3470a1d064c9db81b Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Thu, 29 Oct 2026 15:09:24 -0400 Subject: [PATCH 7/8] fix(automerge): gate native queue before mergeable/current-base checks - Queued lane: a blocked/behind PR on a queue-enabled base is enqueued instead of stopping at not-mergeable (and withdrawing its own entry). - Self, trusted and contributor lanes skip the behind update and the default-on current-base gate on a queue-enabled base; the merge group tests the merged result and a new head would drop the queue entry. - Withdraw the App's own entry only on an allowlist of eligibility-loss reasons (holds, approval loss, changes requested, paused/disabled, draft/closed); transient reasons keep the entry. - Match the own entry by enqueuer __typename Bot as well as slug. - Fold the 085 method-refusal note into the added- changelog fragment. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- changelog.d/added-native-merge-queue.md | 2 +- .../fixed-native-queue-method-refusal.md | 1 - src/docs/native-merge-queue.md | 25 +++-- .../automerge/automerge_contributor_lane.go | 11 +- .../automerge/automerge_native_queue.go | 63 +++++++++-- .../automerge/automerge_native_queue_test.go | 105 +++++++++++++++++- src/pkg/github/automerge/automerge_sweep.go | 25 +++-- src/pkg/github/native_merge_queue.go | 9 +- src/pkg/github/native_merge_queue_test.go | 6 +- 9 files changed, 208 insertions(+), 39 deletions(-) delete mode 100644 changelog.d/fixed-native-queue-method-refusal.md diff --git a/changelog.d/added-native-merge-queue.md b/changelog.d/added-native-merge-queue.md index e622f71e3b..4731adca0d 100644 --- a/changelog.d/added-native-merge-queue.md +++ b/changelog.d/added-native-merge-queue.md @@ -1 +1 @@ -- Automerge sweeps enqueue eligible PRs into GitHub's native merge queue with the validated head pinned, report queue position and state, and withdraw Hive-enqueued entries when their merge gates break while preserving entries queued by people. +- Automerge sweeps enqueue eligible PRs into GitHub's native merge queue with the validated head pinned, report queue position and state, and withdraw Hive-enqueued entries only when the PR loses merge eligibility (a hold, a withdrawn or stale approval, changes requested, a paused or disabled repo, or a draft or closed PR) while preserving entries queued by people. On a queue-enabled base a `blocked` or `behind` PR is enqueued rather than branch-updated, and the default-on current-base gate defers to the queue's merge group. A mixed-authorship PR whose queue uses `SQUASH` parks at `stage/085-needs-decision` with the `merge-method` explanation, and the refusal is remembered per head for 30 minutes. diff --git a/changelog.d/fixed-native-queue-method-refusal.md b/changelog.d/fixed-native-queue-method-refusal.md deleted file mode 100644 index cbd5b60ff3..0000000000 --- a/changelog.d/fixed-native-queue-method-refusal.md +++ /dev/null @@ -1 +0,0 @@ -- A mixed-authorship PR whose base uses a native merge queue set to `SQUASH` no longer sits at `stage/100-merge-queue` forever: the `native-merge-queue-method` refusal now parks it at `stage/085-needs-decision` with the `merge-method` explanation, like a direct merge refused for a disabled merge method, and the refusal is remembered per head for 30 minutes instead of re-reading the queue's method every sweep ([#15377](https://github.com/hivecommons/hive/issues/15377)). diff --git a/src/docs/native-merge-queue.md b/src/docs/native-merge-queue.md index 465b7acec1..f4eddca1b3 100644 --- a/src/docs/native-merge-queue.md +++ b/src/docs/native-merge-queue.md @@ -20,18 +20,29 @@ Already queued PRs are left in place. The sweep reason reports `stage/100-merge-queue`; enqueueing does not emit a completed-merge event or close an issue. The stage observation carries the reason to the dashboard. -When the owning lane rejects a PR for any reason other than its own -native-queue outcomes or author-routing skips (for example a hold, withdrawn -approval, changes request, stale approval, or a transient refusal such as -`not-mergeable`, `required-checks-expected`, `behind`, soak or `release-hold*`), -Hive rechecks the entry's enqueuer and withdraws it only if it belongs to this -App. A transient refusal therefore dequeues the entry, and a later sweep -re-enqueues it once the gates pass again. Entries from a person or another App are preserved. +When the owning lane rejects a PR because it lost merge eligibility — a hold +or `do-not-merge`/`needs-human` label, a removed merger label, a missing, +untrusted or stale queue approval, changes requested, or a draft or closed +PR — Hive rechecks the entry's enqueuer and withdraws it only if it belongs to +this App (a `Bot` enqueuer whose login matches the App's slug). Every other +reason, including transient refusals such as `behind`, `not-mergeable`, pending +checks, a moved head, API read errors, author-routing skips and Hive's own +native-queue outcomes, leaves the entry for GitHub's queue to settle. Entries +from a person or another App are preserved. The queued sweep also withdraws this App's entries on paused repositories or repositories with automerge disabled. Every enqueue and dequeue uses the existing mutation boundary, including its write denials. Reconciliation is periodic/event-driven, so a gate change can race GitHub's eventual merge. +A queue-required base reports `mergeable_state: blocked` (or `behind`) until +the PR merges through the queue, so on a queue-enabled base the queued lane +does not treat those states as `not-mergeable`. For the same reason the lanes +do not update a PR's branch on a queue-enabled base: neither the `behind` +update nor the `auto_merge.require_current_base` gate (on by default) runs +there, since the merge group already tests the PR against the current base +and a pushed head would drop the PR from the queue. A failed queue read keeps +the non-queue behavior. + Queue rules choose the merge method. Where Hive elected a merge commit to preserve mixed authorship and DCO attribution, the queue must use `MERGE`; otherwise Hive reports `native-merge-queue-method` rather than enqueueing. diff --git a/src/pkg/github/automerge/automerge_contributor_lane.go b/src/pkg/github/automerge/automerge_contributor_lane.go index 6651bb4f71..ea7de195bd 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane.go +++ b/src/pkg/github/automerge/automerge_contributor_lane.go @@ -1377,7 +1377,10 @@ func (c *Engine) evaluateContributorLanePR(ctx context.Context, displayRepo, own return eval, contributorLaneReasonChangesRequested, nil } mergeableState := strings.ToLower(strings.TrimSpace(pr.GetMergeableState())) - if mergeableState == "behind" { + // See trySweepSelfAuthoredPR: a native queue tests the merged result. A + // relay evaluation never enqueues, so it keeps the current-base gates. + queueEnabled := sync.OnceValue(func() bool { return !relay && c.nativeQueueEnabled(ctx, pr) }) + if mergeableState == "behind" && !queueEnabled() { if relay { return eval, "behind", nil } @@ -1451,8 +1454,10 @@ func (c *Engine) evaluateContributorLanePR(ctx context.Context, displayRepo, own // Current-base gate (hivecommons/hive#13634): only a green, soaked, // approved head is worth an update-branch commit; a relay evaluation // never updates the branch. - if _, reason, stop, err := c.currentBaseGate(ctx, displayRepo, owner, repo, number, baseBranch, headSHA, !relay, "behind", c.isForkPR(pr)); stop { - return eval, reason, err + if c.requireCurrentBaseFor(owner+"/"+repo) && !queueEnabled() { + if _, reason, stop, err := c.currentBaseGate(ctx, displayRepo, owner, repo, number, baseBranch, headSHA, !relay, "behind", c.isForkPR(pr)); stop { + return eval, reason, err + } } if gateReason, err := c.forgestateMergeGate(ctx, owner, repo, number, headSHA); gateReason != "" || err != nil { return eval, gateReason, err diff --git a/src/pkg/github/automerge/automerge_native_queue.go b/src/pkg/github/automerge/automerge_native_queue.go index 2be156d6cf..bface3036f 100644 --- a/src/pkg/github/automerge/automerge_native_queue.go +++ b/src/pkg/github/automerge/automerge_native_queue.go @@ -18,6 +18,7 @@ const ( nativeQueueReasonRead = prstage.AutoMergeReasonNativeQueuePrefix + "-read" nativeQueueReasonEnqueue = prstage.AutoMergeReasonNativeQueuePrefix + "-enqueue" nativeQueueReasonMethod = prstage.AutoMergeReasonNativeQueueMethod + nativeQueueReasonPaused = "repo-paused-or-disabled" // Lanes passed to reconcileNativeQueueGate by callers outside the three // authoring lanes. @@ -43,6 +44,52 @@ func botSlug(login string) string { return strings.TrimSuffix(strings.TrimSpace(login), "[bot]") } +// ownNativeQueueEntry matches the enqueuer by type as well as slug: a user +// account may hold a login equal to the App's slug. +func ownNativeQueueEntry(e *hgithub.NativeMergeQueueEntry, slug string) bool { + return e != nil && e.Enqueuer.Typename == "Bot" && strings.EqualFold(botSlug(e.Enqueuer.Login), slug) +} + +// nativeQueueWithdrawReason reports whether a lane's rejection reason means +// the PR lost merge eligibility (#15198): a hold, a withdrawn or stale +// approval, changes requested, a paused or disabled repo, or a draft or +// closed PR. Anything else — pending or red checks, behind, a moved head, an +// API error, a routing observation — keeps the App's queue entry. +func nativeQueueWithdrawReason(reason string) bool { + switch reason { + case "held", "label:hold", "label:needs-human", "sentinel", contributorLaneReasonSentinelBlocked, "exempt-label", + "label-removed", autoMergeReasonNoHiveQueueApproval, autoMergeReasonUntrustedQueueApproval, + "queue-approval-missing-head", "queue-approval-head-changed", "self-merge-ban", autoMergeReasonUntrustedMerger, + "changes-requested", autoMergeReasonHumanChangesRequested, contributorLaneReasonChangesRequested, + nativeQueueReasonPaused, "repo-auto-merge-disabled", relayLaneReasonDisabled, + "draft", "closed": + return true + } + return strings.HasPrefix(reason, "label:do-not-merge") || strings.HasPrefix(reason, "excluded-label:") || + strings.HasPrefix(reason, autoMergeReasonReviewGatePrefix+ReviewGateOutcomeChangesRequested) +} + +// nativeQueueEnabled reports whether the PR's base has a native merge queue. +// A read failure reports false so the lane keeps its pre-queue behaviour. +func (c *Engine) nativeQueueEnabled(ctx context.Context, pr *gh.PullRequest) bool { + t := c.nativeQueue() + if t == nil || pr.GetNodeID() == "" { + return false + } + state, err := t.ReadNativeMergeQueue(ctx, pr.GetNodeID()) + return err == nil && state.IsMergeQueueEnabled +} + +// nativeQueueGatedState: a PR not yet merged through a required queue reads +// blocked (or behind) even when every gate the queue re-tests has passed. +func nativeQueueGatedState(pr *gh.PullRequest) bool { + switch strings.ToLower(strings.TrimSpace(pr.GetMergeableState())) { + case "blocked", "behind": + return true + } + return false +} + func (c *Engine) nativeQueue() nativeQueueTransport { t, ok := c.transport.(nativeQueueTransport) if !ok || !t.NativeMergeQueueAvailable() { @@ -127,21 +174,13 @@ func (c *Engine) tryNativeQueue(ctx context.Context, pr *gh.PullRequest, claim e // including cheap prefilter holds. Never withdraw another actor's entry. func (c *Engine) reconcileNativeQueueGate(ctx context.Context, lane, owner, repo string, pr *gh.PullRequest, reason string) { t := c.nativeQueue() - if t == nil || pr == nil || pr.GetNodeID() == "" || reason == "" || strings.HasPrefix(reason, prstage.AutoMergeReasonNativeQueuePrefix) { - return - } - // Routing observations are not eligibility decisions by the owning lane. - switch reason { - case "not-app-authored", trustedAuthorReasonAppAuthored, trustedBotLaneSkipReason, contributorLaneReasonAppAuthored: + if t == nil || pr == nil || pr.GetNodeID() == "" || !nativeQueueWithdrawReason(reason) { return } author := hgithub.SafeGetLogin(pr.GetUser()) if _, own := c.sweepLaneForAuthor(author); own && lane != stageLaneSelf && lane != nativeQueueLaneInactive { return } - if lane == stageLaneTrusted && reason == "untrusted-author-role" { - return - } if lane == stageLaneContributor && c.trustedAuthorDecision(author, c.currentTrustedAuthorPolicy().RequireRole).Allowed { return } @@ -150,7 +189,7 @@ func (c *Engine) reconcileNativeQueueGate(ctx context.Context, lane, owner, repo return } state, err := t.ReadNativeMergeQueue(ctx, pr.GetNodeID()) - if err == nil && state.IsInMergeQueue && state.MergeQueueEntry != nil && strings.EqualFold(botSlug(state.MergeQueueEntry.Enqueuer.Login), login) { + if err == nil && state.IsInMergeQueue && ownNativeQueueEntry(state.MergeQueueEntry, login) { entryID := state.MergeQueueEntry.ID claim := effects.Claim{Repo: owner + "/" + repo, Kind: effects.KindPullRequestMerge, Target: strconv.Itoa(pr.GetNumber()), Actor: "automerge", Inputs: map[string]string{"operation": "dequeue", "entry": entryID}} observe := func(ctx context.Context) (effects.ExternalState, error) { @@ -167,7 +206,7 @@ func (c *Engine) reconcileNativeQueueGate(ctx context.Context, lane, owner, repo if err != nil { return effects.Result{}, err } - if !current.IsInMergeQueue || current.MergeQueueEntry == nil || current.MergeQueueEntry.ID != entryID || !strings.EqualFold(botSlug(current.MergeQueueEntry.Enqueuer.Login), login) { + if !current.IsInMergeQueue || current.MergeQueueEntry == nil || current.MergeQueueEntry.ID != entryID || !ownNativeQueueEntry(current.MergeQueueEntry, login) { return effects.Result{}, nil } if err := t.DequeueNativePullRequest(ctx, state.ID); err != nil { @@ -207,7 +246,7 @@ func (c *Engine) reconcileInactiveNativeQueues(ctx context.Context) { break } for _, pr := range prs { - c.reconcileNativeQueueGate(ctx, nativeQueueLaneInactive, owner, repo, pr, "repo-paused-or-disabled") + c.reconcileNativeQueueGate(ctx, nativeQueueLaneInactive, owner, repo, pr, nativeQueueReasonPaused) } if resp == nil || resp.NextPage == 0 { break diff --git a/src/pkg/github/automerge/automerge_native_queue_test.go b/src/pkg/github/automerge/automerge_native_queue_test.go index 6634c9fb41..5442fb0a26 100644 --- a/src/pkg/github/automerge/automerge_native_queue_test.go +++ b/src/pkg/github/automerge/automerge_native_queue_test.go @@ -2,7 +2,9 @@ package automerge import ( "context" + "encoding/json" "errors" + "net/http" "strings" "testing" "time" @@ -46,6 +48,10 @@ func queueEntry(login string) *hgithub.NativeMergeQueueEntry { e := &hgithub.NativeMergeQueueEntry{ID: "entry1", Position: 2, State: "AWAITING_CHECKS"} // GraphQL Bot.login omits the REST "[bot]" suffix. e.Enqueuer.Login = strings.TrimSuffix(login, "[bot]") + e.Enqueuer.Typename = "User" + if strings.HasSuffix(login, "[bot]") { + e.Enqueuer.Typename = "Bot" + } return e } func nativeQueueEngine() (*Engine, *nativeQueueFake, *gh.PullRequest, effects.Claim) { @@ -119,8 +125,9 @@ func (denyNativeQueueBoundary) Execute(context.Context, effects.Claim, func(cont } func TestNativeQueueGateWithdrawsOnlyHiveEntries(t *testing.T) { - for _, reason := range []string{"held", "changes-requested", "no-approval", "stale-approval", "label:hold"} { - for _, actor := range []string{testHiveAppBotLogin, "maintainer", "another-app[bot]", ""} { + for _, reason := range []string{"held", "label:hold", "label:do-not-merge", "changes-requested", autoMergeReasonHumanChangesRequested, autoMergeReasonReviewGatePrefix + ReviewGateOutcomeChangesRequested, autoMergeReasonNoHiveQueueApproval, "queue-approval-head-changed", "label-removed", nativeQueueReasonPaused, "draft", "closed"} { + // A user account named like the App's slug is not the App. + for _, actor := range []string{testHiveAppBotLogin, "maintainer", "another-app[bot]", "", botSlug(testHiveAppBotLogin)} { t.Run(reason+"/"+actor, func(t *testing.T) { c, f, pr, _ := nativeQueueEngine() f.state.IsInMergeQueue = true @@ -380,3 +387,97 @@ func TestSelfAuthoredSweepReadErrorKeepsQueuedEntry(t *testing.T) { t.Fatalf("dequeues=%d, transient read error must not withdraw own entry", f.dequeues) } } + +// Only eligibility loss withdraws the App's entry (#15198); transient and +// non-gate reasons must leave it for GitHub's queue to settle. +func TestNativeQueueTransientReasonsPreserveOwnEntry(t *testing.T) { + for _, reason := range []string{"behind", "not-mergeable", "conflicting", "fetch-pr-recheck", "head-changed-since-eval", "check-pending", "update-branch", "queue-approval-check", "review-state-check", "author-permission-check", autoMergeReasonCurrentBaseCheck, autoMergeReasonReviewGatePrefix + ReviewGateOutcomeAwaiting, "not-app-authored", "untrusted-author-role", "merge-failed", ""} { + t.Run(reason, func(t *testing.T) { + c, f, pr, _ := nativeQueueEngine() + f.state.IsInMergeQueue = true + f.state.MergeQueueEntry = queueEntry(testHiveAppBotLogin) + for _, lane := range []string{stageLaneSelf, nativeQueueLaneQueued} { + c.reconcileNativeQueueGate(context.Background(), lane, "acme", "widget", pr, reason) + } + if f.dequeues != 0 { + t.Fatalf("dequeues=%d, %q must not withdraw own entry", f.dequeues, reason) + } + }) + } +} + +// A queue-required base reports blocked until the PR merges through the +// queue, so the queued lane must enqueue instead of stopping at +// not-mergeable; without a queue, blocked still stops the lane. +func TestQueuedLaneEnqueuesBlockedPROnQueueBase(t *testing.T) { + for _, enabled := range []bool{true, false} { + t.Run(map[bool]string{true: "queue", false: "no-queue"}[enabled], func(t *testing.T) { + var merged []int + api := newAutoMergeSweepAPI(t, hgithub.AutoMergeQueuedLabel, []sweepPR{{number: 7, nodeID: "PR_7", author: "alice", queuedBy: "bob", label: true, mergeableState: "blocked", statusState: "success", checkStatus: "completed", checkConclusion: "success"}}, &merged) + defer api.Close() + c := newAutoMergeSweepClient(api.URL) + f := &nativeQueueFake{Transport: c.transport, state: hgithub.NativeMergeQueueState{ID: "PR_7", HeadRefOid: "sha7", IsMergeQueueEnabled: enabled}} + c.transport = f + c.trustedAuthorizer = func(string, string) TrustedAuthorDecision { + return TrustedAuthorDecision{Allowed: true, Role: "merger"} + } + result, err := c.SweepQueuedAutoMerges(context.Background(), AutoMergeSweepOptions{}) + want := 0 + if enabled { + want = 1 + } + if err != nil || len(merged) != 0 || len(result.Merged) != 0 || f.enqueues != want || f.dequeues != 0 { + t.Fatalf("result=%+v merged=%v enqueues=%d dequeues=%d error=%v", result, merged, f.enqueues, f.dequeues, err) + } + }) + } +} + +// On a queue-enabled base the merge group tests the merged result, so the +// behind update and the default-on current-base gate must not update the +// branch: a new head would drop the PR from the queue. +func TestNativeQueueSkipsCurrentBaseUpdate(t *testing.T) { + for _, lane := range []string{stageLaneSelf, stageLaneTrusted} { + for _, state := range []string{"behind", "clean"} { + t.Run(lane+"/"+state, func(t *testing.T) { + var merged []int + author := testHiveAppBotLogin + if lane == stageLaneTrusted { + author = "alice" + } + updates := 0 + api := newAutoMergeSweepAPI(t, "", []sweepPR{{number: 7, nodeID: "PR_7", author: author, mergeableState: state, statusState: "success", checkStatus: "completed", checkConclusion: "success"}}, &merged, func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodPut && strings.HasSuffix(r.URL.Path, "/update-branch"): + updates++ + w.WriteHeader(http.StatusAccepted) + _, _ = w.Write([]byte(`{"message":"Updating pull request branch."}`)) + case strings.Contains(r.URL.Path, "/compare/"): + _ = json.NewEncoder(w).Encode(map[string]any{"behind_by": 2, "ahead_by": 1}) + default: + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + w.WriteHeader(http.StatusNotFound) + } + }) + defer api.Close() + c := newAutoMergeSweepClient(api.URL) + c.requireCurrentBase = func(string) bool { return true } + f := &nativeQueueFake{Transport: c.transport, state: hgithub.NativeMergeQueueState{ID: "PR_7", HeadRefOid: "sha7", IsMergeQueueEnabled: true}} + c.transport = f + var err error + if lane == stageLaneSelf { + _, err = c.SweepSelfAuthoredAutoMerges(context.Background(), AutoMergeSweepOptions{}) + } else { + c.trustedAuthorizer = func(string, string) TrustedAuthorDecision { + return TrustedAuthorDecision{Allowed: true, Role: "merger"} + } + c.trustedAuthorPolicy = func() TrustedAuthorPolicy { return TrustedAuthorPolicy{Enabled: true, RequireRole: "merger"} } + _, err = c.SweepTrustedAuthorAutoMerges(context.Background(), AutoMergeSweepOptions{}) + } + if err != nil || updates != 0 || f.enqueues != 1 || len(merged) != 0 { + t.Fatalf("updates=%d enqueues=%d merged=%v error=%v", updates, f.enqueues, merged, err) + } + }) + } + } +} diff --git a/src/pkg/github/automerge/automerge_sweep.go b/src/pkg/github/automerge/automerge_sweep.go index 95b228911e..228a7c9f9b 100644 --- a/src/pkg/github/automerge/automerge_sweep.go +++ b/src/pkg/github/automerge/automerge_sweep.go @@ -1893,7 +1893,10 @@ func (c *Engine) trySweepSelfAuthoredPR(ctx context.Context, displayRepo, owner, c.reconcileOpenPRMergeEffect(ctx, mergeClaim, owner, repo, number) mergeableState := strings.ToLower(strings.TrimSpace(pr.GetMergeableState())) - if strings.EqualFold(pr.GetMergeableState(), "behind") { + // A native merge queue tests the merged result itself; updating the + // branch would only push a new head and drop any queue entry. + queueEnabled := sync.OnceValue(func() bool { return c.nativeQueueEnabled(ctx, pr) }) + if strings.EqualFold(pr.GetMergeableState(), "behind") && !queueEnabled() { if !branchUpdateAllowed { // Keep the cause: still behind, retried next tick, not CI // state and not author work (hivecommons/hive#12607). @@ -1971,8 +1974,10 @@ func (c *Engine) trySweepSelfAuthoredPR(ctx context.Context, displayRepo, owner, // head is the author's to fix and must not be re-based under them, and a // head policy refuses would be updated for nothing. Sits before the desk // so the desk is only asked about a head that would merge this tick. - if ev, reason, stop, err := c.currentBaseGate(ctx, displayRepo, owner, repo, number, baseBranch, evaluatedHeadSHA, branchUpdateAllowed, autoMergeReasonBehindBudgetExhausted, c.isForkPR(pr)); stop { - return ev, reason, err + if c.requireCurrentBaseFor(owner+"/"+repo) && !queueEnabled() { + if ev, reason, stop, err := c.currentBaseGate(ctx, displayRepo, owner, repo, number, baseBranch, evaluatedHeadSHA, branchUpdateAllowed, autoMergeReasonBehindBudgetExhausted, c.isForkPR(pr)); stop { + return ev, reason, err + } } // Approval desk (RFC #4000). Consulted AFTER the sweep's own eligibility @@ -2389,7 +2394,9 @@ func (c *Engine) trySweepTrustedAuthorPR(ctx context.Context, displayRepo, owner return AutoMergeSweepEvent{}, "changes-requested", nil } mergeableState := strings.ToLower(strings.TrimSpace(pr.GetMergeableState())) - if mergeableState == "behind" { + // See trySweepSelfAuthoredPR: a native queue tests the merged result. + queueEnabled := sync.OnceValue(func() bool { return c.nativeQueueEnabled(ctx, pr) }) + if mergeableState == "behind" && !queueEnabled() { headSHA := "" if pr.GetHead() != nil { headSHA = pr.GetHead().GetSHA() @@ -2443,8 +2450,10 @@ func (c *Engine) trySweepTrustedAuthorPR(ctx context.Context, displayRepo, owner } // Current-base gate (hivecommons/hive#13634): only a green head that // every gate above lets through is worth an update-branch commit. - if ev, reason, stop, err := c.currentBaseGate(ctx, displayRepo, owner, repo, number, baseBranch, headSHA, true, "behind", c.isForkPR(pr)); stop { - return ev, reason, err + if c.requireCurrentBaseFor(owner+"/"+repo) && !queueEnabled() { + if ev, reason, stop, err := c.currentBaseGate(ctx, displayRepo, owner, repo, number, baseBranch, headSHA, true, "behind", c.isForkPR(pr)); stop { + return ev, reason, err + } } if gateReason, err := c.forgestateMergeGate(ctx, owner, repo, number, headSHA); gateReason != "" || err != nil { return AutoMergeSweepEvent{}, gateReason, err @@ -2785,7 +2794,9 @@ func (c *Engine) trySweepQueuedPR(ctx context.Context, displayRepo, owner, repo } mergeable := hgithub.MergeableFromState(pr.GetMergeableState(), pr.Mergeable) - if mergeable != hgithub.MergeableYes { + // On a queue-required base GitHub reports blocked until the PR merges + // through the queue; the queue, not this gate, settles that state. + if mergeable != hgithub.MergeableYes && !(nativeQueueGatedState(pr) && c.nativeQueueEnabled(ctx, pr)) { return AutoMergeSweepEvent{}, "not-mergeable", nil } baseBranch := "" diff --git a/src/pkg/github/native_merge_queue.go b/src/pkg/github/native_merge_queue.go index f49e26bdd7..91d3a69263 100644 --- a/src/pkg/github/native_merge_queue.go +++ b/src/pkg/github/native_merge_queue.go @@ -10,7 +10,10 @@ type NativeMergeQueueEntry struct { ID string Position int State string - Enqueuer struct{ Login string } + Enqueuer struct { + Typename string `json:"__typename"` + Login string + } } // NativeMergeQueueState describes the queue on a PR's target branch. @@ -31,7 +34,7 @@ func (c *Client) ReadNativeMergeQueue(ctx context.Context, id string) (NativeMer var out struct{ Node *NativeMergeQueueState } err := c.graphQL(ctx, `query($id: ID!) { node(id: $id) { ... on PullRequest { id headRefOid isMergeQueueEnabled isInMergeQueue - mergeQueueEntry { id position state enqueuer { login } } + mergeQueueEntry { id position state enqueuer { __typename login } } timelineItems(last: 1, itemTypes: [REMOVED_FROM_MERGE_QUEUE_EVENT]) { nodes { ... on RemovedFromMergeQueueEvent { id } } } } } }`, map[string]any{"id": id}, &out) if err != nil { @@ -52,7 +55,7 @@ func (c *Client) EnqueueNativePullRequest(ctx context.Context, id, head string) EnqueuePullRequest struct{ MergeQueueEntry *NativeMergeQueueEntry } } err := c.graphQL(ctx, `mutation($input: EnqueuePullRequestInput!) { - enqueuePullRequest(input: $input) { mergeQueueEntry { id position state enqueuer { login } } } + enqueuePullRequest(input: $input) { mergeQueueEntry { id position state enqueuer { __typename login } } } }`, map[string]any{"input": map[string]any{"pullRequestId": id, "expectedHeadOid": head}}, &out) if err != nil { return nil, err diff --git a/src/pkg/github/native_merge_queue_test.go b/src/pkg/github/native_merge_queue_test.go index f96cc4b6fb..4f61da1ef8 100644 --- a/src/pkg/github/native_merge_queue_test.go +++ b/src/pkg/github/native_merge_queue_test.go @@ -28,10 +28,10 @@ func TestNativeMergeQueueGraphQL(t *testing.T) { w.Header().Set("Content-Type", "application/json") switch mode { case "read": - if !strings.Contains(body.Query, "isMergeQueueEnabled") || string(body.Variables["id"]) != `"PR_7"` { + if !strings.Contains(body.Query, "isMergeQueueEnabled") || !strings.Contains(body.Query, "enqueuer { __typename login }") || string(body.Variables["id"]) != `"PR_7"` { t.Errorf("query=%+v", body) } - _, _ = w.Write([]byte(`{"data":{"node":{"id":"PR_7","headRefOid":"validated","isMergeQueueEnabled":true,"isInMergeQueue":true,"mergeQueueEntry":{"id":"entry","position":3,"state":"AWAITING_CHECKS","enqueuer":{"login":"hive[bot]"}}}}}`)) + _, _ = w.Write([]byte(`{"data":{"node":{"id":"PR_7","headRefOid":"validated","isMergeQueueEnabled":true,"isInMergeQueue":true,"mergeQueueEntry":{"id":"entry","position":3,"state":"AWAITING_CHECKS","enqueuer":{"__typename":"Bot","login":"hive[bot]"}}}}}`)) case "graphql-error": _, _ = w.Write([]byte(`{"errors":[{"message":"expectedHeadOid mismatch"}],"data":null}`)) case "missing-entry": @@ -64,7 +64,7 @@ func TestNativeMergeQueueGraphQL(t *testing.T) { switch mode { case "read": state, err := c.ReadNativeMergeQueue(context.Background(), "PR_7") - if err != nil || !state.IsMergeQueueEnabled || state.MergeQueueEntry == nil || state.MergeQueueEntry.Enqueuer.Login != "hive[bot]" { + if err != nil || !state.IsMergeQueueEnabled || state.MergeQueueEntry == nil || state.MergeQueueEntry.Enqueuer.Login != "hive[bot]" || state.MergeQueueEntry.Enqueuer.Typename != "Bot" { t.Fatalf("state=%+v error=%v", state, err) } case "dequeue": From cb0e58fa96e5b3508939eee90b4785c5345c7bb9 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Fri, 30 Oct 2026 08:16:51 -0400 Subject: [PATCH 8/8] =?UTF-8?q?=E2=9C=A8=20feature:=20make=20the=20native?= =?UTF-8?q?=20merge=20queue=20hand-off=20opt-in=20per=20repo,=20keep=20the?= =?UTF-8?q?=20current-base=20gate=20explicit,=20cache=20the=20queue=20prob?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addresses the three standing review findings on the native merge queue hand-off (hivecommons/hive issue 15198): - Opt-in, per repo: auto_merge.native_merge_queue.enabled (default off) plus a repos allow-list. The zero policy never probes, enqueues, withdraws or lists a paused repo's PRs, so hives without queues see no behaviour change. Settings-parity allowlist, config accessors, the runtime golden and docs are updated. - The require_current_base gate and the behind update-branch stay in force on queue bases; auto_merge.native_merge_queue.skip_current_base_gate (default false) is the explicit, documented bypass. - API cost: isMergeQueueEnabled is cached per owner/repo#base for auto_merge.native_merge_queue.probe_ttl (default 30m, failures not cached), and the per-held-PR reconcile, the queued-lane PullRequests.Get and the 10-minute paused-repo listing run only on opted-in repos. Signed-off-by: Andrew Anderson --- changelog.d/added-native-merge-queue.md | 2 +- src/cmd/hive/main_sweeps.go | 4 + .../stage-labels.md | 2 +- src/docs/native-merge-queue.md | 58 ++++- src/docs/operator-reference.md | 4 + .../automerge_native_merge_queue_test.go | 55 +++++ src/pkg/config/config.go | 57 +++++ .../config/testdata/agent_runtime_golden.json | 3 +- .../dashboard/settings_parity_allowlist.txt | 4 + .../automerge/automerge_contributor_lane.go | 8 +- .../automerge/automerge_native_queue.go | 156 +++++++++++- .../automerge/automerge_native_queue_test.go | 227 ++++++++++++++---- src/pkg/github/automerge/automerge_sweep.go | 35 ++- 13 files changed, 537 insertions(+), 78 deletions(-) create mode 100644 src/pkg/config/automerge_native_merge_queue_test.go diff --git a/changelog.d/added-native-merge-queue.md b/changelog.d/added-native-merge-queue.md index 4731adca0d..3dcfb9cb8b 100644 --- a/changelog.d/added-native-merge-queue.md +++ b/changelog.d/added-native-merge-queue.md @@ -1 +1 @@ -- Automerge sweeps enqueue eligible PRs into GitHub's native merge queue with the validated head pinned, report queue position and state, and withdraw Hive-enqueued entries only when the PR loses merge eligibility (a hold, a withdrawn or stale approval, changes requested, a paused or disabled repo, or a draft or closed PR) while preserving entries queued by people. On a queue-enabled base a `blocked` or `behind` PR is enqueued rather than branch-updated, and the default-on current-base gate defers to the queue's merge group. A mixed-authorship PR whose queue uses `SQUASH` parks at `stage/085-needs-decision` with the `merge-method` explanation, and the refusal is remembered per head for 30 minutes. +- Automerge sweeps can enqueue eligible PRs into GitHub's native merge queue with the validated head pinned, report queue position and state, and withdraw Hive-enqueued entries only when the PR loses merge eligibility (a hold, a withdrawn or stale approval, changes requested, a paused or disabled repo, or a draft or closed PR) while preserving entries queued by people. The hand-off is opt-in per repo through `auto_merge.native_merge_queue.enabled` and `.repos` (default off; hives without queues see no change). On a queue-enabled base a `blocked` or `behind` PR is enqueued rather than reported `not-mergeable`; the `require_current_base` gate and the `behind` update-branch stay in force unless `auto_merge.native_merge_queue.skip_current_base_gate` is set. The `isMergeQueueEnabled` probe is cached per base branch for `auto_merge.native_merge_queue.probe_ttl` (default 30m) and the entry reconciliation runs only on opted-in repos. A mixed-authorship PR whose queue uses `SQUASH` parks at `stage/085-needs-decision` with the `merge-method` explanation, and the refusal is remembered per head for 30 minutes. diff --git a/src/cmd/hive/main_sweeps.go b/src/cmd/hive/main_sweeps.go index 0182af412d..8e1dc87e36 100644 --- a/src/cmd/hive/main_sweeps.go +++ b/src/cmd/hive/main_sweeps.go @@ -850,6 +850,10 @@ func runAutoMergeSweepIfDue(ctx context.Context, ghClient *github.Client, cfg *c return automerge.MergeTrainPolicy{Enabled: cfg.AutoMerge.MergeTrain.IsEnabled(), MaxRebaseAttempts: cfg.AutoMerge.MergeTrain.EffectiveMaxRebaseAttempts()} } opts.TrustedBotAuthors = func() map[string]bool { return cfg.AutoMerge.TrustedBotAuthorSet() } + opts.NativeMergeQueue = func() automerge.NativeMergeQueuePolicy { + q := cfg.AutoMerge.NativeMergeQueue + return automerge.NativeMergeQueuePolicy{Enabled: q.Enabled, Repos: q.RepoSet(), SkipCurrentBaseGate: q.SkipCurrentBaseGate, ProbeTTL: q.EffectiveProbeTTL()} + } } audit := func(event automerge.AutoMergeSweepEvent) { if dashSrv == nil { diff --git a/src/docs/labels-and-control-signals/stage-labels.md b/src/docs/labels-and-control-signals/stage-labels.md index f9c9952435..300c9b505a 100644 --- a/src/docs/labels-and-control-signals/stage-labels.md +++ b/src/docs/labels-and-control-signals/stage-labels.md @@ -365,7 +365,7 @@ used to admit the PR: | a trusted maintainer answered that question on the same head — `/hive decision ` or a CHANGES_REQUESTED review (recorded in `review-human-change-requests.json`, head-scoped) | `070-fixing`, reason `contributor-lane:decision-answered`: the answer is folded into the reviewer verdict for that head on every tick and after a restart, so the stage never re-derives 085 from the stale `requires_human` verdict. A bare `/hive decision approve` instead answers that `requires_human` question; it does not remove a `hold` label or provide the broad GitHub-review override for `changes_requested`/`reject`. A new head draws a new verdict with no answer and asks again ([#14679](https://github.com/hivecommons/hive/issues/14679)) | | CI red (`check-failing:*`, `required-check-failing:*`, `status-*`), `conflicting`, `not-mergeable`, `changes-requested`, verdict changes_requested/reject, `needs-rebase` present | `070-fixing` (self-authored lane or `author/hive`) / `071-needs-author` (`author/human`) / `072-returned-to-contributor` (`author/clanker`) — see [Stage 07x by author kind](#stage-07x-by-author-kind). A head whose App-linked commits carry a `Signed-off-by` the squash would misattribute to the PR author (`hive-signoff-attribution`) follows the same split: the branch owner is the only one who can rewrite them. | | `contributor-lane:soaking(...)` — and no red check on the head | `095-pre-merge-soak` | -| `head-changed-since-eval`, `merge-failed`, `merge-queue-required` (the base branch's ruleset only accepts merges through GitHub's merge queue and the transport is a static token that cannot enqueue, so the direct REST merge is refused; with App auth these bases are enqueued instead, see [native-merge-queue.md](../native-merge-queue.md); logged as a skip rather than a merge error, [#14018](https://github.com/hivecommons/hive/issues/14018); the self-authored sweep then skips the direct merge on that base for 30 minutes and logs the skip once per PR head, [#14068](https://github.com/hivecommons/hive/issues/14068); the trusted-author, queued and contributor-lane sweeps skip it the same way), `required-checks-expected`, `behind`, `updated-branch`, `behind-update-budget-exhausted` (a behind self-authored PR the sweep did not update only because this tick's branch-update budget was spent; retried next tick, waits on the hive, [#12607](https://github.com/hivecommons/hive/issues/12607)), `release-hold*`, `native-merge-queued: queued (position N, STATE)` (the PR was enqueued into GitHub's native merge queue) and the failures `native-merge-queue-read`, `native-merge-queue-enqueue`, `native-merge-queue-method` — and no red check on the head | `100-merge-queue` | +| `head-changed-since-eval`, `merge-failed`, `merge-queue-required` (the base branch's ruleset only accepts merges through GitHub's merge queue and the transport is a static token that cannot enqueue, so the direct REST merge is refused; with App auth on a repo opted in through `auto_merge.native_merge_queue` these bases are enqueued instead, see [native-merge-queue.md](../native-merge-queue.md); logged as a skip rather than a merge error, [#14018](https://github.com/hivecommons/hive/issues/14018); the self-authored sweep then skips the direct merge on that base for 30 minutes and logs the skip once per PR head, [#14068](https://github.com/hivecommons/hive/issues/14068); the trusted-author, queued and contributor-lane sweeps skip it the same way), `required-checks-expected`, `behind`, `updated-branch`, `behind-update-budget-exhausted` (a behind self-authored PR the sweep did not update only because this tick's branch-update budget was spent; retried next tick, waits on the hive, [#12607](https://github.com/hivecommons/hive/issues/12607)), `release-hold*`, `native-merge-queued: queued (position N, STATE)` (the PR was enqueued into GitHub's native merge queue) and the failures `native-merge-queue-read`, `native-merge-queue-enqueue`, `native-merge-queue-method` — and no red check on the head | `100-merge-queue` | | CI red (required or optional check) while the PR is deferred to an open `[shared-ci]` incident (``, the governor's `deferred_incident`) for the same head | `050-review-pending` / `060-reviewed`, reason `shared-ci-incident:`: the base or a shared check is what is red, so the PR is never sent to 070/071/072, never nudged and never stalled; once the incident closes a still-red head is handed back as usual. Conflicts and requested changes stay author-facing ([#12606](https://github.com/hivecommons/hive/issues/12606)) | | CI red (required or optional) while **every** failing check on the head is also red on the base branch's tip — the governor's `base_red_checks` / `base_red`, set during CI enrichment when a same-named latest check run on the base ref (meta checks excluded) concluded failure / timed_out / cancelled / action_required / startup_failure / stale **and** the PR's changed files include none of `.github/workflows/*`, `Dockerfile` / `Dockerfile.*` / `*.Dockerfile`, `go.mod` / `go.sum` (a PR touching those plausibly owns the check and stays author-facing; a lookup error or a diff beyond the file-list page budget also stays author-facing) | `050-review-pending` / `060-reviewed`, reason `base-branch-red:,` ("waiting for the base branch's CI to recover (…) — not this PR's fault"): never 070/071/072, never nudged, never stalled, still not merged (the lane's skip reason is the same token). The fix loop counts no attempt, kicks no fixer, and posts one `` hold comment per head; an escalation the hive itself raised (ledger `label_applied`) over such a head has its `needs-human` removed with the hold wording. The next evaluation after the base goes green re-evaluates the head: a still-red PR is handed back as usual. Conflicts and requested changes stay author-facing (seen on [#13270](https://github.com/hivecommons/hive/pull/13270) and [#13258](https://github.com/hivecommons/hive/pull/13258), 2026-10-20) | | CI red (required or optional) on a **fork** PR while **every** failing check's annotation says the check refuses forks (case-insensitive `fork pull request(s) cannot`, `maintainer must push this branch`, or `cannot publish` together with `fork` — a fork-publication / fork-secrets guard) | `085-needs-decision`, reason `fork-guard:,`: never 070/071/072, never nudged, never adopted (074) — neither the author nor the hive can turn the check green by pushing to the fork. The entry comment names the check and says a maintainer must push the branch to the repository (or approve the run as the repository requires). A same-repo PR with the same message, or a fork PR with any other red check, is handed back as usual ([#14273](https://github.com/hivecommons/hive/issues/14273)) | diff --git a/src/docs/native-merge-queue.md b/src/docs/native-merge-queue.md index f4eddca1b3..dc7135c314 100644 --- a/src/docs/native-merge-queue.md +++ b/src/docs/native-merge-queue.md @@ -1,5 +1,30 @@ # Native GitHub merge queues +## Opting in + +The hand-off is **off by default** and opt-in per repository. A hive that +never sets `auto_merge.native_merge_queue` sees no behaviour change: no lane +probes a queue, enqueues, withdraws an entry or lists a paused repo's PRs, and +a queue-required base still parks at `merge-queue-required`. + +```yaml +auto_merge: + native_merge_queue: + enabled: true # default false + repos: [hivecommons/hive] # owner/repo allow-list; empty = every watched repo + skip_current_base_gate: false # default false — see "Current-base gate" below + probe_ttl: 30m # default 30m — see "API cost" below +``` + +| Key | Default | Meaning | +| --- | --- | --- | +| `auto_merge.native_merge_queue.enabled` | `false` | Turns the native-queue hand-off on. | +| `auto_merge.native_merge_queue.repos` | unset (every watched repo once enabled) | `owner/repo` allow-list, matched case-insensitively. Repos outside it keep the pre-queue behaviour. | +| `auto_merge.native_merge_queue.skip_current_base_gate` | `false` | Lets a queue-enabled base bypass the `auto_merge.require_current_base` gate and the `behind` update-branch. Off keeps both gates in force on queue bases. | +| `auto_merge.native_merge_queue.probe_ttl` | `30m` | How long one base branch's `isMergeQueueEnabled` answer is reused before it is re-read. | + +## How it works + Automerge sweeps detect a PR's target queue with GraphQL `PullRequest.isMergeQueueEnabled`. Once the lane's existing approvals, trust, holds, review, CI and soak gates allow a merge, Hive calls @@ -29,19 +54,36 @@ reason, including transient refusals such as `behind`, `not-mergeable`, pending checks, a moved head, API read errors, author-routing skips and Hive's own native-queue outcomes, leaves the entry for GitHub's queue to settle. Entries from a person or another App are preserved. -The queued sweep also withdraws this App's entries on paused repositories or -repositories with automerge disabled. Every enqueue and dequeue uses the +The queued sweep also withdraws this App's entries on opted-in repositories +that are paused or have automerge disabled. Every enqueue and dequeue uses the existing mutation boundary, including its write denials. Reconciliation is periodic/event-driven, so a gate change can race GitHub's eventual merge. A queue-required base reports `mergeable_state: blocked` (or `behind`) until the PR merges through the queue, so on a queue-enabled base the queued lane -does not treat those states as `not-mergeable`. For the same reason the lanes -do not update a PR's branch on a queue-enabled base: neither the `behind` -update nor the `auto_merge.require_current_base` gate (on by default) runs -there, since the merge group already tests the PR against the current base -and a pushed head would drop the PR from the queue. A failed queue read keeps -the non-queue behavior. +does not treat those states as `not-mergeable`. A failed queue read keeps the +non-queue behavior. + +## Current-base gate + +The `auto_merge.require_current_base` gate and the `behind` update-branch stay +in force on a queue-enabled base exactly as on any other base: a behind head +is updated (or parked) before it is enqueued. The merge group does test the +merged result itself, and a pushed head drops the PR from the queue, so an +operator who prefers to let the queue own freshness can set +`auto_merge.native_merge_queue.skip_current_base_gate: true`. That bypass is +explicit and hive-wide for opted-in repos; it is never implied by the presence +of a queue. + +## API cost + +Each `isMergeQueueEnabled` probe is a GraphQL read. The answer is cached per +`owner/repo#base-branch` for `auto_merge.native_merge_queue.probe_ttl` +(default 30 minutes), so a sweep over many PRs on one base costs one probe, +not one per PR; a read failure is not cached. The per-held-PR entry +reconciliation, the extra `GET /pulls/{n}` the queued lane needs to run it +on a prefiltered PR, and the 10-minute paused-repo PR listing all run only on +repos that opted in through `auto_merge.native_merge_queue`. Queue rules choose the merge method. Where Hive elected a merge commit to preserve mixed authorship and DCO attribution, the queue must use `MERGE`; diff --git a/src/docs/operator-reference.md b/src/docs/operator-reference.md index 2d3867a641..6d068a7679 100644 --- a/src/docs/operator-reference.md +++ b/src/docs/operator-reference.md @@ -440,6 +440,10 @@ advanced into the merge path. | `auto_merge.merge_forward_trusted_human` | unset (off) | Repo list (`owner/repo`) where a `role/trusted` + `author/human` PR on a same-repo branch that GitHub reports as conflicting (`dirty`) gets the base merged into its head as one App-authored, signed-off merge commit via the GitHub merges API (no human commit is rewritten), then merges once CI is green on the new head. A real conflict pushes nothing and posts one `` note naming the files both sides changed. Fork heads are never written. Attempted once per head (#13636). | | `auto_merge.merge_train.enabled` | `true` | Conflict-aware merge train in every automerge lane (hive, trusted-author, contributor) ([#15117](https://github.com/hivecommons/hive/issues/15117)). Each sweep groups the open approved / mergeable-pending PRs of one repo+base by overlapping changed files (`GET /pulls/{n}/files`, cached per head SHA) and lets ONE front per group merge — the clean-mergeable PR approved earliest — while the others wait at stage/080 with lane reason `merge-train:queued-behind#:/:…` (tracker pill "In merge queue · 3 of 4 · ~4m", never a human stage, never `needs-rebase` as a human flag). The moment the front merges, every sibling whose branch is merely `behind` gets `PUT /pulls/{n}/update-branch`; a `dirty` one is handed to the fixer's rebase-resolve episode (`requested_by: merge-train`, the shared two-attempt guard of [#15104](https://github.com/hivecommons/hive/pull/15104); the fixer router still requires a head the hive can push to, so a fork without maintainer edits stays on the plain `conflicting` path for a person). A head the lane last saw blocked on its own (red required check, terminal hold, permanent merge refusal) never fronts, and a group whose best member is blocked imposes no ordering (fail-open) — nobody waits behind a PR that is not moving. One INFO line `merge train` per order change, not per cycle. The queue carries one PR per issue: when two open PRs declare the same `Fixes #N`, the sweep keeps one (green first, then the earlier-opened) and holds the other with lane reason `merge-train:duplicate-of#:/0:0/0:` (tracker pill "Duplicate of #15402 · needs a pick"; *Let hive decide* supersedes, [#15395](https://github.com/hivecommons/hive/issues/15395)). `false` restores independent per-PR evaluation. A PR GitHub reports conflicting on its own, or one at stage/072 returned-to-contributor (label or stage record), is never a member nor counted in the train's size and draws no train idle comment; a conflicting PR rides only while the train itself is repairing it (its rebase requested behind a merged front, loop guard not spent) ([#15395](https://github.com/hivecommons/hive/issues/15395)). | | `auto_merge.merge_train.max_rebase_attempts` | `2` | Loop guard: how many rebase-resolve requests the train makes for one PR behind one front. Past it the PR moves to stage/085 with the pill "Needs human decision · rebase failed twice" and the decision option "Rebase, then merge as maintainer". | +| `auto_merge.native_merge_queue.enabled` | `false` | Opt-in: once the lane's gates pass, enqueue the PR into GitHub's native merge queue (App auth only) instead of merging directly, and withdraw only Hive's own entries when the PR loses eligibility ([#15198](https://github.com/hivecommons/hive/issues/15198)). Off keeps the pre-queue behaviour everywhere, including the `merge-queue-required` park. See [native-merge-queue.md](native-merge-queue.md). | +| `auto_merge.native_merge_queue.repos` | unset (every watched repo once enabled) | `owner/repo` allow-list for the hand-off, matched case-insensitively. | +| `auto_merge.native_merge_queue.skip_current_base_gate` | `false` | Explicitly lets a queue-enabled base bypass the `auto_merge.require_current_base` gate and the `behind` update-branch, because the merge group re-tests the merged result and a pushed head drops the PR from the queue. Off keeps both gates in force on queue bases. | +| `auto_merge.native_merge_queue.probe_ttl` | `30m` | How long one base branch's `isMergeQueueEnabled` GraphQL answer is cached before re-reading. | | `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). | diff --git a/src/pkg/config/automerge_native_merge_queue_test.go b/src/pkg/config/automerge_native_merge_queue_test.go new file mode 100644 index 0000000000..f51faf9f33 --- /dev/null +++ b/src/pkg/config/automerge_native_merge_queue_test.go @@ -0,0 +1,55 @@ +package config + +import ( + "testing" + "time" + + "gopkg.in/yaml.v3" +) + +func TestNativeMergeQueueAutoMergeConfigIsOptInPerRepo(t *testing.T) { + tests := []struct { + name string + cfg NativeMergeQueueAutoMergeConfig + repo string + want bool + wantTTL time.Duration + }{ + {"unset is off", NativeMergeQueueAutoMergeConfig{}, "acme/widget", false, DefaultNativeMergeQueueProbeTTL}, + {"repos without enabled stay off", NativeMergeQueueAutoMergeConfig{Repos: []string{"acme/widget"}}, "acme/widget", false, DefaultNativeMergeQueueProbeTTL}, + {"enabled with no allow-list covers every repo", NativeMergeQueueAutoMergeConfig{Enabled: true}, "acme/widget", true, DefaultNativeMergeQueueProbeTTL}, + {"listed repo matches case-insensitively", NativeMergeQueueAutoMergeConfig{Enabled: true, Repos: []string{" Acme/Widget "}}, "acme/widget", true, DefaultNativeMergeQueueProbeTTL}, + {"unlisted repo is off", NativeMergeQueueAutoMergeConfig{Enabled: true, Repos: []string{"acme/other"}}, "acme/widget", false, DefaultNativeMergeQueueProbeTTL}, + {"probe_ttl overrides the default", NativeMergeQueueAutoMergeConfig{Enabled: true, ProbeTTL: 5 * time.Minute}, "acme/widget", true, 5 * time.Minute}, + {"negative probe_ttl falls back", NativeMergeQueueAutoMergeConfig{Enabled: true, ProbeTTL: -time.Second}, "acme/widget", true, DefaultNativeMergeQueueProbeTTL}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + if got := tc.cfg.EnabledFor(tc.repo); got != tc.want { + t.Fatalf("EnabledFor(%q) = %v, want %v", tc.repo, got, tc.want) + } + if got := tc.cfg.EffectiveProbeTTL(); got != tc.wantTTL { + t.Fatalf("EffectiveProbeTTL = %v, want %v", got, tc.wantTTL) + } + }) + } +} + +func TestNativeMergeQueueAutoMergeConfigParsesYAML(t *testing.T) { + var cfg Config + err := yaml.Unmarshal([]byte(` +auto_merge: + native_merge_queue: + enabled: true + repos: [acme/widget] + skip_current_base_gate: true + probe_ttl: 15m +`), &cfg) + if err != nil { + t.Fatal(err) + } + q := cfg.AutoMerge.NativeMergeQueue + if !q.EnabledFor("acme/widget") || q.EnabledFor("acme/other") || !q.SkipCurrentBaseGate || q.EffectiveProbeTTL() != 15*time.Minute { + t.Fatalf("parsed %+v", q) + } +} diff --git a/src/pkg/config/config.go b/src/pkg/config/config.go index d6d794683c..83aa3ab314 100644 --- a/src/pkg/config/config.go +++ b/src/pkg/config/config.go @@ -8174,6 +8174,63 @@ type AutoMergeConfig struct { // `conflicting` with nothing owning the repair // (hivecommons/hive#15117). Default ON. MergeTrain MergeTrainAutoMergeConfig `yaml:"merge_train,omitempty" json:"merge_train,omitempty"` + // NativeMergeQueue lets the sweep lanes enqueue an eligible PR into + // GitHub's native merge queue instead of merging it directly when the + // base branch requires one (hivecommons/hive#15198). Default OFF and + // opt-in per repo: a hive that never sets it keeps the pre-queue + // behaviour, including the `merge-queue-required` park. + NativeMergeQueue NativeMergeQueueAutoMergeConfig `yaml:"native_merge_queue,omitempty" json:"native_merge_queue,omitempty"` +} + +// NativeMergeQueueAutoMergeConfig is auto_merge.native_merge_queue.*. +type NativeMergeQueueAutoMergeConfig struct { + // Enabled turns the native-queue hand-off on. Default off. + Enabled bool `yaml:"enabled,omitempty" json:"enabled,omitempty"` + // Repos restricts the hand-off to these repositories (owner/repo, + // case-insensitive). Empty means every watched repository once Enabled. + Repos []string `yaml:"repos,omitempty" json:"repos,omitempty"` + // SkipCurrentBaseGate lets a queue-enabled base bypass the + // auto_merge.require_current_base gate and the behind update-branch, + // because the merge group re-tests the merged result and a new head + // would drop the PR from the queue. Default off: the gate stays in + // force on queue bases exactly as on any other base. + SkipCurrentBaseGate bool `yaml:"skip_current_base_gate,omitempty" json:"skip_current_base_gate,omitempty"` + // ProbeTTL bounds how long a base branch's "has a merge queue" answer + // is reused before it is re-read from GraphQL. 0/unset means + // DefaultNativeMergeQueueProbeTTL. + ProbeTTL time.Duration `yaml:"probe_ttl,omitempty" json:"probe_ttl,omitempty"` +} + +// DefaultNativeMergeQueueProbeTTL is how long a base branch's merge-queue +// probe is cached when auto_merge.native_merge_queue.probe_ttl is unset. +const DefaultNativeMergeQueueProbeTTL = 30 * time.Minute + +// RepoSet is auto_merge.native_merge_queue.repos as a lower-cased membership +// map; nil means the allow-list is empty (every repo once Enabled). +func (n NativeMergeQueueAutoMergeConfig) RepoSet() map[string]bool { + return repoListSet(n.Repos) +} + +// EnabledFor reports whether repo (owner/repo, matched case-insensitively) +// may hand PRs to a native merge queue: Enabled and either no allow-list or +// listed in it. +func (n NativeMergeQueueAutoMergeConfig) EnabledFor(repo string) bool { + if !n.Enabled { + return false + } + set := n.RepoSet() + if len(set) == 0 { + return true + } + return set[strings.ToLower(strings.TrimSpace(repo))] +} + +// EffectiveProbeTTL is probe_ttl with the default applied. +func (n NativeMergeQueueAutoMergeConfig) EffectiveProbeTTL() time.Duration { + if n.ProbeTTL <= 0 { + return DefaultNativeMergeQueueProbeTTL + } + return n.ProbeTTL } // MergeTrainAutoMergeConfig is auto_merge.merge_train.*. diff --git a/src/pkg/config/testdata/agent_runtime_golden.json b/src/pkg/config/testdata/agent_runtime_golden.json index d417b2075f..9d293e932b 100644 --- a/src/pkg/config/testdata/agent_runtime_golden.json +++ b/src/pkg/config/testdata/agent_runtime_golden.json @@ -721,7 +721,8 @@ "trusted_authors": {}, "contributor_lane": {}, "contributor_tier": {}, - "merge_train": {} + "merge_train": {}, + "native_merge_queue": {} }, "decisions": { "delegate": {} diff --git a/src/pkg/dashboard/settings_parity_allowlist.txt b/src/pkg/dashboard/settings_parity_allowlist.txt index 6f889a011f..99510731d5 100644 --- a/src/pkg/dashboard/settings_parity_allowlist.txt +++ b/src/pkg/dashboard/settings_parity_allowlist.txt @@ -160,6 +160,10 @@ auto_merge.human_merge_labels[] # needs-owner-merge PRs wait for a human; yaml-o auto_merge.merge_forward_trusted_human[] # TODO #11854 auto_merge.merge_train.enabled # conflict-aware merge train (#15117); yaml-only auto_merge.merge_train.max_rebase_attempts # merge train rebase loop guard (#15117); yaml-only +auto_merge.native_merge_queue.enabled # native merge queue hand-off opt-in (#15198); yaml-only +auto_merge.native_merge_queue.probe_ttl # merge-queue probe cache TTL (#15198); yaml-only +auto_merge.native_merge_queue.repos[] # native merge queue per-repo allow-list (#15198); yaml-only +auto_merge.native_merge_queue.skip_current_base_gate # explicit current-base bypass on queue bases (#15198); yaml-only auto_merge.no_ci_ok[] # TODO #11854 auto_merge.repos..required_checks[] # TODO #11854 auto_merge.require_current_base[] # current-base gate per-repo "on" override, written by the Repos tab (#15335) diff --git a/src/pkg/github/automerge/automerge_contributor_lane.go b/src/pkg/github/automerge/automerge_contributor_lane.go index ea7de195bd..093f11ab6b 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane.go +++ b/src/pkg/github/automerge/automerge_contributor_lane.go @@ -1377,10 +1377,10 @@ func (c *Engine) evaluateContributorLanePR(ctx context.Context, displayRepo, own return eval, contributorLaneReasonChangesRequested, nil } mergeableState := strings.ToLower(strings.TrimSpace(pr.GetMergeableState())) - // See trySweepSelfAuthoredPR: a native queue tests the merged result. A + // See trySweepSelfAuthoredPR: the queue bypass is an explicit opt-in. A // relay evaluation never enqueues, so it keeps the current-base gates. - queueEnabled := sync.OnceValue(func() bool { return !relay && c.nativeQueueEnabled(ctx, pr) }) - if mergeableState == "behind" && !queueEnabled() { + queueSkipsBase := sync.OnceValue(func() bool { return !relay && c.nativeQueueSkipsCurrentBase(ctx, owner+"/"+repo, pr) }) + if mergeableState == "behind" && !queueSkipsBase() { if relay { return eval, "behind", nil } @@ -1454,7 +1454,7 @@ func (c *Engine) evaluateContributorLanePR(ctx context.Context, displayRepo, own // Current-base gate (hivecommons/hive#13634): only a green, soaked, // approved head is worth an update-branch commit; a relay evaluation // never updates the branch. - if c.requireCurrentBaseFor(owner+"/"+repo) && !queueEnabled() { + if c.requireCurrentBaseFor(owner+"/"+repo) && !queueSkipsBase() { if _, reason, stop, err := c.currentBaseGate(ctx, displayRepo, owner, repo, number, baseBranch, headSHA, !relay, "behind", c.isForkPR(pr)); stop { return eval, reason, err } diff --git a/src/pkg/github/automerge/automerge_native_queue.go b/src/pkg/github/automerge/automerge_native_queue.go index bface3036f..daf9885f8b 100644 --- a/src/pkg/github/automerge/automerge_native_queue.go +++ b/src/pkg/github/automerge/automerge_native_queue.go @@ -30,6 +30,93 @@ const ( // each read costs GraphQL points per PR. const inactiveQueueInterval = 10 * time.Minute +// DefaultNativeMergeQueueProbeTTL mirrors +// config.DefaultNativeMergeQueueProbeTTL: how long a base branch's +// "has a merge queue" answer is reused before it is re-read. +const DefaultNativeMergeQueueProbeTTL = 30 * time.Minute + +// NativeMergeQueuePolicy is the live auto_merge.native_merge_queue.* policy +// (hivecommons/hive#15198). The zero value is OFF: no lane reads, enqueues +// or withdraws a native merge queue entry, so a hive without queues sees +// no behaviour change. +type NativeMergeQueuePolicy struct { + // Enabled turns the hand-off on. + Enabled bool + // Repos is the lower-cased owner/repo allow-list; empty means every + // watched repository once Enabled. + Repos map[string]bool + // SkipCurrentBaseGate lets a queue-enabled base bypass the + // require_current_base gate and the behind update-branch. Off keeps + // both gates in force exactly as on any other base. + SkipCurrentBaseGate bool + // ProbeTTL bounds the per-base "has a merge queue" cache; <= 0 means + // DefaultNativeMergeQueueProbeTTL. + ProbeTTL time.Duration +} + +func (p NativeMergeQueuePolicy) repoAllowed(repo string) bool { + if !p.Enabled { + return false + } + if len(p.Repos) == 0 { + return true + } + return p.Repos[strings.ToLower(strings.TrimSpace(repo))] +} + +func (p NativeMergeQueuePolicy) probeTTL() time.Duration { + if p.ProbeTTL <= 0 { + return DefaultNativeMergeQueueProbeTTL + } + return p.ProbeTTL +} + +// nativeQueueProbe is one cached isMergeQueueEnabled answer for a base. +type nativeQueueProbe struct { + enabled bool + at time.Time +} + +func (c *Engine) nativeQueuePolicy() NativeMergeQueuePolicy { + if c == nil || c.nativeMergeQueuePolicy == nil { + return NativeMergeQueuePolicy{} + } + return c.nativeMergeQueuePolicy() +} + +// nativeQueueOptedIn reports whether repo (owner/repo) may use the native +// queue at all: the policy is on, the repo is allow-listed and the +// transport can enqueue (App auth). +func (c *Engine) nativeQueueOptedIn(repo string) bool { + return c.nativeQueuePolicy().repoAllowed(repo) && c.nativeQueueTransport() != nil +} + +// nativeQueueProbeKey identifies one base branch of one repo. +func nativeQueueProbeKey(repo string, pr *gh.PullRequest) string { + return strings.ToLower(strings.TrimSpace(repo)) + "#" + pr.GetBase().GetRef() +} + +// cachedNativeQueueProbe returns the base's cached answer when it is +// younger than the policy's probe TTL. +func (c *Engine) cachedNativeQueueProbe(key string) (bool, bool) { + c.nativeQueueProbeMu.Lock() + defer c.nativeQueueProbeMu.Unlock() + probe, ok := c.nativeQueueProbes[key] + if !ok || c.clock().Sub(probe.at) >= c.nativeQueuePolicy().probeTTL() { + return false, false + } + return probe.enabled, true +} + +func (c *Engine) recordNativeQueueProbe(key string, enabled bool) { + c.nativeQueueProbeMu.Lock() + defer c.nativeQueueProbeMu.Unlock() + if c.nativeQueueProbes == nil { + c.nativeQueueProbes = map[string]nativeQueueProbe{} + } + c.nativeQueueProbes[key] = nativeQueueProbe{enabled: enabled, at: c.clock()} +} + type nativeQueueTransport interface { NativeMergeQueueAvailable() bool ReadNativeMergeQueue(context.Context, string) (hgithub.NativeMergeQueueState, error) @@ -69,15 +156,35 @@ func nativeQueueWithdrawReason(reason string) bool { strings.HasPrefix(reason, autoMergeReasonReviewGatePrefix+ReviewGateOutcomeChangesRequested) } -// nativeQueueEnabled reports whether the PR's base has a native merge queue. -// A read failure reports false so the lane keeps its pre-queue behaviour. -func (c *Engine) nativeQueueEnabled(ctx context.Context, pr *gh.PullRequest) bool { - t := c.nativeQueue() +// nativeQueueEnabled reports whether the PR's base has a native merge queue +// and repo (owner/repo) opted in. The isMergeQueueEnabled answer is cached +// per repo#base for the policy's probe TTL (default 30m) so a sweep over +// many PRs costs one GraphQL read per base, not one per PR. A read failure +// reports false (and is not cached) so the lane keeps its pre-queue +// behaviour. +func (c *Engine) nativeQueueEnabled(ctx context.Context, repo string, pr *gh.PullRequest) bool { + t := c.nativeQueue(repo) if t == nil || pr.GetNodeID() == "" { return false } + key := nativeQueueProbeKey(repo, pr) + if enabled, ok := c.cachedNativeQueueProbe(key); ok { + return enabled + } state, err := t.ReadNativeMergeQueue(ctx, pr.GetNodeID()) - return err == nil && state.IsMergeQueueEnabled + if err != nil { + return false + } + c.recordNativeQueueProbe(key, state.IsMergeQueueEnabled) + return state.IsMergeQueueEnabled +} + +// nativeQueueSkipsCurrentBase reports whether a lane may bypass the +// current-base gate and the behind update-branch for pr: only when the +// operator set auto_merge.native_merge_queue.skip_current_base_gate AND the +// base has a queue. By default the gate stays in force on queue bases. +func (c *Engine) nativeQueueSkipsCurrentBase(ctx context.Context, repo string, pr *gh.PullRequest) bool { + return c.nativeQueuePolicy().SkipCurrentBaseGate && c.nativeQueueEnabled(ctx, repo, pr) } // nativeQueueGatedState: a PR not yet merged through a required queue reads @@ -90,7 +197,12 @@ func nativeQueueGatedState(pr *gh.PullRequest) bool { return false } -func (c *Engine) nativeQueue() nativeQueueTransport { +// nativeQueueTransport returns the transport when it can enqueue (App auth), +// regardless of policy. +func (c *Engine) nativeQueueTransport() nativeQueueTransport { + if c == nil { + return nil + } t, ok := c.transport.(nativeQueueTransport) if !ok || !t.NativeMergeQueueAvailable() { return nil @@ -98,17 +210,31 @@ func (c *Engine) nativeQueue() nativeQueueTransport { return t } +// nativeQueue returns the queue transport for repo (owner/repo), or nil when +// the repo has not opted in or the transport cannot enqueue. +func (c *Engine) nativeQueue(repo string) nativeQueueTransport { + if !c.nativeQueuePolicy().repoAllowed(repo) { + return nil + } + return c.nativeQueueTransport() +} + // tryNativeQueue runs only after the lane's existing merge gates passed. A // queued PR is not a merged PR: no merged audit, issue closure or merge event. func (c *Engine) tryNativeQueue(ctx context.Context, pr *gh.PullRequest, claim effects.Claim) (string, error) { - t := c.nativeQueue() + t := c.nativeQueue(claim.Repo) if t == nil || pr.GetNodeID() == "" { return "", nil } + key := nativeQueueProbeKey(claim.Repo, pr) + if enabled, ok := c.cachedNativeQueueProbe(key); ok && !enabled { + return "", nil + } state, err := t.ReadNativeMergeQueue(ctx, pr.GetNodeID()) if err != nil { return nativeQueueReasonRead, err } + c.recordNativeQueueProbe(key, state.IsMergeQueueEnabled) if !state.IsMergeQueueEnabled && !state.IsInMergeQueue { return "", nil } @@ -173,10 +299,15 @@ func (c *Engine) tryNativeQueue(ctx context.Context, pr *gh.PullRequest, claim e // reconcileNativeQueueGate runs for the owning lane's rejected candidates, // including cheap prefilter holds. Never withdraw another actor's entry. func (c *Engine) reconcileNativeQueueGate(ctx context.Context, lane, owner, repo string, pr *gh.PullRequest, reason string) { - t := c.nativeQueue() + t := c.nativeQueue(owner + "/" + repo) if t == nil || pr == nil || pr.GetNodeID() == "" || !nativeQueueWithdrawReason(reason) { return } + // A base without a queue holds no entry to withdraw; reuse the probe + // so held PRs on plain bases cost nothing. + if enabled, ok := c.cachedNativeQueueProbe(nativeQueueProbeKey(owner+"/"+repo, pr)); ok && !enabled { + return + } author := hgithub.SafeGetLogin(pr.GetUser()) if _, own := c.sweepLaneForAuthor(author); own && lane != stageLaneSelf && lane != nativeQueueLaneInactive { return @@ -223,7 +354,7 @@ func (c *Engine) reconcileNativeQueueGate(ctx context.Context, lane, owner, repo // Pausing a repo stops new merges, but GitHub may still hold entries from a // previous pass. Withdraw only this App's entries through the usual boundary. func (c *Engine) reconcileInactiveNativeQueues(ctx context.Context) { - if c.nativeQueue() == nil { + if !c.nativeQueuePolicy().Enabled || c.nativeQueueTransport() == nil { return } active := map[string]bool{} @@ -234,10 +365,15 @@ func (c *Engine) reconcileInactiveNativeQueues(ctx context.Context) { if (active[display] && c.repoAutoMergeAllowed(display)) || (c.repoOnGitHub != nil && !c.repoOnGitHub(display)) { continue } + owner, repo := c.transport.SplitRepo(display) + // Only an opted-in repo ever held an App entry; listing the others' + // open PRs every 10 minutes would be pure API cost. + if !c.nativeQueueOptedIn(owner + "/" + repo) { + continue + } if !c.inactiveQueueDue(display) { continue } - owner, repo := c.transport.SplitRepo(display) opts := &gh.PullRequestListOptions{State: "open", ListOptions: gh.ListOptions{PerPage: 100}} for { prs, resp, err := c.gh.PullRequests.List(ctx, owner, repo, opts) diff --git a/src/pkg/github/automerge/automerge_native_queue_test.go b/src/pkg/github/automerge/automerge_native_queue_test.go index 5442fb0a26..f24524a2d7 100644 --- a/src/pkg/github/automerge/automerge_native_queue_test.go +++ b/src/pkg/github/automerge/automerge_native_queue_test.go @@ -54,8 +54,20 @@ func queueEntry(login string) *hgithub.NativeMergeQueueEntry { } return e } + +// enableNativeQueue opts every repo into the native queue; the zero policy +// is off (hivecommons/hive#15308). +func enableNativeQueue(c *Engine, policy ...NativeMergeQueuePolicy) { + p := NativeMergeQueuePolicy{Enabled: true} + if len(policy) > 0 { + p = policy[0] + } + c.nativeMergeQueuePolicy = func() NativeMergeQueuePolicy { return p } +} + func nativeQueueEngine() (*Engine, *nativeQueueFake, *gh.PullRequest, effects.Claim) { c := newAutoMergeSweepClient("http://unused.invalid") + enableNativeQueue(c) f := &nativeQueueFake{Transport: c.transport, state: hgithub.NativeMergeQueueState{ID: "PR_7", HeadRefOid: "sha7", IsMergeQueueEnabled: true}} c.transport = f pr := &gh.PullRequest{Number: gh.Ptr(7), NodeID: gh.Ptr("PR_7"), User: &gh.User{Login: gh.Ptr(testHiveAppBotLogin)}} @@ -183,6 +195,7 @@ func TestSelfAuthoredSweepNativeQueueAndHold(t *testing.T) { c := newAutoMergeSweepClient(api.URL) f := &nativeQueueFake{Transport: c.transport, state: hgithub.NativeMergeQueueState{ID: "PR_7", HeadRefOid: "sha7", IsMergeQueueEnabled: true}} c.transport = f + enableNativeQueue(c) if held { f.state.IsInMergeQueue = true f.state.MergeQueueEntry = queueEntry(testHiveAppBotLogin) @@ -212,6 +225,7 @@ func TestHumanMergeLanesEnqueueWithoutDirectMerge(t *testing.T) { c := newAutoMergeSweepClient(api.URL) f := &nativeQueueFake{Transport: c.transport, state: hgithub.NativeMergeQueueState{ID: "PR_7", HeadRefOid: "sha7", IsMergeQueueEnabled: true}} c.transport = f + enableNativeQueue(c) c.trustedAuthorizer = func(string, string) TrustedAuthorDecision { return TrustedAuthorDecision{Allowed: true, Role: "merger"} } @@ -246,6 +260,7 @@ func TestNativeQueuePauseWithdrawsOwnEntry(t *testing.T) { client.SetRepoPausedFunc(func(string) bool { return true }) f := &nativeQueueFake{Transport: client, state: hgithub.NativeMergeQueueState{ID: "PR_7", IsMergeQueueEnabled: true, IsInMergeQueue: true, MergeQueueEntry: queueEntry(testHiveAppBotLogin)}} c.transport = f + enableNativeQueue(c) c.reconcileInactiveNativeQueues(context.Background()) if f.dequeues != 1 || f.enqueues != 0 || len(merged) != 0 { t.Fatalf("dequeues=%d enqueues=%d merged=%v", f.dequeues, f.enqueues, merged) @@ -328,6 +343,7 @@ func TestContributorLaneEnqueuesAfterGates(t *testing.T) { c := newContributorLaneEngine(api.URL, policy, clock, Options{ContributorSoak: soakedTracker(clock)}) f := &nativeQueueFake{Transport: c.transport, state: hgithub.NativeMergeQueueState{ID: "PR_7", HeadRefOid: "sha7", IsMergeQueueEnabled: true}} c.transport = f + enableNativeQueue(c) event, reason, err := c.trySweepContributorLanePR(context.Background(), "widget", "acme", "widget", 7, policy) if err != nil || !strings.HasPrefix(reason, nativeQueueReason) || f.enqueues != 1 || fx.merges != 0 || event.MergeSHA != "" { t.Fatalf("event=%+v reason=%q error=%v enqueues=%d merges=%d", event, reason, err, f.enqueues, fx.merges) @@ -380,6 +396,7 @@ func TestSelfAuthoredSweepReadErrorKeepsQueuedEntry(t *testing.T) { c := newAutoMergeSweepClient(api.URL) f := &nativeQueueFake{Transport: c.transport, state: hgithub.NativeMergeQueueState{ID: "PR_7", HeadRefOid: "sha7", IsMergeQueueEnabled: true, IsInMergeQueue: true, MergeQueueEntry: queueEntry(testHiveAppBotLogin)}, readErr: errors.New("graphql 502")} c.transport = f + enableNativeQueue(c) if _, err := c.SweepSelfAuthoredAutoMerges(context.Background(), AutoMergeSweepOptions{}); err != nil { t.Fatal(err) } @@ -418,6 +435,7 @@ func TestQueuedLaneEnqueuesBlockedPROnQueueBase(t *testing.T) { c := newAutoMergeSweepClient(api.URL) f := &nativeQueueFake{Transport: c.transport, state: hgithub.NativeMergeQueueState{ID: "PR_7", HeadRefOid: "sha7", IsMergeQueueEnabled: enabled}} c.transport = f + enableNativeQueue(c) c.trustedAuthorizer = func(string, string) TrustedAuthorDecision { return TrustedAuthorDecision{Allowed: true, Role: "merger"} } @@ -433,51 +451,176 @@ func TestQueuedLaneEnqueuesBlockedPROnQueueBase(t *testing.T) { } } -// On a queue-enabled base the merge group tests the merged result, so the -// behind update and the default-on current-base gate must not update the -// branch: a new head would drop the PR from the queue. -func TestNativeQueueSkipsCurrentBaseUpdate(t *testing.T) { - for _, lane := range []string{stageLaneSelf, stageLaneTrusted} { - for _, state := range []string{"behind", "clean"} { - t.Run(lane+"/"+state, func(t *testing.T) { - var merged []int - author := testHiveAppBotLogin - if lane == stageLaneTrusted { - author = "alice" - } - updates := 0 - api := newAutoMergeSweepAPI(t, "", []sweepPR{{number: 7, nodeID: "PR_7", author: author, mergeableState: state, statusState: "success", checkStatus: "completed", checkConclusion: "success"}}, &merged, func(w http.ResponseWriter, r *http.Request) { - switch { - case r.Method == http.MethodPut && strings.HasSuffix(r.URL.Path, "/update-branch"): - updates++ - w.WriteHeader(http.StatusAccepted) - _, _ = w.Write([]byte(`{"message":"Updating pull request branch."}`)) - case strings.Contains(r.URL.Path, "/compare/"): - _ = json.NewEncoder(w).Encode(map[string]any{"behind_by": 2, "ahead_by": 1}) - default: - t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) - w.WriteHeader(http.StatusNotFound) +// The current-base gate stays in force on a queue-enabled base: a behind +// head is updated (and so not enqueued this tick) exactly as on any other +// base. Only the explicit auto_merge.native_merge_queue.skip_current_base_gate +// lets the merge group test the merged result instead, because a new head +// would drop the PR from the queue. +func TestNativeQueueCurrentBaseGateIsExplicit(t *testing.T) { + for _, skip := range []bool{false, true} { + for _, lane := range []string{stageLaneSelf, stageLaneTrusted} { + for _, state := range []string{"behind", "clean"} { + t.Run(map[bool]string{false: "gate-kept", true: "gate-skipped"}[skip]+"/"+lane+"/"+state, func(t *testing.T) { + var merged []int + author := testHiveAppBotLogin + if lane == stageLaneTrusted { + author = "alice" } - }) - defer api.Close() - c := newAutoMergeSweepClient(api.URL) - c.requireCurrentBase = func(string) bool { return true } - f := &nativeQueueFake{Transport: c.transport, state: hgithub.NativeMergeQueueState{ID: "PR_7", HeadRefOid: "sha7", IsMergeQueueEnabled: true}} - c.transport = f - var err error - if lane == stageLaneSelf { - _, err = c.SweepSelfAuthoredAutoMerges(context.Background(), AutoMergeSweepOptions{}) - } else { - c.trustedAuthorizer = func(string, string) TrustedAuthorDecision { - return TrustedAuthorDecision{Allowed: true, Role: "merger"} + updates := 0 + api := newAutoMergeSweepAPI(t, "", []sweepPR{{number: 7, nodeID: "PR_7", author: author, mergeableState: state, statusState: "success", checkStatus: "completed", checkConclusion: "success"}}, &merged, func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodPut && strings.HasSuffix(r.URL.Path, "/update-branch"): + updates++ + w.WriteHeader(http.StatusAccepted) + _, _ = w.Write([]byte(`{"message":"Updating pull request branch."}`)) + case strings.Contains(r.URL.Path, "/compare/"): + _ = json.NewEncoder(w).Encode(map[string]any{"behind_by": 2, "ahead_by": 1}) + default: + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + w.WriteHeader(http.StatusNotFound) + } + }) + defer api.Close() + c := newAutoMergeSweepClient(api.URL) + c.requireCurrentBase = func(string) bool { return true } + f := &nativeQueueFake{Transport: c.transport, state: hgithub.NativeMergeQueueState{ID: "PR_7", HeadRefOid: "sha7", IsMergeQueueEnabled: true}} + c.transport = f + enableNativeQueue(c, NativeMergeQueuePolicy{Enabled: true, SkipCurrentBaseGate: skip}) + var err error + if lane == stageLaneSelf { + _, err = c.SweepSelfAuthoredAutoMerges(context.Background(), AutoMergeSweepOptions{}) + } else { + c.trustedAuthorizer = func(string, string) TrustedAuthorDecision { + return TrustedAuthorDecision{Allowed: true, Role: "merger"} + } + c.trustedAuthorPolicy = func() TrustedAuthorPolicy { return TrustedAuthorPolicy{Enabled: true, RequireRole: "merger"} } + _, err = c.SweepTrustedAuthorAutoMerges(context.Background(), AutoMergeSweepOptions{}) } - c.trustedAuthorPolicy = func() TrustedAuthorPolicy { return TrustedAuthorPolicy{Enabled: true, RequireRole: "merger"} } - _, err = c.SweepTrustedAuthorAutoMerges(context.Background(), AutoMergeSweepOptions{}) - } - if err != nil || updates != 0 || f.enqueues != 1 || len(merged) != 0 { - t.Fatalf("updates=%d enqueues=%d merged=%v error=%v", updates, f.enqueues, merged, err) + // The fixture has no base ref, so a clean head with the + // gate kept parks at current-base-check instead of + // updating; either way it is not enqueued. + wantUpdates, wantEnqueues := 0, 0 + if !skip && state == "behind" { + wantUpdates = 1 + } + if skip { + wantEnqueues = 1 + } + if err != nil || updates != wantUpdates || f.enqueues != wantEnqueues || len(merged) != 0 { + t.Fatalf("updates=%d enqueues=%d merged=%v error=%v, want updates=%d enqueues=%d", updates, f.enqueues, merged, err, wantUpdates, wantEnqueues) + } + }) + } + } + } +} + +// Without auto_merge.native_merge_queue.enabled (or with the repo outside +// its allow-list) no lane reads, enqueues or withdraws a queue entry: the +// hive behaves exactly as before the queue hand-off existed. +func TestNativeQueueIsOptInPerRepo(t *testing.T) { + type readCounter struct{ reads int } + for _, mode := range []string{"default-off", "other-repo-only", "listed-repo"} { + t.Run(mode, func(t *testing.T) { + c, f, pr, claim := nativeQueueEngine() + counter := &readCounter{} + reading := &countingNativeQueueFake{nativeQueueFake: f, reads: &counter.reads} + c.transport = reading + f.state.IsInMergeQueue = true + f.state.MergeQueueEntry = queueEntry(testHiveAppBotLogin) + switch mode { + case "default-off": + c.nativeMergeQueuePolicy = nil + case "other-repo-only": + enableNativeQueue(c, NativeMergeQueuePolicy{Enabled: true, Repos: map[string]bool{"acme/other": true}}) + case "listed-repo": + enableNativeQueue(c, NativeMergeQueuePolicy{Enabled: true, Repos: map[string]bool{"acme/widget": true}}) + } + reason, err := c.tryNativeQueue(context.Background(), pr, claim) + c.reconcileNativeQueueGate(context.Background(), stageLaneSelf, "acme", "widget", pr, "held") + enabled := c.nativeQueueEnabled(context.Background(), "acme/widget", pr) + if mode == "listed-repo" { + if err != nil || !strings.HasPrefix(reason, nativeQueueReason) || f.dequeues != 1 || !enabled || counter.reads == 0 { + t.Fatalf("listed repo: reason=%q error=%v dequeues=%d enabled=%v reads=%d", reason, err, f.dequeues, enabled, counter.reads) } - }) + return + } + if reason != "" || err != nil || f.enqueues != 0 || f.dequeues != 0 || enabled || counter.reads != 0 { + t.Fatalf("%s: reason=%q error=%v enqueues=%d dequeues=%d enabled=%v reads=%d, want no queue traffic", mode, reason, err, f.enqueues, f.dequeues, enabled, counter.reads) + } + }) + } +} + +type countingNativeQueueFake struct { + *nativeQueueFake + reads *int +} + +func (f *countingNativeQueueFake) ReadNativeMergeQueue(ctx context.Context, id string) (hgithub.NativeMergeQueueState, error) { + *f.reads++ + return f.nativeQueueFake.ReadNativeMergeQueue(ctx, id) +} + +// The isMergeQueueEnabled probe is read once per repo#base and reused for +// probe_ttl (default 30m); a read failure is not cached, and a new base +// branch is its own probe. +func TestNativeQueueProbeIsCachedPerBase(t *testing.T) { + c, f, pr, _ := nativeQueueEngine() + reads := 0 + c.transport = &countingNativeQueueFake{nativeQueueFake: f, reads: &reads} + now := time.Date(2026, 10, 29, 12, 0, 0, 0, time.UTC) + c.now = func() time.Time { return now } + pr.Base = &gh.PullRequestBranch{Ref: gh.Ptr("main")} + other := &gh.PullRequest{Number: gh.Ptr(8), NodeID: gh.Ptr("PR_8"), Base: &gh.PullRequestBranch{Ref: gh.Ptr("main")}} + for i := 0; i < 3; i++ { + if !c.nativeQueueEnabled(context.Background(), "acme/widget", pr) || !c.nativeQueueEnabled(context.Background(), "acme/widget", other) { + t.Fatal("base has a queue") } } + if reads != 1 { + t.Fatalf("reads=%d, want one probe per base across PRs and sweeps", reads) + } + release := &gh.PullRequest{Number: gh.Ptr(9), NodeID: gh.Ptr("PR_9"), Base: &gh.PullRequestBranch{Ref: gh.Ptr("release-1")}} + f.state.IsMergeQueueEnabled = false + if c.nativeQueueEnabled(context.Background(), "acme/widget", release) || reads != 2 { + t.Fatalf("other base: reads=%d, want its own probe reporting no queue", reads) + } + if !c.nativeQueueEnabled(context.Background(), "acme/widget", pr) || reads != 2 { + t.Fatalf("main stays cached: reads=%d", reads) + } + + now = now.Add(DefaultNativeMergeQueueProbeTTL) + if c.nativeQueueEnabled(context.Background(), "acme/widget", pr) || reads != 3 { + t.Fatalf("after TTL: reads=%d, want a re-read seeing the queue gone", reads) + } + + enableNativeQueue(c, NativeMergeQueuePolicy{Enabled: true, ProbeTTL: time.Minute}) + f.state.IsMergeQueueEnabled = true + now = now.Add(time.Minute) + f.readErr = errors.New("graphql 502") + if c.nativeQueueEnabled(context.Background(), "acme/widget", pr) || reads != 4 { + t.Fatalf("read failure: reads=%d, want false without caching", reads) + } + f.readErr = nil + if !c.nativeQueueEnabled(context.Background(), "acme/widget", pr) || reads != 5 { + t.Fatalf("after failure: reads=%d, want a fresh probe", reads) + } +} + +// A paused repo's open PRs are only listed when the repo opted in. +func TestInactiveQueueReconcileSkipsUnlistedRepos(t *testing.T) { + var merged []int + api := newAutoMergeSweepAPI(t, "", []sweepPR{{number: 7, nodeID: "PR_7", author: testHiveAppBotLogin}}, &merged) + defer api.Close() + c := newAutoMergeSweepClient(api.URL) + client := c.transport.(*hgithub.Client) + client.SetRepoPausedFunc(func(string) bool { return true }) + f := &nativeQueueFake{Transport: client, state: hgithub.NativeMergeQueueState{ID: "PR_7", IsMergeQueueEnabled: true, IsInMergeQueue: true, MergeQueueEntry: queueEntry(testHiveAppBotLogin)}} + c.transport = f + enableNativeQueue(c, NativeMergeQueuePolicy{Enabled: true, Repos: map[string]bool{"acme/other": true}}) + c.reconcileInactiveNativeQueues(context.Background()) + if f.dequeues != 0 { + t.Fatalf("dequeues=%d, unlisted repo must not be reconciled", f.dequeues) + } } diff --git a/src/pkg/github/automerge/automerge_sweep.go b/src/pkg/github/automerge/automerge_sweep.go index 228a7c9f9b..8b99aabd4c 100644 --- a/src/pkg/github/automerge/automerge_sweep.go +++ b/src/pkg/github/automerge/automerge_sweep.go @@ -218,6 +218,10 @@ type Options struct { // callers wired before the policy existed (#15149); ignored when // MergeTrain is set. MergeTrainEnabled func() bool + // NativeMergeQueue returns the live auto_merge.native_merge_queue.* + // policy (hivecommons/hive#15198); nil means off, so no lane probes, + // enqueues or withdraws a native merge queue entry. + NativeMergeQueue func() NativeMergeQueuePolicy // RebaseResolver receives the train's rebase-resolve requests for a // conflicting PR; nil leaves a dirty front on its conflicting path. RebaseResolver RebaseResolver @@ -242,6 +246,10 @@ type Engine struct { inactiveQueueMu sync.Mutex inactiveQueueLast map[string]time.Time + nativeMergeQueuePolicy func() NativeMergeQueuePolicy + nativeQueueProbeMu sync.Mutex + nativeQueueProbes map[string]nativeQueueProbe + selfSkipWarnedMu sync.Mutex selfSkipWarned map[string]struct{} @@ -418,6 +426,7 @@ func New(transport Transport, opts Options) *Engine { openPRs: opts.OpenPRs, sweepTrigger: opts.SweepTrigger, mergeTrainPolicy: mergeTrainPolicyOption(opts), + nativeMergeQueuePolicy: opts.NativeMergeQueue, rebaseResolver: opts.RebaseResolver, rebaseEpisode: opts.RebaseEpisode, hotspotRecorder: opts.HotspotRecorder, @@ -1002,7 +1011,9 @@ func (c *Engine) SweepQueuedAutoMerges(ctx context.Context, opts AutoMergeSweepO } result.Seen++ if reason := c.prefilterQueuedIssue(issue, label); reason != "" { - if c.nativeQueue() != nil { + // Only an opted-in repo may hold an App queue entry worth + // withdrawing; the extra PR read is skipped everywhere else. + if c.nativeQueue(owner+"/"+repoName) != nil { if pr, _, err := c.gh.PullRequests.Get(ctx, owner, repoName, issue.GetNumber()); err == nil { c.reconcileNativeQueueGate(ctx, nativeQueueLaneQueued, owner, repoName, pr, reason) } @@ -1893,10 +1904,12 @@ func (c *Engine) trySweepSelfAuthoredPR(ctx context.Context, displayRepo, owner, c.reconcileOpenPRMergeEffect(ctx, mergeClaim, owner, repo, number) mergeableState := strings.ToLower(strings.TrimSpace(pr.GetMergeableState())) - // A native merge queue tests the merged result itself; updating the - // branch would only push a new head and drop any queue entry. - queueEnabled := sync.OnceValue(func() bool { return c.nativeQueueEnabled(ctx, pr) }) - if strings.EqualFold(pr.GetMergeableState(), "behind") && !queueEnabled() { + // A native merge queue tests the merged result itself, and updating the + // branch would push a new head that drops any queue entry — but the + // current-base gate stays in force unless the operator explicitly set + // auto_merge.native_merge_queue.skip_current_base_gate. + queueSkipsBase := sync.OnceValue(func() bool { return c.nativeQueueSkipsCurrentBase(ctx, owner+"/"+repo, pr) }) + if strings.EqualFold(pr.GetMergeableState(), "behind") && !queueSkipsBase() { if !branchUpdateAllowed { // Keep the cause: still behind, retried next tick, not CI // state and not author work (hivecommons/hive#12607). @@ -1974,7 +1987,7 @@ func (c *Engine) trySweepSelfAuthoredPR(ctx context.Context, displayRepo, owner, // head is the author's to fix and must not be re-based under them, and a // head policy refuses would be updated for nothing. Sits before the desk // so the desk is only asked about a head that would merge this tick. - if c.requireCurrentBaseFor(owner+"/"+repo) && !queueEnabled() { + if c.requireCurrentBaseFor(owner+"/"+repo) && !queueSkipsBase() { if ev, reason, stop, err := c.currentBaseGate(ctx, displayRepo, owner, repo, number, baseBranch, evaluatedHeadSHA, branchUpdateAllowed, autoMergeReasonBehindBudgetExhausted, c.isForkPR(pr)); stop { return ev, reason, err } @@ -2394,9 +2407,9 @@ func (c *Engine) trySweepTrustedAuthorPR(ctx context.Context, displayRepo, owner return AutoMergeSweepEvent{}, "changes-requested", nil } mergeableState := strings.ToLower(strings.TrimSpace(pr.GetMergeableState())) - // See trySweepSelfAuthoredPR: a native queue tests the merged result. - queueEnabled := sync.OnceValue(func() bool { return c.nativeQueueEnabled(ctx, pr) }) - if mergeableState == "behind" && !queueEnabled() { + // See trySweepSelfAuthoredPR: the queue bypass is an explicit opt-in. + queueSkipsBase := sync.OnceValue(func() bool { return c.nativeQueueSkipsCurrentBase(ctx, owner+"/"+repo, pr) }) + if mergeableState == "behind" && !queueSkipsBase() { headSHA := "" if pr.GetHead() != nil { headSHA = pr.GetHead().GetSHA() @@ -2450,7 +2463,7 @@ func (c *Engine) trySweepTrustedAuthorPR(ctx context.Context, displayRepo, owner } // Current-base gate (hivecommons/hive#13634): only a green head that // every gate above lets through is worth an update-branch commit. - if c.requireCurrentBaseFor(owner+"/"+repo) && !queueEnabled() { + if c.requireCurrentBaseFor(owner+"/"+repo) && !queueSkipsBase() { if ev, reason, stop, err := c.currentBaseGate(ctx, displayRepo, owner, repo, number, baseBranch, headSHA, true, "behind", c.isForkPR(pr)); stop { return ev, reason, err } @@ -2796,7 +2809,7 @@ func (c *Engine) trySweepQueuedPR(ctx context.Context, displayRepo, owner, repo mergeable := hgithub.MergeableFromState(pr.GetMergeableState(), pr.Mergeable) // On a queue-required base GitHub reports blocked until the PR merges // through the queue; the queue, not this gate, settles that state. - if mergeable != hgithub.MergeableYes && !(nativeQueueGatedState(pr) && c.nativeQueueEnabled(ctx, pr)) { + if mergeable != hgithub.MergeableYes && !(nativeQueueGatedState(pr) && c.nativeQueueEnabled(ctx, owner+"/"+repo, pr)) { return AutoMergeSweepEvent{}, "not-mergeable", nil } baseBranch := ""