diff --git a/changelog.d/fixed-merge-lane-suppressed-sweep-front.md b/changelog.d/fixed-merge-lane-suppressed-sweep-front.md new file mode 100644 index 0000000000..ab7274c214 --- /dev/null +++ b/changelog.d/fixed-merge-lane-suppressed-sweep-front.md @@ -0,0 +1 @@ +- Serialized merge lane: a pull request that only an automerge sweep lane would merge, and whose branch update and merge writeback containment refuses (for example an auto-promoted contributor PR without `hive-review` at L6), no longer takes the lane front. It used to reach the front and hold it for the full front timeout, refused every round with `writeback: unsolicited write suppressed`, while owner-queued pull requests waited behind it; with several such PRs the lane made no merges for hours. The lane now asks containment before the PR takes a place, so it is refused without being recorded (no front churn), and a front whose sweep writes become refused leaves on its path's next call. The lgtm queue, `/hive merge` relay and dashboard paths are not asked and behave as before. diff --git a/src/docs/operator-reference.md b/src/docs/operator-reference.md index f604c4553c..4af26b92bd 100644 --- a/src/docs/operator-reference.md +++ b/src/docs/operator-reference.md @@ -902,6 +902,18 @@ the block, the lane records that no remaining lane action can satisfy it and pro the next pull request. Pending, missing or unreadable required checks still wait; an eligible pull request that returns joins the back of the lane. +The lane's branch updates and merges pass the same writeback containment as +every other write (`contributor_adopt`: L6 plus the opt-in label for an item +the hive did not author), except on the explicitly requested paths (the `lgtm` +queue, the `/hive merge` relay and the dashboard), which only an operator +pause stops. A pull request that only an automerge sweep lane would merge, +and whose writes containment refuses (for example a contributor PR without +`hive-review` at L6), takes no lane place: the sweep logs +`the sweep merge path cannot update or merge this pull request, so it takes +no lane place: …` and the next eligible pull request goes ahead. A front whose +sweep writes become refused leaves on that path's next call, and stays out +until containment allows it again. + ### The remaining window, and GitHub's up-to-date rule No GitHub merge API can be made conditional on the target branch's tip; they diff --git a/src/pkg/mergelane/gate.go b/src/pkg/mergelane/gate.go index da76ec997c..75e4671e0e 100644 --- a/src/pkg/mergelane/gate.go +++ b/src/pkg/mergelane/gate.go @@ -59,14 +59,19 @@ func (l *Lane) Merge(ctx context.Context, req ghub.LaneMergeRequest) (ghub.LaneM l.emit([]Event{{At: l.opts.Now(), Repo: req.Repo, Branch: branch, PR: req.Number, Action: ActionRefusal, Reason: reason}}) return ghub.LaneMergeResult{Outcome: ghub.LaneOutcomeRefused, Reason: reason}, err } - dec, err := l.Acquire(req.Repo, branch, req.Number, req.Path) - if err != nil || dec.Outcome == OutcomeDeferred { - return ghub.LaneMergeResult{Outcome: string(dec.Outcome), Reason: dec.Reason}, err - } method := &mergeMethod{ requested: req.Method, solicited: req.Path == ghub.PRAuditPathQueue || req.Path == ghub.PRAuditPathRelay || req.Path == ghub.PRAuditPathDashboard, } + if !method.solicited { + if reason := l.pathWriteSuppressed(ctx, req.Repo, branch, req.Number, req.Path); reason != "" { + return l.refuseSuppressedPath(req.Repo, branch, req.Number, reason) + } + } + dec, err := l.Acquire(req.Repo, branch, req.Number, req.Path) + if err != nil || dec.Outcome == OutcomeDeferred { + return ghub.LaneMergeResult{Outcome: string(dec.Outcome), Reason: dec.Reason}, err + } ctx = context.WithValue(ctx, mergeMethodKey{}, method) dec, err = l.Advance(ctx, req.Repo, branch, req.Number, Authorizer(req.Authorize)) res := ghub.LaneMergeResult{Outcome: string(dec.Outcome), Reason: dec.Reason, SHA: dec.MergeSHA, MergeError: dec.MergeError} @@ -76,6 +81,61 @@ func (l *Lane) Merge(ctx context.Context, req ghub.LaneMergeRequest) (ghub.LaneM return res, err } +// PathWriteChecker is an optional GitHub capability: it reports, without +// writing, whether the merge path in ctx may update and merge PR number. It +// returns an error wrapping writeback.ErrSuppressed when writeback +// containment would refuse both (RESTGitHub: the check UpdateBranch and +// Merge apply); any other error means the answer is not known. +type PathWriteChecker interface { + PathCanWrite(ctx context.Context, repo string, number int) error +} + +// pathWriteSuppressed returns a refusal when an unsolicited merge path (the +// automerge sweep lanes) cannot write to pr: writeback containment refuses +// its branch update and merge alike, so as the front it could only hold the +// lane until the front timeout while queued pull requests waited. Such a PR +// takes no lane place. An admitted waiter is not re-checked, so waiting +// costs no extra GitHub call; it is checked again once it is the front. +func (l *Lane) pathWriteSuppressed(ctx context.Context, repo, branch string, pr int, path string) string { + checker, ok := l.opts.GitHub.(PathWriteChecker) + if !ok { + return "" + } + rec, _, err := l.Snapshot(repo, branch) + if err != nil { + return "" + } + if (rec.Front == nil || rec.Front.PR != pr) && rec.waiterIndex(pr) >= 0 { + return "" + } + err = checker.PathCanWrite(ctx, repo, pr) + if !errors.Is(err, writeback.ErrSuppressed) { + return "" + } + if path == "" { + path = "calling" + } + return fmt.Sprintf("the %s merge path cannot update or merge this pull request, so it takes no lane place: %v", path, err) +} + +// refuseSuppressedPath refuses pr with reason. A PR already in the lane +// leaves it (a front ends and the oldest waiter is promoted) and the refusal +// is audited once; a PR outside the lane is not recorded, so asking again on +// every sweep changes nothing. +func (l *Lane) refuseSuppressedPath(repo, branch string, pr int, reason string) (ghub.LaneMergeResult, error) { + res := ghub.LaneMergeResult{Outcome: ghub.LaneOutcomeRefused, Reason: reason} + rec, _, err := l.Snapshot(repo, branch) + if err != nil { + return res, err + } + if (rec.Front == nil || rec.Front.PR != pr) && rec.waiterIndex(pr) < 0 { + return res, nil + } + err = l.Release(repo, branch, pr, reason) + l.emit([]Event{{At: l.opts.Now(), Repo: repo, Branch: branch, PR: pr, Action: ActionRefusal, Reason: reason}}) + return res, err +} + // advanceRejectedFront serves a merge path that rejected pr before the lane // gate (#11023). A PR that is not the front is neither recorded nor read on // GitHub. The front runs one round without the path's authorization, so it @@ -144,6 +204,11 @@ func (l *Lane) nativeMergeQueue(ctx context.Context, repo, branch string) string return "" } +// laneWriteKind is the writeback kind the lane's branch update and merge are +// contained under; PathCanWrite asks the same question before a PR takes a +// lane place. +const laneWriteKind = writeback.KindContributorAdopt + // RESTGitHub is the lane's GitHub over the hive's App client. Client is read // on every call so a rebuilt client is followed. Reads are live. type RESTGitHub struct { @@ -310,7 +375,7 @@ func (g *RESTGitHub) UpdateBranch(ctx context.Context, repo string, number int, return err } // Mutations have no comment body: never reserve comment dedupe on a retry. - if err := g.allowWriteback(ctx, c, repo, number, writeback.KindContributorAdopt, ""); err != nil { + if err := g.allowWriteback(ctx, c, repo, number, laneWriteKind, ""); err != nil { return err } var apiErr error @@ -381,6 +446,16 @@ func (g *RESTGitHub) allowWriteback(ctx context.Context, c *ghub.Client, repo st return nil } +// PathCanWrite applies the containment UpdateBranch and Merge apply, without +// writing: nil when ctx's merge path may update and merge number. +func (g *RESTGitHub) PathCanWrite(ctx context.Context, repo string, number int) error { + c, _, _, _, err := g.rest(repo) + if err != nil { + return err + } + return g.allowWriteback(ctx, c, repo, number, laneWriteKind, "") +} + // Merge is the REST merge with head pinned. The method is the calling path's; // squash is upgraded to a merge commit for forward-merge and upstream-sync // PRs, as MergePR does. @@ -390,7 +465,7 @@ func (g *RESTGitHub) Merge(ctx context.Context, repo string, number int, head st return "", err } // The mutation journal, not comment-content dedupe, reconciles merge attempts. - if err := g.allowWriteback(ctx, c, repo, number, writeback.KindContributorAdopt, ""); err != nil { + if err := g.allowWriteback(ctx, c, repo, number, laneWriteKind, ""); err != nil { return "", err } method, _ := ctx.Value(mergeMethodKey{}).(*mergeMethod) diff --git a/src/pkg/mergelane/suppressed_path_test.go b/src/pkg/mergelane/suppressed_path_test.go new file mode 100644 index 0000000000..10556fb4a5 --- /dev/null +++ b/src/pkg/mergelane/suppressed_path_test.go @@ -0,0 +1,266 @@ +package mergelane + +import ( + "context" + "errors" + "fmt" + "net/http" + "net/http/httptest" + "strings" + "testing" + + ghub "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/writeback" +) + +// writeCheckGH adds the PathWriteChecker capability to the fake: suppressed +// PRs get writeback containment's refusal, err (when set) is an unknown +// answer, and checks records every PR asked about. +type writeCheckGH struct { + *fakeGH + suppressed map[int]bool + err error + checks []int +} + +func (g *writeCheckGH) PathCanWrite(_ context.Context, _ string, n int) error { + g.checks = append(g.checks, n) + if g.err != nil { + return g.err + } + if g.suppressed[n] { + return fmt.Errorf("%w: #%d carries none of hive-review; contributor_adopt needs the label at L6", writeback.ErrSuppressed, n) + } + return nil +} + +func newWriteCheckHarness(t *testing.T) (*harness, *writeCheckGH) { + t.Helper() + h := newGateHarness(t) + g := &writeCheckGH{fakeGH: h.f, suppressed: map[int]bool{}} + store, err := OpenStore(h.path) + if err != nil { + t.Fatalf("OpenStore: %v", err) + } + opts := h.options(store) + opts.GitHub = g + if h.lane, err = New(opts); err != nil { + t.Fatalf("New: %v", err) + } + return h, g +} + +func (h *harness) gateVia(pr int, path string) ghub.LaneMergeResult { + h.t.Helper() + res, err := h.lane.Merge(context.Background(), ghub.LaneMergeRequest{ + Repo: testRepo, Branch: testBranch, Number: pr, Path: path, Authorize: h.authorize, + }) + if err != nil { + h.t.Fatalf("Merge(#%d via %s): %v", pr, path, err) + } + return res +} + +// beyondRules drops the native-merge-queue rules read every gate call already +// makes before the lane runs. +func beyondRules(calls []string) []string { + var out []string + for _, c := range calls { + if c != "RequiredChecks" { + out = append(out, c) + } + } + return out +} + +const suppressedReason = "the sweep merge path cannot update or merge this pull request, so it takes no lane place" + +// The live case: green auto-promoted contributor PRs the sweep would merge +// but whose branch update and merge writeback containment refuses (no +// hive-review label at L6). They used to take the front one at a time and +// hold it for the full front timeout while an owner-queued PR waited. Now +// they take no lane place, sweep after sweep, with no front churn, and the +// queued PR reaches the front on its first call and merges. +func TestSuppressedSweepPRTakesNoLanePlace(t *testing.T) { + h, g := newWriteCheckHarness(t) + for _, n := range []int{1, 2, 3} { + h.f.addPR(n, fmt.Sprintf("h%d", n)) + } + g.suppressed[1], g.suppressed[2] = true, true + + for sweep := 0; sweep < 3; sweep++ { + for _, n := range []int{1, 2} { + expectGate(t, h.gateVia(n, ghub.PRAuditPathSweep), ghub.LaneOutcomeRefused, suppressedReason) + } + expectGate(t, h.frontOnly(1), ghub.LaneOutcomeDeferred, ReasonNotAtFront) + } + if rec := h.record(); rec.Front != nil || len(rec.Waiting) != 0 || rec.LastExit != nil { + t.Fatalf("lane = %+v, want suppressed PRs never recorded", rec) + } + if len(h.events) != 0 || len(beyondRules(h.f.calls)) != 0 { + t.Fatalf("suppressed PRs audited %+v / read GitHub %v", h.events, h.f.calls) + } + + expectGate(t, h.gateVia(3, ghub.PRAuditPathQueue), ghub.LaneOutcomeMerged, "merged") + if len(h.f.merges) != 1 || h.f.merges[0] != "3:h3" || len(h.f.updates) != 0 { + t.Fatalf("merges = %v updates = %v, want only the queued #3", h.f.merges, h.f.updates) + } + if enters := h.eventsFor(ActionFrontEnter); len(enters) != 1 || enters[0].PR != 3 { + t.Fatalf("front enters = %+v, want only #3", enters) + } + if want := []int{1, 2, 1, 2, 1, 2}; fmt.Sprint(g.checks) != fmt.Sprint(want) { + t.Fatalf("write checks = %v, want %v (never the queued PR, never front-only)", g.checks, want) + } +} + +// A front admitted while its path could write, whose writes are refused +// later, leaves on its path's next call, once: the queued waiter is +// promoted and merges, and further calls change nothing. +func TestFrontWhosePathLosesWriteLeavesOnce(t *testing.T) { + h, g := newWriteCheckHarness(t) + p := h.f.addPR(1, "h1") + h.f.setRun(p.Head, "build", "in_progress", "") + h.f.addPR(3, "h3") + expectGate(t, h.gateVia(1, ghub.PRAuditPathSweep), ghub.LaneOutcomeWaiting, "build (in_progress)") + expectGate(t, h.gateVia(3, ghub.PRAuditPathQueue), ghub.LaneOutcomeDeferred, ReasonNotAtFront) + + g.suppressed[1] = true + res := h.gateVia(1, ghub.PRAuditPathSweep) + expectGate(t, res, ghub.LaneOutcomeRefused, suppressedReason) + rec := h.record() + if rec.Front == nil || rec.Front.PR != 3 || len(rec.Waiting) != 0 { + t.Fatalf("lane = %+v, want the queued #3 promoted", rec) + } + if rec.LastExit == nil || rec.LastExit.PR != 1 || rec.LastExit.Reason != res.Reason { + t.Fatalf("last exit = %+v, want #1 with %q", rec.LastExit, res.Reason) + } + events := len(h.events) + if exits := h.eventsFor(ActionFrontExit); len(exits) != 1 || exits[0].PR != 1 { + t.Fatalf("front exits = %+v", exits) + } + if refusals := h.eventsFor(ActionRefusal); len(refusals) == 0 || refusals[len(refusals)-1].Reason != res.Reason { + t.Fatalf("refusals = %+v, want the suppression audited", refusals) + } + for i := 0; i < 3; i++ { + expectGate(t, h.gateVia(1, ghub.PRAuditPathSweep), ghub.LaneOutcomeRefused, suppressedReason) + } + if len(h.events) != events || h.frontPR() != 3 { + t.Fatalf("repeat calls churned the lane: %+v", h.events[events:]) + } + expectGate(t, h.gateVia(3, ghub.PRAuditPathQueue), ghub.LaneOutcomeMerged, "merged") + if len(h.f.merges) != 1 || h.f.merges[0] != "3:h3" { + t.Fatalf("merges = %v", h.f.merges) + } +} + +// An admitted waiter is deferred without any GitHub call, as before; it is +// checked again only once it reaches the front. +func TestAdmittedWaiterIsCheckedAgainAtTheFront(t *testing.T) { + h, g := newWriteCheckHarness(t) + p := h.f.addPR(1, "h1") + h.f.setRun(p.Head, "build", "in_progress", "") + h.f.addPR(2, "h2") + expectGate(t, h.gateVia(1, ghub.PRAuditPathSweep), ghub.LaneOutcomeWaiting, "") + expectGate(t, h.gateVia(2, ghub.PRAuditPathSweep), ghub.LaneOutcomeDeferred, ReasonNotAtFront) + g.suppressed[2] = true + calls, checks := len(h.f.calls), len(g.checks) + for i := 0; i < 3; i++ { + expectGate(t, h.gateVia(2, ghub.PRAuditPathSweep), ghub.LaneOutcomeDeferred, ReasonNotAtFront) + } + if extra := beyondRules(h.f.calls[calls:]); len(extra) != 0 || len(g.checks) != checks { + t.Fatalf("waiter cost GitHub calls %v / checks %v", extra, g.checks[checks:]) + } + + h.f.pass(p.Head) + expectGate(t, h.gateVia(1, ghub.PRAuditPathSweep), ghub.LaneOutcomeMerged, "merged") + if h.frontPR() != 2 { + t.Fatalf("front = #%d, want #2 promoted", h.frontPR()) + } + expectGate(t, h.gateVia(2, ghub.PRAuditPathSweep), ghub.LaneOutcomeRefused, suppressedReason) + if rec := h.record(); rec.Front != nil || len(rec.Waiting) != 0 || len(h.f.merges) != 1 { + t.Fatalf("lane = %+v merges = %v, want #2 gone without a merge", rec, h.f.merges) + } +} + +// Solicited paths (lgtm queue, hive-merge relay, dashboard) are never asked: +// their writes are explicitly requested, so a front on them still updates +// its branch and merges exactly as before. +func TestSolicitedFrontStillUpdatesAndMerges(t *testing.T) { + for _, path := range []string{ghub.PRAuditPathQueue, ghub.PRAuditPathRelay, ghub.PRAuditPathDashboard} { + t.Run(path, func(t *testing.T) { + h, g := newWriteCheckHarness(t) + p := h.f.addPR(1, "h1") + h.f.contained[p.Head] = "old-tip" + g.suppressed[1] = true + + expectGate(t, h.gateVia(1, path), ghub.LaneOutcomeUpdated, "merged the tip in") + h.f.pass(p.Head) + expectGate(t, h.gateVia(1, path), ghub.LaneOutcomeMerged, "merged") + if len(h.f.updates) != 1 || len(h.f.merges) != 1 || h.f.merges[0] != "1:"+p.Head { + t.Fatalf("updates = %v merges = %v", h.f.updates, h.f.merges) + } + if len(g.checks) != 0 { + t.Fatalf("solicited path was write-checked: %v", g.checks) + } + }) + } +} + +// An unknown answer (the PR could not be read) never refuses: the PR enters +// as before, and the write itself is still contained. +func TestUnknownWriteCheckDoesNotRefuse(t *testing.T) { + h, g := newWriteCheckHarness(t) + h.f.addPR(1, "h1") + g.err = errors.New("reading writeback target: boom") + expectGate(t, h.gateVia(1, ghub.PRAuditPathSweep), ghub.LaneOutcomeMerged, "merged") +} + +// RESTGitHub.PathCanWrite asks the containment its UpdateBranch and Merge +// apply, without writing: an unlabeled contributor PR at L6 is refused, an +// opted-in one is allowed, and a solicited path is not asked about the item. +func TestRESTPathCanWriteMatchesTheLaneWrites(t *testing.T) { + for _, tc := range []struct { + name string + labels string + solicited bool + wantErr bool + wantGets int + }{ + {"unlabeled contributor PR", `[]`, false, true, 1}, + {"opted-in contributor PR", `[{"name":"hive-review"}]`, false, false, 1}, + {"solicited path", `[]`, true, false, 0}, + } { + t.Run(tc.name, func(t *testing.T) { + gets, writes := 0, 0 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + writes++ + } + gets++ + _, _ = w.Write([]byte(`{"number":42,"user":{"login":"bob-test7777"},"labels":` + tc.labels + `,"pull_request":{}}`)) + })) + defer srv.Close() + client := ghub.NewClientForTest(srv.URL, "org", []string{"repo"}, nil) + yes := true + gate := writeback.New(writeback.Config{Unsolicited: writeback.UnsolicitedConfig{Backfill: &yes}}, + writeback.Deps{LevelFor: func(string) int { return 6 }}, "") + client.SetWritebackGate(gate) + api := &RESTGitHub{Client: func() *ghub.Client { return client }} + ctx := context.Background() + if tc.solicited { + ctx = context.WithValue(ctx, mergeMethodKey{}, &mergeMethod{solicited: true}) + } + err := api.PathCanWrite(ctx, "org/repo", 42) + if got := errors.Is(err, writeback.ErrSuppressed); got != tc.wantErr { + t.Fatalf("PathCanWrite = %v, want suppressed %v", err, tc.wantErr) + } + if tc.wantErr && !strings.Contains(err.Error(), "hive-review") { + t.Fatalf("refusal %q does not name the opt-in label", err) + } + if writes != 0 || gets != tc.wantGets { + t.Fatalf("gets = %d writes = %d, want %d reads and no write", gets, writes, tc.wantGets) + } + var _ PathWriteChecker = api + }) + } +}