From be3d228568a9e9337e7a067b8c521299904646fb Mon Sep 17 00:00:00 2001 From: Danathar Date: Sat, 31 Oct 2026 11:11:45 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(mergelane):=20release=20a=20?= =?UTF-8?q?closed=20or=20merged=20front=20on=20the=20next=20sweep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A serialized-lane front PR closed, merged outside the lane, made a draft or retargeted got no further round: the lane only reads its front when that PR's own merge path calls in, and every sweep lane lists open PRs only. It held every waiter until the front timeout (up to 4x that when its checks were last seen pending). Each sweep pass now sends the lane one repo-wide ReleaseGoneFronts request per hive-serialized repo it covers. Under the per-lane lock the lane reads each front PR once and, when prShape gives a reason, ends the front with "front check: " (fenced on the epoch it read) and promotes the oldest waiter. Open fronts, including ones waiting on CI, and unreadable PRs are left alone; nothing is written to GitHub. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BQYnPGFJx81c8JXR3o1sg1 Signed-off-by: Danathar --- changelog.d/fixed-merge-lane-gone-front.md | 1 + src/docs/operator-reference.md | 9 + .../github/automerge/automerge_lane_test.go | 41 ++++ src/pkg/github/automerge/automerge_sweep.go | 32 +++ src/pkg/github/merge_lane_gate.go | 6 + src/pkg/mergelane/gate.go | 36 ++++ src/pkg/mergelane/gone_front_test.go | 187 ++++++++++++++++++ src/pkg/mergelane/lane.go | 45 +++++ 8 files changed, 357 insertions(+) create mode 100644 changelog.d/fixed-merge-lane-gone-front.md create mode 100644 src/pkg/mergelane/gone_front_test.go diff --git a/changelog.d/fixed-merge-lane-gone-front.md b/changelog.d/fixed-merge-lane-gone-front.md new file mode 100644 index 0000000000..7182cf5cab --- /dev/null +++ b/changelog.d/fixed-merge-lane-gone-front.md @@ -0,0 +1 @@ +- Serialized merge lane: a front pull request that is closed, merged outside the lane, turned into a draft or retargeted now leaves on the next automerge sweep pass, with a `front check: …` exit reason, and the next waiting pull request is promoted. The sweeps list open pull requests only, so no merge path ever called in for such a front again and it held every waiter until the front timeout (up to four timeouts when its checks were last seen pending). Each pass now reads every front of a `hive-serialized` repository once; open fronts are unchanged. diff --git a/src/docs/operator-reference.md b/src/docs/operator-reference.md index c8302221e8..f604c4553c 100644 --- a/src/docs/operator-reference.md +++ b/src/docs/operator-reference.md @@ -885,6 +885,15 @@ GitHub: ...`, since nothing was applied; a 5xx or transport error on the merge call is not definitive, so that front keeps waiting and re-validates until the front timeout. +A front pull request that is closed, merged outside the lane, turned into a +draft or retargeted to another branch leaves on the next automerge sweep +pass. The merge paths list open pull requests only, so none of them calls in +for it again; instead every pass reads each front of a `hive-serialized` +repository once and releases a front in one of those states with a reason +such as `front check: pull request is closed` or `front check: pull request +is already merged`, and the next waiting pull request is promoted. An open +front, including one waiting on its required checks, is left as it is. + A front that GitHub still reports as `blocked` after its head contains the current target tip and all required checks pass also leaves immediately. The lane re-reads the PR after observing passing checks, so a block in the earlier diff --git a/src/pkg/github/automerge/automerge_lane_test.go b/src/pkg/github/automerge/automerge_lane_test.go index 02d01c2524..f26e2a619a 100644 --- a/src/pkg/github/automerge/automerge_lane_test.go +++ b/src/pkg/github/automerge/automerge_lane_test.go @@ -601,3 +601,44 @@ func TestLaneSweep_HeldQueuedFrontLeavesTheLane(t *testing.T) { } } } + +// A front PR closed or merged outside the lane is not in the open-PR lists +// the lanes walk, so no path calls in for it again. Every pass asks the lane, +// once per hive-serialized repo it covers, to release such fronts; a direct +// repo and a repo the pass does not target are never consulted. +func TestLaneSweep_PassReleasesGoneFronts(t *testing.T) { + var merged []int + api := newSelfAuthoredAutoMergeAPI(t, nil, &merged) + defer api.Close() + g := &laneGate{answer: laneAnswer(hgithub.LaneOutcomeLeft, "#25: front check: pull request is closed")} + c := newLaneSweepEngine(api.URL, config.MergeStrategyHiveSerialized, g.gate, Options{}) + + c.sweepPass(context.Background(), 5, true, nil, "tick") + if len(g.reqs) != 1 { + t.Fatalf("lane requests = %+v, want one front check", g.reqs) + } + if req := g.reqs[0]; !req.ReleaseGoneFronts || req.Repo != "acme/widget" || req.Number != 0 || req.Branch != "" || req.FrontOnly || req.Authorize != nil || req.Path != hgithub.PRAuditPathSweep { + t.Fatalf("lane request = %+v, want a repo-wide front check from the sweep", req) + } + + g.reqs = nil + g.answer = func(context.Context, hgithub.LaneMergeRequest) (hgithub.LaneMergeResult, error) { + return hgithub.LaneMergeResult{Outcome: hgithub.LaneOutcomeWaiting}, errors.New("lane record unwritable") + } + c.sweepPass(context.Background(), 5, true, SweepTargets{"acme/widget": {7: true}}, "event") + if len(g.reqs) != 1 || !g.reqs[0].ReleaseGoneFronts { + t.Fatalf("targeted pass lane requests = %+v, want the repo's front check (a lane error is only logged)", g.reqs) + } + + g.reqs = nil + c.sweepPass(context.Background(), 5, true, SweepTargets{"acme/other": {7: true}}, "event") + if len(g.reqs) != 0 { + t.Fatalf("pass targeting another repo consulted the lane: %+v", g.reqs) + } + + direct := &laneGate{answer: func(context.Context, hgithub.LaneMergeRequest) (hgithub.LaneMergeResult, error) { + t.Fatal("a direct repo must never consult the lane") + return hgithub.LaneMergeResult{}, nil + }} + newLaneSweepEngine(api.URL, config.MergeStrategyDirect, direct.gate, Options{}).sweepPass(context.Background(), 5, true, nil, "tick") +} diff --git a/src/pkg/github/automerge/automerge_sweep.go b/src/pkg/github/automerge/automerge_sweep.go index f88b8f7dfa..821f305c86 100644 --- a/src/pkg/github/automerge/automerge_sweep.go +++ b/src/pkg/github/automerge/automerge_sweep.go @@ -1853,6 +1853,7 @@ func (c *Engine) sweepPass(ctx context.Context, maxMerges int, acmmAllowed bool, result.Merged = append(result.Merged, laneResult.Merged...) } } + c.releaseGoneLaneFronts(ctx, targets) if stageResult, stageErr := c.ApplyStageLabels(ctx); stageErr != nil { c.warn("stage labels reconcile failed", "error", stageErr, "items", stageResult.Items, "writes", stageResult.Writes) } else if stageResult.Writes+stageResult.Comments > 0 { @@ -2346,6 +2347,37 @@ func (c *Engine) rejectBeforeLane(ctx context.Context, serialized bool, gate hgi return AutoMergeSweepEvent{}, reason, nil } +// releaseGoneLaneFronts asks the serialized lane, once per pass and +// hive-serialized repo, to release a front PR that was closed, merged outside +// the lane, made a draft or retargeted. The lanes above list open PRs only, so +// no path calls in for such a front again: without this it held every waiter +// until the front timeout (up to 4x that while its checks were last pending). +// The lane reads one PR per front and never merges here. +func (c *Engine) releaseGoneLaneFronts(ctx context.Context, targets SweepTargets) { + for _, repo := range c.activeRepos() { + owner, name := c.transport.SplitRepo(repo) + fullRepo := owner + "/" + name + if !targets.selectsRepo(fullRepo) { + continue + } + serialized, gate := c.serializedLane(repo) + if !serialized || gate == nil { + continue + } + res, err := hgithub.RunSerializedLane(ctx, gate, hgithub.LaneMergeRequest{ + Repo: fullRepo, + Path: hgithub.PRAuditPathSweep, + ReleaseGoneFronts: true, + }) + switch { + case err != nil: + c.warn("automerge sweep: serialized lane could not check its fronts", "repo", fullRepo, "error", err) + case res.Outcome == hgithub.LaneOutcomeLeft: + c.info("automerge sweep: a closed, merged or retargeted PR left the serialized lane front", "repo", fullRepo, "lane_reason", res.Reason) + } + } +} + // mergeThroughLane hands a PR that passed every gate of its sweep path to the // serialized lane. Only the lane's front PR merges, after the lane's final // re-check and authorize (the path's own authorization for the exact head). diff --git a/src/pkg/github/merge_lane_gate.go b/src/pkg/github/merge_lane_gate.go index 3c25684cfc..8edd346b38 100644 --- a/src/pkg/github/merge_lane_gate.go +++ b/src/pkg/github/merge_lane_gate.go @@ -43,6 +43,12 @@ type LaneMergeRequest struct { // never merges it, and lets it leave with the lane's own reason; any // other PR is not recorded. FrontOnly bool + // ReleaseGoneFronts asks the lane to read every current front of Repo + // (Number and Branch unset) and release one that can no longer merge into + // its branch: closed, merged outside the lane, a draft or retargeted. The + // sweeps list open PRs only, so no path calls in for such a front again. + // The lane never merges, enqueues or writes to GitHub here. + ReleaseGoneFronts bool } // LaneMergeResult is what the lane did. Reason is set for every no-merge. diff --git a/src/pkg/mergelane/gate.go b/src/pkg/mergelane/gate.go index c1baa7b6d0..da76ec997c 100644 --- a/src/pkg/mergelane/gate.go +++ b/src/pkg/mergelane/gate.go @@ -37,6 +37,9 @@ func NativeMergeQueueReason(branch string) string { // the lane runs; otherwise the PR records its eligibility and, only when it // is at the front, runs one lane round that may end in the pinned merge. func (l *Lane) Merge(ctx context.Context, req ghub.LaneMergeRequest) (ghub.LaneMergeResult, error) { + if req.ReleaseGoneFronts { + return l.releaseGoneFronts(ctx, req.Repo) + } branch := strings.TrimSpace(req.Branch) if branch == "" { p, err := l.opts.GitHub.PullRequest(ctx, req.Repo, req.Number) @@ -91,6 +94,39 @@ func (l *Lane) advanceRejectedFront(ctx context.Context, repo, branch string, pr return ghub.LaneMergeResult{Outcome: string(dec.Outcome), Reason: dec.Reason}, err } +// releaseGoneFronts serves the sweep's once-per-pass front check: every front +// of repo whose PR can no longer merge into its branch leaves at once. The +// outcome is left when any front left, with one "#N: reason" per front. +func (l *Lane) releaseGoneFronts(ctx context.Context, repo string) (ghub.LaneMergeResult, error) { + if strings.TrimSpace(repo) == "" { + err := errors.New("mergelane: a repo is required to check its fronts") + return ghub.LaneMergeResult{Outcome: ghub.LaneOutcomeRefused, Reason: err.Error()}, err + } + recs, err := l.opts.Store.Lanes(repo) + if err != nil { + return ghub.LaneMergeResult{Outcome: ghub.LaneOutcomeWaiting, Reason: "reading the lane failed: " + err.Error()}, err + } + res := ghub.LaneMergeResult{Outcome: ghub.LaneOutcomeWaiting} + var left []string + var errs []error + for _, rec := range recs { + if rec.Front == nil { + continue + } + reason, err := l.releaseGoneFront(ctx, rec.Repo, rec.Branch, rec.Front.PR) + if err != nil { + errs = append(errs, err) + } + if reason != "" { + left = append(left, fmt.Sprintf("#%d: %s", rec.Front.PR, reason)) + } + } + if len(left) > 0 { + res.Outcome, res.Reason = ghub.LaneOutcomeLeft, strings.Join(left, "; ") + } + return res, errors.Join(errs...) +} + // nativeMergeQueue returns a refusal when branch has GitHub's native merge // queue or when that cannot be established (fail closed). func (l *Lane) nativeMergeQueue(ctx context.Context, repo, branch string) string { diff --git a/src/pkg/mergelane/gone_front_test.go b/src/pkg/mergelane/gone_front_test.go new file mode 100644 index 0000000000..3feac639c1 --- /dev/null +++ b/src/pkg/mergelane/gone_front_test.go @@ -0,0 +1,187 @@ +package mergelane + +import ( + "context" + "strings" + "testing" + + ghub "github.com/hivecommons/hive/pkg/github" +) + +func (h *harness) releaseGone() (ghub.LaneMergeResult, error) { + h.t.Helper() + return h.lane.Merge(context.Background(), ghub.LaneMergeRequest{ + Repo: testRepo, Path: ghub.PRAuditPathSweep, ReleaseGoneFronts: true, + }) +} + +// A front PR closed, merged, drafted or retargeted outside the lane gets no +// further round from its merge path (the sweeps list open PRs only), so it +// held every waiter until the front timeout. The sweep's front check releases +// it at once with prShape's reason and promotes the oldest waiter, without +// any GitHub write. +func TestGoneFrontLeavesOnTheSweepFrontCheck(t *testing.T) { + for _, tc := range []struct { + name string + apply func(*PullRequest) + want string + }{ + {"closed", func(p *PullRequest) { p.Open = false }, "pull request is closed"}, + {"merged elsewhere", func(p *PullRequest) { p.Open, p.Merged = false, true }, "pull request is already merged"}, + {"draft", func(p *PullRequest) { p.Draft = true }, "pull request is a draft"}, + {"retargeted", func(p *PullRequest) { p.Base = "release" }, `pull request now targets "release", not "main"`}, + } { + t.Run(tc.name, func(t *testing.T) { + h := newGateHarness(t) + p := h.f.addPR(1, "h1") + h.f.addPR(2, "h2") + for _, n := range []int{1, 2, 3} { + h.acquire(n) + } + deadline := h.record().Front.DeadlineAt + tc.apply(p) + calls := len(h.f.calls) + + res, err := h.releaseGone() + if err != nil { + t.Fatalf("releaseGone: %v", err) + } + want := "front check: " + tc.want + expectGate(t, res, ghub.LaneOutcomeLeft, "#1: "+want) + rec := h.record() + if rec.Front == nil || rec.Front.PR != 2 || !h.now.Before(deadline) { + t.Fatalf("front = %+v at %s, want #2 promoted before deadline %s", rec.Front, h.now, deadline) + } + if len(rec.Waiting) != 1 || rec.Waiting[0].PR != 3 { + t.Fatalf("waiting = %+v, want only #3", rec.Waiting) + } + if exit := rec.LastExit; exit == nil || exit.PR != 1 || exit.Reason != want || !exit.At.Equal(h.now) { + t.Fatalf("last exit = %+v, want #1 with %q", exit, want) + } + exits := h.eventsFor(ActionFrontExit) + if len(exits) != 1 || exits[0].PR != 1 || exits[0].Head != "h1" || exits[0].Reason != want { + t.Fatalf("front exit audit = %+v", exits) + } + if enters := h.eventsFor(ActionFrontEnter); len(enters) != 2 || enters[1].PR != 2 { + t.Fatalf("front enter audit = %+v, want #2 entering after #1", enters) + } + if got := h.f.calls[calls:]; len(got) != 1 || got[0] != "PullRequest" { + t.Fatalf("GitHub calls = %v, want one PR read", got) + } + if len(h.f.merges) != 0 || len(h.f.updates) != 0 || len(h.f.comments) != 0 { + t.Fatalf("front check wrote to GitHub: merges=%v updates=%v comments=%v", h.f.merges, h.f.updates, h.f.comments) + } + // The promoted front then runs its own round and merges. + expectGate(t, h.gate(2), ghub.LaneOutcomeMerged, "merged") + }) + } +} + +// An open front is untouched, including one legitimately waiting on CI: the +// check reads the PR and changes nothing, so its deadline, extensions and +// pending-checks state stay as they were. +func TestOpenFrontStaysOnTheSweepFrontCheck(t *testing.T) { + h := newGateHarness(t) + p := h.f.addPR(1, "h1") + h.f.setRun(p.Head, "build", "in_progress", "") + h.acquire(1) + h.acquire(2) + expectOutcome(t, h.advance(1), OutcomeWaiting, "build (in_progress)") + before := h.record() + if !before.Front.ChecksPending { + t.Fatalf("front = %+v, want it waiting on pending checks", before.Front) + } + events := len(h.events) + + res, err := h.releaseGone() + if err != nil { + t.Fatalf("releaseGone: %v", err) + } + if res.Outcome != ghub.LaneOutcomeWaiting || res.Reason != "" { + t.Fatalf("result = %+v, want waiting with no reason", res) + } + after := h.record() + if after.Front == nil || *after.Front != *before.Front || after.LastExit != nil || len(after.Waiting) != 1 { + t.Fatalf("record changed:\nbefore %+v\nafter %+v", before, after) + } + if len(h.events) != events { + t.Fatalf("open front check audited %+v", h.events[events:]) + } +} + +// A front whose PR cannot be read stays (the timeout still bounds it), and +// the error is reported to the sweep. +func TestUnreadableFrontStaysOnTheSweepFrontCheck(t *testing.T) { + h := newGateHarness(t) + h.f.addPR(1, "h1").Open = false + h.acquire(1) + h.acquire(2) + h.f.errs["PullRequest"] = errBoom + res, err := h.releaseGone() + if err == nil || !strings.Contains(err.Error(), "#1") { + t.Fatalf("err = %v, want the PR read failure for #1", err) + } + if res.Outcome != ghub.LaneOutcomeWaiting || h.frontPR() != 1 || h.record().LastExit != nil { + t.Fatalf("result = %+v, front = #%d; want #1 kept", res, h.frontPR()) + } +} + +// The release is fenced on the front it read: a front that changed while the +// PR was being read (here released by its path, promoting #2) is not ended. +func TestGoneFrontCheckIsFencedOnTheFrontItRead(t *testing.T) { + h := newGateHarness(t) + h.f.addPR(1, "h1").Open = false + for _, n := range []int{1, 2, 3} { + h.acquire(n) + } + h.f.on("PullRequest", 1, func() { + if err := h.lane.Release(testRepo, testBranch, 1, "released by its merge path"); err != nil { + t.Fatalf("Release: %v", err) + } + }) + res, err := h.releaseGone() + if err != nil || res.Outcome != ghub.LaneOutcomeWaiting { + t.Fatalf("result = %+v, %v; want waiting, nothing left by the check", res, err) + } + if rec := h.record(); rec.Front == nil || rec.Front.PR != 2 || len(rec.Waiting) != 1 || rec.LastExit == nil || rec.LastExit.Reason != "released by its merge path" { + t.Fatalf("record = %+v, want #2 at the front and #3 waiting", rec) + } +} + +// Every branch's front of the repo is checked; an empty lane and another +// repo's lane cost no GitHub call. +func TestGoneFrontCheckCoversEveryBranchOfTheRepo(t *testing.T) { + h := newGateHarness(t) + h.f.addPR(1, "h1").Open = false + rel := h.f.addPR(5, "h5") + rel.Base = "release" + h.acquire(1) + if _, err := h.lane.Acquire(testRepo, "release", 5, "sweep"); err != nil { + t.Fatal(err) + } + if _, err := h.lane.Acquire("acme/other", testBranch, 1, "sweep"); err != nil { + t.Fatal(err) + } + res, err := h.releaseGone() + if err != nil { + t.Fatalf("releaseGone: %v", err) + } + expectGate(t, res, ghub.LaneOutcomeLeft, "#1: front check: pull request is closed") + if h.frontPR() != 0 { + t.Fatalf("main front = #%d, want empty", h.frontPR()) + } + if rec, _, _ := h.lane.Snapshot(testRepo, "release"); rec.Front == nil || rec.Front.PR != 5 { + t.Fatalf("release front = %+v, want #5 kept", rec.Front) + } + if rec, _, _ := h.lane.Snapshot("acme/other", testBranch); rec.Front == nil || rec.Front.PR != 1 { + t.Fatalf("other repo front = %+v, want untouched", rec.Front) + } + if n := h.f.count("PullRequest"); n != 2 { + t.Fatalf("PR reads = %d, want one per front of %s", n, testRepo) + } + + empty := newGateHarness(t) + if res, err := empty.releaseGone(); err != nil || res.Outcome != ghub.LaneOutcomeWaiting || len(empty.f.calls) != 0 { + t.Fatalf("empty lane: %+v, %v, calls %v", res, err, empty.f.calls) + } +} diff --git a/src/pkg/mergelane/lane.go b/src/pkg/mergelane/lane.go index 5ba8713761..547fdf9e2d 100644 --- a/src/pkg/mergelane/lane.go +++ b/src/pkg/mergelane/lane.go @@ -303,6 +303,51 @@ func (l *Lane) Release(repo, branch string, pr int, reason string) error { return nil } +// releaseGoneFront reads the front PR pr of repo/branch and, when it can no +// longer merge into branch (prShape: closed, merged outside the lane, a draft +// or retargeted), ends the front with that reason and promotes the oldest +// waiter. Only the front's own merge path runs a round, and the sweeps list +// open PRs only, so without this such a front held the lane until its +// timeout, and up to MaxFrontTimeoutExtensions more when its checks were last +// seen pending. An open front, or one that cannot be read, is left alone. It +// returns the exit reason, empty when the front stayed. +func (l *Lane) releaseGoneFront(ctx context.Context, repo, branch string, pr int) (string, error) { + unlock := l.lockLane(repo, branch) + defer unlock() + rec, _, err := l.Snapshot(repo, branch) + if err != nil || rec.Front == nil || rec.Front.PR != pr { + return "", err + } + epoch := rec.Front.Epoch + p, err := l.opts.GitHub.PullRequest(ctx, repo, pr) + if err != nil { + return "", fmt.Errorf("mergelane: reading front PR #%d of %s: %w", pr, laneKey(repo, branch), err) + } + reason := prShape(p, branch) + if reason == "" { + return "", nil + } + reason = "front check: " + reason + now := l.opts.Now() + var ev []Event + left := false + err = l.opts.Store.update(func(lanes map[string]*Record) error { + rec := lane(lanes, repo, branch) + if rec.Front == nil || rec.Front.PR != pr || rec.Front.Epoch != epoch { + return nil + } + l.exitLocked(rec, now, p.Head, "", reason, &ev) + l.promoteLocked(rec, now, &ev) + left = true + return nil + }) + l.emit(ev) + if err != nil || !left { + return "", err + } + return reason, nil +} + // Advance runs one round of the front PR's validation: validate → update → // wait → final live re-check → pinned merge → post-merge verify. A caller // that is not at the front gets ReasonNotAtFront and nothing is read or