From dc6f09174d463dd94a422b708d6a9abb30a1a477 Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Wed, 28 Oct 2026 09:54:13 -0400 Subject: [PATCH 1/4] fix(mergelane): record definite merge refusals as not-applied Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Co-authored-by: Danathar <6772335+Danathar@users.noreply.github.com> --- ...ed-14936-lane-merge-refusal-not-applied.md | 1 + .../automerge/automerge_contributor_lane.go | 2 +- src/pkg/github/automerge/automerge_sweep.go | 21 ++------ src/pkg/github/merge_effects.go | 28 ++++++++++ src/pkg/mergelane/gate.go | 2 +- src/pkg/mergelane/gate_test.go | 53 +++++++++++++++++++ src/pkg/mergelane/lane.go | 12 ++++- src/pkg/mergelane/scenario_test.go | 22 ++++++++ 8 files changed, 120 insertions(+), 21 deletions(-) create mode 100644 changelog.d/fixed-14936-lane-merge-refusal-not-applied.md create mode 100644 src/pkg/github/merge_effects.go diff --git a/changelog.d/fixed-14936-lane-merge-refusal-not-applied.md b/changelog.d/fixed-14936-lane-merge-refusal-not-applied.md new file mode 100644 index 0000000000..4e42640850 --- /dev/null +++ b/changelog.d/fixed-14936-lane-merge-refusal-not-applied.md @@ -0,0 +1 @@ +- Serialized merge lane: a definite GitHub refusal (403, 405, 422) of the front's merge call is now recorded as not applied, so the pull request leaves the front and the next waiter is promoted within one round instead of waiting out the front timeout; 5xx and transport errors stay uncertain ([#14936](https://github.com/hivecommons/hive/issues/14936)). diff --git a/src/pkg/github/automerge/automerge_contributor_lane.go b/src/pkg/github/automerge/automerge_contributor_lane.go index 1966b3ce75..3e543268d6 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane.go +++ b/src/pkg/github/automerge/automerge_contributor_lane.go @@ -1423,7 +1423,7 @@ func (c *Engine) trySweepContributorLanePR(ctx context.Context, displayRepo, own var apiErr error mergeResult, _, apiErr = c.gh.PullRequests.Merge(ctx, owner, repo, number, mergeMessage, mergeOpts) if apiErr != nil { - return effects.Result{}, mergeAPIErrorNotApplied(apiErr) + return effects.Result{}, hgithub.MergeAPIErrorNotApplied(apiErr) } return effects.Result{Provenance: mergeResult.GetSHA()}, nil }) diff --git a/src/pkg/github/automerge/automerge_sweep.go b/src/pkg/github/automerge/automerge_sweep.go index 8a92a0b7b5..c375f5e9e2 100644 --- a/src/pkg/github/automerge/automerge_sweep.go +++ b/src/pkg/github/automerge/automerge_sweep.go @@ -1886,7 +1886,7 @@ func (c *Engine) trySweepSelfAuthoredPR(ctx context.Context, displayRepo, owner, var apiErr error mergeResult, _, apiErr = c.gh.PullRequests.Merge(ctx, owner, repo, number, mergeMessage, mergeOpts) if apiErr != nil { - return effects.Result{}, mergeAPIErrorNotApplied(apiErr) + return effects.Result{}, hgithub.MergeAPIErrorNotApplied(apiErr) } return effects.Result{Provenance: mergeResult.GetSHA()}, nil }) @@ -2404,7 +2404,7 @@ func (c *Engine) trySweepTrustedAuthorPR(ctx context.Context, displayRepo, owner var apiErr error mergeResult, _, apiErr = c.gh.PullRequests.Merge(ctx, owner, repo, number, mergeMessage, mergeOpts) if apiErr != nil { - return effects.Result{}, mergeAPIErrorNotApplied(apiErr) + return effects.Result{}, hgithub.MergeAPIErrorNotApplied(apiErr) } return effects.Result{Provenance: mergeResult.GetSHA()}, nil }) @@ -2759,7 +2759,7 @@ func (c *Engine) trySweepQueuedPR(ctx context.Context, displayRepo, owner, repo var apiErr error mergeResult, _, apiErr = c.gh.PullRequests.Merge(ctx, owner, repo, number, mergeMessage, mergeOpts) if apiErr != nil { - return effects.Result{}, mergeAPIErrorNotApplied(apiErr) + return effects.Result{}, hgithub.MergeAPIErrorNotApplied(apiErr) } return effects.Result{Provenance: mergeResult.GetSHA()}, nil }) @@ -3152,21 +3152,6 @@ func (c *Engine) mergeQueueBaseKnown(owner, repo, base string) bool { return c.mergeQueueMemory().Required(owner+"/"+repo, base, c.clock()) } -func mergeAPIErrorNotApplied(err error) error { - if err == nil { - return nil - } - var ghErr *gh.ErrorResponse - if !errors.As(err, &ghErr) || ghErr.Response == nil { - return err - } - switch ghErr.Response.StatusCode { - case http.StatusMethodNotAllowed, http.StatusConflict, http.StatusUnprocessableEntity, http.StatusForbidden: - return effects.NotApplied(err) - } - return err -} - func (c *Engine) executeMergeEffect(ctx context.Context, claim effects.Claim, owner, repo string, number int, merge func(context.Context) (effects.Result, error)) (effects.Result, error) { out, err := effects.Execute(ctx, c.mutation, claim, merge) if !errors.Is(err, effects.ErrNeedsReconciliation) { diff --git a/src/pkg/github/merge_effects.go b/src/pkg/github/merge_effects.go new file mode 100644 index 0000000000..c2269bf63d --- /dev/null +++ b/src/pkg/github/merge_effects.go @@ -0,0 +1,28 @@ +package github + +import ( + "errors" + "net/http" + + gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/effects" +) + +// MergeAPIErrorNotApplied wraps a definite GitHub refusal of a pull request +// merge (403, 405, 409, 422) in effects.NotApplied so the mutation journal +// records NotApplied instead of an uncertain outcome. Other errors (5xx, +// transport failures) are returned unchanged and stay uncertain. +func MergeAPIErrorNotApplied(err error) error { + if err == nil { + return nil + } + var ghErr *gh.ErrorResponse + if !errors.As(err, &ghErr) || ghErr.Response == nil { + return err + } + switch ghErr.Response.StatusCode { + case http.StatusMethodNotAllowed, http.StatusConflict, http.StatusUnprocessableEntity, http.StatusForbidden: + return effects.NotApplied(err) + } + return err +} diff --git a/src/pkg/mergelane/gate.go b/src/pkg/mergelane/gate.go index 49299b7d5e..295078d2bb 100644 --- a/src/pkg/mergelane/gate.go +++ b/src/pkg/mergelane/gate.go @@ -345,7 +345,7 @@ func (g *RESTGitHub) Merge(ctx context.Context, repo string, number int, head st }, func(ctx context.Context) (effects.Result, error) { res, _, apiErr = api.PullRequests.Merge(ctx, owner, name, number, "", &gh.PullRequestOptions{SHA: head, MergeMethod: used}) if apiErr != nil { - return effects.Result{}, apiErr + return effects.Result{}, ghub.MergeAPIErrorNotApplied(apiErr) } return effects.Result{Provenance: res.GetSHA()}, nil }) diff --git a/src/pkg/mergelane/gate_test.go b/src/pkg/mergelane/gate_test.go index fd6d608ebd..89f6e966ce 100644 --- a/src/pkg/mergelane/gate_test.go +++ b/src/pkg/mergelane/gate_test.go @@ -12,6 +12,7 @@ import ( gh "github.com/google/go-github/v72/github" "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/effects" ghub "github.com/hivecommons/hive/pkg/github" ) @@ -486,3 +487,55 @@ func TestGatePreservesMergeError(t *testing.T) { t.Fatal("failed merge must retain the unmerged front") } } + +// A definite GitHub refusal of the merge call is recorded as not applied; a +// 5xx stays uncertain, and a 409 is still a moved head. +func TestRESTGitHubMergeRefusalClassification(t *testing.T) { + status := map[string]int{} + api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + if r.Method != http.MethodPut || !strings.HasSuffix(r.URL.Path, "/merge") { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message":"Not Found"}`)) + return + } + w.WriteHeader(status[r.URL.Path]) + _, _ = w.Write([]byte(`{"message":"refused by policy"}`)) + })) + defer api.Close() + client := ghub.NewClient("token", "acme", []string{"widgets"}, nil, api.URL) + g := &RESTGitHub{Client: func() *ghub.Client { return client }} + ctx := context.Background() + + for _, tc := range []struct { + code int + notApplied bool + headMoved bool + uncertain bool + }{ + {code: http.StatusForbidden, notApplied: true}, + {code: http.StatusUnprocessableEntity, notApplied: true}, + {code: http.StatusMethodNotAllowed, notApplied: true}, + {code: http.StatusConflict, headMoved: true}, + {code: http.StatusInternalServerError, uncertain: true}, + } { + // A distinct PR per case keeps each effect a new logical operation. + n := 100 + tc.code + status[fmt.Sprintf("/repos/acme/widgets/pulls/%d/merge", n)] = tc.code + _, err := g.Merge(ctx, testRepo, n, "h1") + switch { + case tc.headMoved: + if !errors.Is(err, ErrHeadMoved) { + t.Fatalf("%d: err = %v, want ErrHeadMoved", tc.code, err) + } + case tc.notApplied: + if !errors.Is(err, effects.ErrNotApplied) || !strings.Contains(err.Error(), fmt.Sprint(tc.code)) || !strings.Contains(err.Error(), "refused by policy") { + t.Fatalf("%d: err = %v, want not-applied with status and message", tc.code, err) + } + case tc.uncertain: + if err == nil || errors.Is(err, effects.ErrNotApplied) || !strings.Contains(err.Error(), "uncertain") { + t.Fatalf("%d: err = %v, want an uncertain effect", tc.code, err) + } + } + } +} diff --git a/src/pkg/mergelane/lane.go b/src/pkg/mergelane/lane.go index 775702e7eb..837e56704f 100644 --- a/src/pkg/mergelane/lane.go +++ b/src/pkg/mergelane/lane.go @@ -19,6 +19,7 @@ import ( "time" "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/effects" ghub "github.com/hivecommons/hive/pkg/github" ) @@ -175,7 +176,8 @@ type Decision struct { Reason string Position int MergeSHA string - // MergeError is the underlying failure when a merge call holds the front. + // MergeError is the underlying failure when a merge call holds the front + // or GitHub definitively refuses it. MergeError error } @@ -802,6 +804,14 @@ func (r *round) merge(head, tip string) (Decision, error) { r.front.Stage = StageValidating return r.hold(head, tip, "merge refused: head moved past the pinned SHA; validation restarts") } + if errors.Is(err, effects.ErrNotApplied) { + // GitHub definitively refused the merge, so nothing happened and + // the front need not wait out its timeout. The typed refusal still + // travels with the decision so callers can explain it. + dec, leaveErr := r.leave(head, tip, "merge refused by GitHub: "+err.Error()) + dec.MergeError = err + return dec, leaveErr + } if err != nil { r.front.Stage = StageValidating dec, holdErr := r.hold(head, tip, "merge call failed; no merge assumed this round: "+err.Error()) diff --git a/src/pkg/mergelane/scenario_test.go b/src/pkg/mergelane/scenario_test.go index 77b9b0143c..118e8e8ecc 100644 --- a/src/pkg/mergelane/scenario_test.go +++ b/src/pkg/mergelane/scenario_test.go @@ -8,6 +8,7 @@ import ( "testing" "time" + "github.com/hivecommons/hive/pkg/effects" ghub "github.com/hivecommons/hive/pkg/github" ) @@ -822,3 +823,24 @@ func TestExtendedFrontMergesWhenChecksFinish(t *testing.T) { t.Fatalf("extensions = %d, want 1", got) } } + +// A definite GitHub refusal of the front's merge (403/405/422) leaves the +// front at once, before the front timeout, and the next waiter is promoted. +func TestFrontWhoseMergeIsRefusedLeavesAndNextWaiterIsPromoted(t *testing.T) { + h := newHarness(t) + p := h.f.addPR(1, "h1") + h.f.addPR(2, "h2") + expectOutcome(t, h.acquire(1), OutcomeFront, "") + h.now = h.now.Add(time.Minute) + expectOutcome(t, h.acquire(2), OutcomeDeferred, ReasonNotAtFront) + + h.f.errs["Merge"] = effects.NotApplied(errors.New("PUT /pulls/1/merge: 403 Resource not accessible by integration []")) + expectOutcome(t, h.advance(1), OutcomeLeft, "merge refused by GitHub: ") + if p.Merged || h.frontPR() != 2 { + t.Fatalf("merged=%v front=#%d, want #1 unmerged and #2 promoted", p.Merged, h.frontPR()) + } + exit := h.record().LastExit + if exit == nil || exit.PR != 1 || !strings.Contains(exit.Reason, "403") || strings.Contains(exit.Reason, "front timeout") { + t.Fatalf("last exit = %+v, want #1 leaving with GitHub's refusal", exit) + } +} From ea5483b8ebf3daacf53597947098bdc452b18168 Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Wed, 28 Oct 2026 10:08:55 -0400 Subject: [PATCH 2/4] test(mergelane): assert a 5xx merge refusal by classification, not wording The no-op mutation boundary returns the API error unchanged, so there is no "uncertain" text to match; uncertain is defined by being neither ErrNotApplied nor ErrHeadMoved. Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- src/pkg/mergelane/gate_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pkg/mergelane/gate_test.go b/src/pkg/mergelane/gate_test.go index 89f6e966ce..59a1ddd577 100644 --- a/src/pkg/mergelane/gate_test.go +++ b/src/pkg/mergelane/gate_test.go @@ -533,7 +533,9 @@ func TestRESTGitHubMergeRefusalClassification(t *testing.T) { t.Fatalf("%d: err = %v, want not-applied with status and message", tc.code, err) } case tc.uncertain: - if err == nil || errors.Is(err, effects.ErrNotApplied) || !strings.Contains(err.Error(), "uncertain") { + // Uncertain means neither a definite refusal nor a moved head: the + // raw API error comes back so the caller holds the front. + if err == nil || errors.Is(err, effects.ErrNotApplied) || errors.Is(err, ErrHeadMoved) || !strings.Contains(err.Error(), fmt.Sprint(tc.code)) { t.Fatalf("%d: err = %v, want an uncertain effect", tc.code, err) } } From 4a648e594e46b5cdfcbdf3cfc7e84f602a1bb956 Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Wed, 28 Oct 2026 10:40:43 -0400 Subject: [PATCH 3/4] docs(mergelane): document the definitive merge-refusal immediate leave Describe in operator-reference.md that a front whose merge call GitHub refuses with 403/405/422 leaves the front at once, while 5xx/transport errors still hold it until the front timeout, and name the effects.ErrNotApplied contract on the GitHub.Merge interface so adapters and fakes stay consistent. 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> --- src/docs/operator-reference.md | 7 ++++++- src/pkg/mergelane/lane.go | 6 +++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/docs/operator-reference.md b/src/docs/operator-reference.md index 4a031d9e1b..892449bbe3 100644 --- a/src/docs/operator-reference.md +++ b/src/docs/operator-reference.md @@ -712,7 +712,12 @@ default, restarted on every branch update, and extended up to 4x while the required checks are only queued or in progress) is the fallback for a front whose checks never finish; its reason says what the lane last knew, for example that the head after the branch update was never evaluated, or the -last recorded waiting reason. +last recorded waiting reason. A front whose merge call GitHub definitively +refuses (HTTP 403, 405 or 422, for example a merge method the repository +does not allow) also leaves at once with the reason `merge refused by +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 that GitHub still reports as `blocked` after its head contains the current target tip and all required checks pass also leaves immediately. The diff --git a/src/pkg/mergelane/lane.go b/src/pkg/mergelane/lane.go index 837e56704f..cc0e38a3f0 100644 --- a/src/pkg/mergelane/lane.go +++ b/src/pkg/mergelane/lane.go @@ -99,7 +99,11 @@ type GitHub interface { IssueCommentsContain(ctx context.Context, repo string, number int, needle string) (bool, error) CreateIssueComment(ctx context.Context, repo string, number int, body string) error // Merge is the REST merge with head pinned, no admin bypass; it returns - // the merge commit SHA. + // the merge commit SHA. On a 409 head mismatch it returns ErrHeadMoved. + // When GitHub definitively refuses the merge (403/405/422) it returns an + // error wrapping effects.ErrNotApplied so the lane can let the front + // leave at once; any other error (5xx, transport) is reported as-is and + // the front keeps waiting. Merge(ctx context.Context, repo string, number int, head string) (string, error) CommitParents(ctx context.Context, repo, sha string) ([]string, error) PullRequestsForCommit(ctx context.Context, repo, sha string) ([]int, error) From dfcb8736e3b02ecf1be92cbe412f61ab697e0c2e Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Wed, 28 Oct 2026 10:53:36 -0400 Subject: [PATCH 4/4] test(mergelane): a refused merge still carries its typed error out of the lane The serialized contributor lane reads the decision's MergeError to park owner-approved workflow PRs on a missing App permission. A definite refusal now leaves the front, so the error has to travel with that leave. Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- src/pkg/mergelane/scenario_test.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/pkg/mergelane/scenario_test.go b/src/pkg/mergelane/scenario_test.go index 118e8e8ecc..db727a29f0 100644 --- a/src/pkg/mergelane/scenario_test.go +++ b/src/pkg/mergelane/scenario_test.go @@ -834,8 +834,13 @@ func TestFrontWhoseMergeIsRefusedLeavesAndNextWaiterIsPromoted(t *testing.T) { h.now = h.now.Add(time.Minute) expectOutcome(t, h.acquire(2), OutcomeDeferred, ReasonNotAtFront) - h.f.errs["Merge"] = effects.NotApplied(errors.New("PUT /pulls/1/merge: 403 Resource not accessible by integration []")) - expectOutcome(t, h.advance(1), OutcomeLeft, "merge refused by GitHub: ") + refusal := effects.NotApplied(errors.New("PUT /pulls/1/merge: 403 Resource not accessible by integration []")) + h.f.errs["Merge"] = refusal + dec := h.advance(1) + expectOutcome(t, dec, OutcomeLeft, "merge refused by GitHub: ") + if !errors.Is(dec.MergeError, refusal) { + t.Fatalf("merge error = %v, want the typed GitHub refusal carried out of the lane", dec.MergeError) + } if p.Merged || h.frontPR() != 2 { t.Fatalf("merged=%v front=#%d, want #1 unmerged and #2 promoted", p.Merged, h.frontPR()) }