From 6bf8f22572445475bd31e48816049ff15b8c2651 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Sat, 31 Oct 2026 10:52:17 -0400 Subject: [PATCH 1/6] =?UTF-8?q?=F0=9F=90=9B=20fix(automerge):=20prepare=20?= =?UTF-8?q?independent=20ready=20heads=20without=20FIFO=20stalls?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andrew Anderson --- .../fixed-readiness-first-preparation.md | 1 + docs/readiness-preparation.md | 44 ++++ src/cmd/hive/boot_agents_deps.go | 3 + .../automerge/automerge_base_preparation.go | 87 +++++++- .../automerge_base_preparation_test.go | 56 ++++- .../automerge/automerge_contributor_lane.go | 8 + .../automerge/automerge_current_base_test.go | 4 + .../automerge/automerge_destination_merge.go | 48 +++++ .../automerge_preparation_readiness.go | 200 ++++++++++++++++++ .../automerge_preparation_readiness_test.go | 196 +++++++++++++++++ .../automerge_preparation_terminal_test.go | 32 +++ .../automerge/automerge_sweep_guards_test.go | 5 +- .../automerge/automerge_sweep_queued.go | 10 +- .../github/automerge/automerge_sweep_test.go | 2 +- .../automerge/automerge_sweep_trusted.go | 6 +- .../automerge_sweep_trusted_author_test.go | 4 +- .../automerge/base_preparation_store.go | 18 +- src/pkg/github/destination_merge.go | 112 ++++++++++ src/pkg/github/destination_merge_test.go | 84 ++++++++ src/pkg/github/issue_dependencies.go | 21 ++ src/pkg/github/merge_request_watcher.go | 14 ++ src/pkg/github/pullrequest.go | 15 ++ .../github/pullrequest_merge_method_test.go | 1 + 23 files changed, 936 insertions(+), 35 deletions(-) create mode 100644 changelog.d/fixed-readiness-first-preparation.md create mode 100644 docs/readiness-preparation.md create mode 100644 src/pkg/github/automerge/automerge_destination_merge.go create mode 100644 src/pkg/github/automerge/automerge_preparation_readiness.go create mode 100644 src/pkg/github/automerge/automerge_preparation_readiness_test.go create mode 100644 src/pkg/github/automerge/automerge_preparation_terminal_test.go create mode 100644 src/pkg/github/destination_merge.go create mode 100644 src/pkg/github/destination_merge_test.go diff --git a/changelog.d/fixed-readiness-first-preparation.md b/changelog.d/fixed-readiness-first-preparation.md new file mode 100644 index 0000000000..09fd1d7770 --- /dev/null +++ b/changelog.d/fixed-readiness-first-preparation.md @@ -0,0 +1 @@ +- Allow independently proven, CI-ready pull requests to prepare past a completed refresh awaiting CI, without displacing unresolved mutations or bypassing dependency, review, fresh-base, or serialized-merge gates. diff --git a/docs/readiness-preparation.md b/docs/readiness-preparation.md new file mode 100644 index 0000000000..b1ce4775f9 --- /dev/null +++ b/docs/readiness-preparation.md @@ -0,0 +1,44 @@ +# Readiness-first preparation + +Preparation admission is not merge approval. A caller must pass its normal +lane gates on the exact head before requesting a base refresh. An unresolved +refresh, ambiguous mutation outcome, or active repair retains exclusive +preparation ownership. + +A completed refresh can yield while its new head has a known pending CI +verdict. Completion requires a changed live head containing both the pinned +pre-refresh head and the authoritative refresh-target base commit. Legacy +attempts without authoritative base evidence remain exclusive. The former +owner keeps its ticket, original priority, and head/base attempt deduplication; +pending CI/review observations do not erase that ticket or trigger another +refresh. Once ready, it cannot be overtaken by another caller. + +Overtaking requires complete changed-file evidence, including rename sources, +disjoint closing claims, explicit native issue dependency summaries reporting +no open blockers, and divergent head ancestry. An extra unclassified reference, +shared claim, supersession, stacked head, incomplete pagination, changed +metadata, missing required evidence, or failed read preserves FIFO. File +disjointness alone is not independence. This is a conservative rule using +repository-declared relationships, not semantic inference across packages. +At most three predecessors are checked for overtaking in one admission; +larger or ambiguous fronts retain FIFO instead of spending unbounded API work. + +Final sweep admission and merge calls share a per-repository/base guard with +the GitHub merge-request relay. Production uses stable flock inodes under +`/data/destination-merges`, shared by writers using the same durable volume; +isolated test engines use process-local locks. Contention parks a caller +rather than allowing it to retain stale evidence while waiting. Locks are +released on return or process death, and retargeting cannot borrow another +destination's lease. No new parallel merge dispatch is introduced. + +The guard serializes Hive writers sharing that volume, not independent human +or external GitHub clients. GitHub still performs atomic protected-base +updates and enforces configured checks. Its REST merge API has an expected +head condition, not an expected base condition: universal serialization +against external writers requires server-side up-to-date protection or a +merge queue. This change does not alter those policies. + +Budgets, runner concurrency, review authority, merge methods, and sweep cadence +are unchanged. Admission reads remain paced by existing GitHub API controls. +Fixtures and CI can establish correctness, but deployed before/after +PR-to-merge measurements are still required to establish latency improvement. diff --git a/src/cmd/hive/boot_agents_deps.go b/src/cmd/hive/boot_agents_deps.go index 98fd2d8914..ab22aa9f79 100644 --- a/src/cmd/hive/boot_agents_deps.go +++ b/src/cmd/hive/boot_agents_deps.go @@ -4,6 +4,7 @@ import ( "context" "log/slog" "os" + "path/filepath" "github.com/hivecommons/hive/crux/api/callback" @@ -15,6 +16,7 @@ import ( "github.com/hivecommons/hive/pkg/github" "github.com/hivecommons/hive/pkg/github/automerge" "github.com/hivecommons/hive/pkg/github/requestwatch" + "github.com/hivecommons/hive/pkg/stagelabels" ) // bootAgentsDeps carries the effects of bootAgents that outlive the call @@ -98,6 +100,7 @@ func defaultBootAgentsDeps() bootAgentsDeps { github.StartTokenAccessAuditWatcher(ctx, logger) }, startRequestRelays: func(ctx context.Context, c *github.Client, r requestRelays) <-chan struct{} { + github.ConfigureDestinationMerges(filepath.Join(stagelabels.DefaultStateDir, "destination-merges")) if r.mergePolicy != nil { c.SetMergeRequestPolicyGate(r.mergePolicy(c)) } diff --git a/src/pkg/github/automerge/automerge_base_preparation.go b/src/pkg/github/automerge/automerge_base_preparation.go index 1fa2830ea4..69ec41b172 100644 --- a/src/pkg/github/automerge/automerge_base_preparation.go +++ b/src/pkg/github/automerge/automerge_base_preparation.go @@ -63,10 +63,13 @@ func (c *Engine) preparationInvalid(ctx context.Context, owner, repo, base strin if err != nil { return "", err } + if why := c.preparationInvalidPR(pr, base, repair); why != "" { + return why, nil + } if expectedHead != "" && pr.GetHead().GetSHA() != expectedHead { return "head-changed-before-preparation", nil } - return c.preparationInvalidPR(pr, base, repair), nil + return "", nil } func (c *Engine) preparationInvalidPR(pr *gh.PullRequest, base string, repair bool) string { @@ -125,24 +128,69 @@ func (c *Engine) admitBasePreparation(ctx context.Context, owner, repo, base str } else { st.Waiting[i].Head = head } + candidate, _, err := c.gh.PullRequests.Get(ctx, owner, repo, number) + if err != nil { + return err + } + if candidate.GetHead().GetSHA() != head || c.preparationInvalidPR(candidate, base, repair) != "" { + reason = basePreparationPrefix + "snapshot-changed" + return nil + } + var yielded preparationCandidate + if st.Owner.Number != 0 { + front, _, err := c.gh.PullRequests.Get(ctx, owner, repo, st.Owner.Number) + if err != nil { + return err + } + if st.Owner.Repair || repair || !c.preparationCompleted(ctx, owner, repo, front, st.Attempts[st.Owner.Number]) || + !c.preparationMayPass(ctx, owner, repo, base, candidate, front, lane, st.Owner.Lane) { + reason = preparationQueued(st.Owner.Number) + return nil + } + yielded = st.Owner + ticket := yielded + ticket.Head = front.GetHead().GetSHA() + ticket.Yielded = true + st.Waiting = append([]preparationCandidate{ticket}, st.Waiting...) + st.Owner = preparationCandidate{} + } if st.Owner.Number == 0 { - for len(st.Waiting) > 0 { - next := st.Waiting[0] + passed := 0 + for index := 0; index < len(st.Waiting); { + next := st.Waiting[index] why, err := c.preparationInvalid(ctx, owner, repo, base, next.Number, next.Head, next.Repair) if err != nil { return err } - if why != "" || now.Sub(next.Since) >= c.preparationWindow() { - st.Waiting = st.Waiting[1:] + if why == "head-changed-before-preparation" && next.Yielded { + reason = preparationQueued(next.Number) + break + } + if why != "" || (!next.Yielded && now.Sub(next.Since) >= c.preparationWindow()) { + st.Waiting = slices.Delete(st.Waiting, index, index+1) continue } if next.Number != number { - // Only the caller has just passed its lane's gates. - // Never turn an old FIFO ticket into a sticky owner. + if passed >= preparationMaxPredecessors { + reason = preparationQueued(next.Number) + break + } + front, _, err := c.gh.PullRequests.Get(ctx, owner, repo, next.Number) + if err != nil { + return err + } + if !repair && !next.Repair && c.preparationMayPass(ctx, owner, repo, base, candidate, front, lane, next.Lane) { + passed++ + index++ + continue + } + if yielded.Number != 0 && st.Owner.Number == number { + c.info("automerge base preparation completed owner yielded", "repo", owner+"/"+repo, "base", base, "pr", number, "former_owner", yielded.Number, "former_since", yielded.Since) + } reason = preparationQueued(next.Number) break } - st.Waiting = st.Waiting[1:] + st.Waiting = slices.Delete(st.Waiting, index, index+1) next.Since = now next.Lane = lane next.Repair = repair @@ -151,6 +199,12 @@ func (c *Engine) admitBasePreparation(ctx context.Context, owner, repo, base str break } } + if yielded.Number != 0 && st.Owner.Number != number { + // A later ambiguous predecessor prevented admission; do not + // relinquish a completed owner's durable priority for nothing. + st.Owner = yielded + st.Waiting = slices.DeleteFunc(st.Waiting, func(w preparationCandidate) bool { return w.Number == yielded.Number }) + } if st.Owner.Number != number { if reason == "" { reason = preparationQueued(st.Owner.Number) @@ -175,7 +229,12 @@ func (c *Engine) claimBasePreparationUpdate(ctx context.Context, owner, repo str if err != nil { return err } - base, baseSHA := pr.GetBase().GetRef(), pr.GetBase().GetSHA() + base := pr.GetBase().GetRef() + branch, _, err := c.gh.Repositories.GetBranch(ctx, owner, repo, base, 0) + if err != nil { + return err + } + baseSHA := branch.GetCommit().GetSHA() if head == "" || pr.GetHead().GetSHA() != head || base == "" || baseSHA == "" { return &preparationWait{basePreparationPrefix + "snapshot-changed"} } @@ -197,7 +256,7 @@ func (c *Engine) claimBasePreparationUpdate(ctx context.Context, owner, repo str wait = basePreparationReady return nil } - st.Attempts[number] = preparationAttempt{Head: head, Base: baseSHA, At: c.now()} + st.Attempts[number] = preparationAttempt{Head: head, Base: baseSHA, At: c.now(), AuthoritativeBase: true} c.info("automerge base preparation refresh claimed", "repo", owner+"/"+repo, "base", base, "pr", number, "head", head, "base_sha", baseSHA) return nil }) @@ -259,7 +318,15 @@ func (c *Engine) observeBasePreparation(lane, owner, repo string, pr *gh.PullReq } // Waiting is not preparation yet: a changed or now-pending waiting // head loses its admission ticket and must qualify again. + for i := range st.Waiting { + if st.Waiting[i].Yielded && st.Waiting[i].Number == pr.GetNumber() && keep { + st.Waiting[i].Head = pr.GetHead().GetSHA() + } + } st.Waiting = slices.DeleteFunc(st.Waiting, func(w preparationCandidate) bool { + if w.Yielded && keep && !merged { + return false + } return w.Lane == lane && w.Number == pr.GetNumber() && (w.Head != pr.GetHead().GetSHA() || !strings.HasPrefix(reason, basePreparationPrefix)) }) if !keep && st.Owner.Lane == lane && st.Owner.Number == pr.GetNumber() { diff --git a/src/pkg/github/automerge/automerge_base_preparation_test.go b/src/pkg/github/automerge/automerge_base_preparation_test.go index b206617020..5794e2d91c 100644 --- a/src/pkg/github/automerge/automerge_base_preparation_test.go +++ b/src/pkg/github/automerge/automerge_base_preparation_test.go @@ -19,17 +19,25 @@ import ( ) type preparationAPI struct { - mu sync.Mutex - prs map[int]*gh.PullRequest - behind map[string]int - pending map[string]bool - updates map[int]int - merges map[int]int + mu sync.Mutex + prs map[int]*gh.PullRequest + behind map[string]int + pending map[string]bool + updates map[int]int + merges map[int]int + files map[int][]*gh.CommitFile + baseSHA string + dependencyMissing bool + dependencyError bool + dependencyBlocked bool + truncated bool + onFiles func(*preparationAPI, int) } func preparationPR(number int, base, head string) *gh.PullRequest { return &gh.PullRequest{ Number: gh.Ptr(number), State: gh.Ptr("open"), Title: gh.Ptr("prepare fixture"), + Body: gh.Ptr(fmt.Sprintf("Closes #%d", number+100)), ChangedFiles: gh.Ptr(1), Mergeable: gh.Ptr(true), MergeableState: gh.Ptr("clean"), User: &gh.User{Login: gh.Ptr(testHiveAppBotLogin)}, Head: &gh.PullRequestBranch{SHA: gh.Ptr(head), Repo: &gh.Repository{FullName: gh.Ptr("acme/widget")}}, @@ -42,11 +50,13 @@ func newPreparationAPI(t *testing.T) (*preparationAPI, *httptest.Server) { api := &preparationAPI{ prs: map[int]*gh.PullRequest{}, behind: map[string]int{}, pending: map[string]bool{}, updates: map[int]int{}, merges: map[int]int{}, + files: map[int][]*gh.CommitFile{}, baseSHA: "base1", } for n := 1; n <= 3; n++ { head := fmt.Sprintf("head%d", n) api.prs[n] = preparationPR(n, "main", head) api.behind[head] = 1 + api.files[n] = []*gh.CommitFile{{Filename: gh.Ptr(fmt.Sprintf("file%d.go", n))}} } srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { api.mu.Lock() @@ -83,6 +93,14 @@ func newPreparationAPI(t *testing.T) (*preparationAPI, *httptest.Server) { write(map[string]string{"message": "Updating pull request branch"}) case "reviews": write([]any{}) + case "files": + if api.onFiles != nil { + api.onFiles(api, n) + } + if api.truncated { + w.Header().Set("Link", `; rel="next"`) + } + write(api.files[n]) case "merge": api.merges[n]++ write(map[string]any{"merged": true, "sha": "merged"}) @@ -91,14 +109,34 @@ func newPreparationAPI(t *testing.T) (*preparationAPI, *httptest.Server) { w.WriteHeader(http.StatusNotFound) } case "compare": - _, head, _ := strings.Cut(path[4], "...") - write(map[string]any{"behind_by": api.behind[head], "base_commit": map[string]string{"sha": "base1"}}) + base, head, _ := strings.Cut(path[4], "...") + status := "diverged" + if (base == "head1" || base == "base1") && head == "refreshed1" || + (base == "head2" || base == "base1" || base == "base2") && head == "refreshed2" { + status = "ahead" + } + write(map[string]any{"status": status, "behind_by": api.behind[head], "base_commit": map[string]string{"sha": api.baseSHA}}) + case "issues": + if api.dependencyError { + w.WriteHeader(http.StatusServiceUnavailable) + return + } + n, _ := strconv.Atoi(path[4]) + payload := map[string]any{"number": n, "state": "open"} + if !api.dependencyMissing { + blockers := 0 + if api.dependencyBlocked { + blockers = 1 + } + payload["issue_dependencies_summary"] = map[string]int{"blocked_by": blockers} + } + write(payload) case "branches": if len(path) > 5 { w.WriteHeader(http.StatusNotFound) write(map[string]string{"message": "Branch not protected"}) } else { - write(map[string]any{"name": path[4], "commit": map[string]string{"sha": "base1"}}) + write(map[string]any{"name": path[4], "commit": map[string]string{"sha": api.baseSHA}}) } case "rules": write([]any{}) diff --git a/src/pkg/github/automerge/automerge_contributor_lane.go b/src/pkg/github/automerge/automerge_contributor_lane.go index 3de8b5d1ea..269f2bce89 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane.go +++ b/src/pkg/github/automerge/automerge_contributor_lane.go @@ -1486,10 +1486,18 @@ func (c *Engine) evaluateContributorLanePR(ctx context.Context, displayRepo, own } func (c *Engine) trySweepContributorLanePR(ctx context.Context, displayRepo, owner, repo string, number int, policy ContributorLanePolicy) (AutoMergeSweepEvent, string, error) { + ctx, unlock, err := c.acquireDestinationMerge(ctx, owner, repo, number) + if err != nil { + return AutoMergeSweepEvent{}, basePreparationPrefix + "merge-guard", err + } + defer unlock() eval, reason, err := c.evaluateContributorLanePR(ctx, displayRepo, owner, repo, number, policy, false) if err != nil || reason != "" { return AutoMergeSweepEvent{}, reason, err } + if !hgithub.DestinationMergeHeld(ctx, destinationDirectory(c), owner+"/"+repo, eval.pr.GetBase().GetRef()) { + return AutoMergeSweepEvent{}, basePreparationPrefix + "snapshot-changed", nil + } author, headSHA, claim, verdict, method, mergeClaim := eval.author, eval.headSHA, eval.claim, eval.verdict, eval.method, eval.mergeClaim fullRepo := owner + "/" + repo if hold := mergeTrainCandidateHold(ctx); hold != "" { diff --git a/src/pkg/github/automerge/automerge_current_base_test.go b/src/pkg/github/automerge/automerge_current_base_test.go index 59838049e1..43c83a00bb 100644 --- a/src/pkg/github/automerge/automerge_current_base_test.go +++ b/src/pkg/github/automerge/automerge_current_base_test.go @@ -50,6 +50,8 @@ func TestCurrentBaseGate(t *testing.T) { return } _ = json.NewEncoder(w).Encode(map[string]any{"behind_by": tc.behindBy, "ahead_by": 1}) + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/branches/main": + _ = json.NewEncoder(w).Encode(map[string]any{"name": "main", "commit": map[string]string{"sha": "base1"}}) case r.Method == http.MethodPut && r.URL.Path == "/repos/acme/widget/pulls/7/update-branch": updates++ var body map[string]any @@ -121,6 +123,8 @@ func TestCurrentBaseGateUpdateUnavailableFallback(t *testing.T) { _ = json.NewEncoder(w).Encode(map[string]any{"number": 7, "state": "open", "head": map[string]string{"sha": head}, "base": map[string]string{"ref": "main", "sha": "base1"}}) case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/compare/main..."+head: _ = json.NewEncoder(w).Encode(map[string]any{"behind_by": 2, "ahead_by": 1}) + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/branches/main": + _ = json.NewEncoder(w).Encode(map[string]any{"name": "main", "commit": map[string]string{"sha": "base1"}}) case r.Method == http.MethodPut && r.URL.Path == "/repos/acme/widget/pulls/7/update-branch": w.WriteHeader(tc.updateStatus) _, _ = w.Write([]byte(tc.updateBody)) diff --git a/src/pkg/github/automerge/automerge_destination_merge.go b/src/pkg/github/automerge/automerge_destination_merge.go new file mode 100644 index 0000000000..796f93683a --- /dev/null +++ b/src/pkg/github/automerge/automerge_destination_merge.go @@ -0,0 +1,48 @@ +package automerge + +import ( + "context" + "errors" + "path/filepath" + + gh "github.com/google/go-github/v72/github" + hgithub "github.com/hivecommons/hive/pkg/github" +) + +func (c *Engine) acquireDestinationMerge(ctx context.Context, owner, repo string, number int) (context.Context, func(), error) { + pr, _, err := c.gh.PullRequests.Get(hgithub.WithRESTCaller(ctx, "hive:destination_merge"), owner, repo, number) + if err != nil { + return ctx, nil, err + } + dir := "" + if stateDir := c.preparationStore().dir; stateDir != "" { + dir = filepath.Join(filepath.Dir(stateDir), "destination-merges") + } + return hgithub.AcquireDestinationMerge(ctx, dir, owner+"/"+repo, pr.GetBase().GetRef()) +} + +func (c *Engine) guardedPR(ctx context.Context, owner, repo string, number int) (*gh.PullRequest, context.Context, func(), error) { + guarded, unlock, err := c.acquireDestinationMerge(ctx, owner, repo, number) + if err != nil { + return nil, ctx, nil, err + } + pr, _, err := c.gh.PullRequests.Get(hgithub.WithRESTCaller(guarded, "hive:automerge_sweep"), owner, repo, number) + if err != nil { + unlock() + return nil, ctx, nil, err + } + // Reacquiring a different destination would let a retargeted PR escape + // the guard. Verify the fresh destination against the held lease instead. + if !hgithub.DestinationMergeHeld(guarded, destinationDirectory(c), owner+"/"+repo, pr.GetBase().GetRef()) { + unlock() + return nil, ctx, nil, errors.New("merge destination changed while acquiring guard") + } + return pr, guarded, unlock, nil +} + +func destinationDirectory(c *Engine) string { + if dir := c.preparationStore().dir; dir != "" { + return filepath.Join(filepath.Dir(dir), "destination-merges") + } + return "" +} diff --git a/src/pkg/github/automerge/automerge_preparation_readiness.go b/src/pkg/github/automerge/automerge_preparation_readiness.go new file mode 100644 index 0000000000..06c7b49cd6 --- /dev/null +++ b/src/pkg/github/automerge/automerge_preparation_readiness.go @@ -0,0 +1,200 @@ +package automerge + +import ( + "context" + "fmt" + "regexp" + "slices" + "strconv" + "strings" + "time" + + gh "github.com/google/go-github/v72/github" + hgithub "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/stageengine" +) + +const preparationMaxPredecessors = 3 + +var preparationReference = regexp.MustCompile(`#([1-9][0-9]*)|/(pull|issues)/([1-9][0-9]*)`) + +// Missing evidence is different from a known pending verdict. Only the latter +// can yield priority to an independently admitted successor. +func (c *Engine) preparationReadiness(ctx context.Context, owner, repo string, pr *gh.PullRequest, lane string) (ready, known bool) { + if lane != stageLaneSelf && lane != stageLaneTrusted { + return false, false + } + green, reason, err := c.commitGreenForPR(ctx, owner, repo, pr.GetBase().GetRef(), pr.GetHead().GetSHA(), pr.GetNumber(), time.Time{}, newExpectedCheckCache()) + if err != nil { + return false, false + } + if !green { + return false, stageengine.ClassifyCIReason(reason) == stageengine.CIReasonPending && strings.HasPrefix(reason, "required-check-pending:") + } + if gate := c.currentReviewGate(); gate != nil && gate.effectiveMode() == ReviewGateModeEnforce { + if gate.Verdict == nil { + return false, false + } + verdict := gate.Verdict(owner+"/"+repo, pr.GetNumber(), pr.GetHead().GetSHA()) + if !verdict.Loaded || !verdict.Found { + return false, false + } + return verdict.Approved, true + } + return true, true +} + +func (c *Engine) preparationCompleted(ctx context.Context, owner, repo string, pr *gh.PullRequest, attempt preparationAttempt) bool { + if !attempt.AuthoritativeBase || attempt.Head == "" || attempt.Base == "" || pr.GetHead().GetSHA() == attempt.Head { + return false + } + for _, ancestor := range []string{attempt.Head, attempt.Base} { + cmp, _, err := c.gh.Repositories.CompareCommits(ctx, owner, repo, ancestor, pr.GetHead().GetSHA(), &gh.ListOptions{PerPage: 1}) + if err != nil || (cmp.GetStatus() != "ahead" && cmp.GetStatus() != "identical") { + return false + } + } + return true +} + +func (c *Engine) preparationFiles(ctx context.Context, owner, repo string, pr *gh.PullRequest) ([]string, error) { + if pr.GetChangedFiles() <= 0 { + return nil, fmt.Errorf("unknown changed-file count") + } + var files []string + count := 0 + opts := &gh.ListOptions{PerPage: 100} + for page := 0; page < mergeTrainFilesPages; page++ { + list, resp, err := c.gh.PullRequests.ListFiles(ctx, owner, repo, pr.GetNumber(), opts) + if err != nil { + return nil, err + } + for _, f := range list { + if f.GetFilename() == "" { + return nil, fmt.Errorf("unknown changed path") + } + count++ + files = append(files, f.GetFilename()) + if f.GetPreviousFilename() != "" { + files = append(files, f.GetPreviousFilename()) + } + } + if resp == nil || resp.NextPage == 0 { + if count != pr.GetChangedFiles() { + return nil, fmt.Errorf("incomplete changed paths") + } + return files, nil + } + opts.Page = resp.NextPage + } + return nil, fmt.Errorf("changed paths exceed bounded pagination") +} + +func preparationClaims(pr *gh.PullRequest, fullRepo string) ([]int, bool) { + text := pr.GetTitle() + "\n" + pr.GetBody() + claims := hgithub.ParseClaimedIssues(text, fullRepo) + if len(claims) == 0 || len(hgithub.ParseReferencedIssues(text, fullRepo)) != 0 { + return nil, false + } + var numbers []int + for _, ref := range claims { + if ref.Repo != "" && !strings.EqualFold(ref.Repo, fullRepo) { + return nil, false + } + numbers = append(numbers, ref.Issue) + } + // An additional reference is ambiguous, not an inferred dependency. + // The existing native issue graph must explain the relationships. + for _, match := range preparationReference.FindAllStringSubmatch(text, -1) { + n, _ := strconv.Atoi(match[1]) + if match[2] == "issues" { + n, _ = strconv.Atoi(match[3]) + } + if match[2] == "pull" || !slices.Contains(numbers, n) { + return nil, false + } + } + return numbers, true +} + +func (c *Engine) preparationIndependent(ctx context.Context, owner, repo string, a, b *gh.PullRequest) bool { + fullRepo := owner + "/" + repo + if mergeTrainAdoptionPair(a, b) || a.GetHead().GetSHA() == b.GetHead().GetSHA() { + return false + } + ac, ak := preparationClaims(a, fullRepo) + bc, bk := preparationClaims(b, fullRepo) + if !ak || !bk { + return false + } + for _, n := range ac { + if slices.Contains(bc, n) { + return false + } + } + for _, n := range append(ac, bc...) { + blocked, known, err := hgithub.ReadIssueDependencyEvidence(ctx, c.gh, owner, repo, n) + if err != nil || !known || blocked { + return false + } + } + af, err := c.preparationFiles(ctx, owner, repo, a) + if err != nil { + return false + } + bf, err := c.preparationFiles(ctx, owner, repo, b) + if err != nil { + return false + } + for _, file := range af { + if slices.Contains(bf, file) { + return false + } + } + cmp, _, err := c.gh.Repositories.CompareCommits(ctx, owner, repo, a.GetHead().GetSHA(), b.GetHead().GetSHA(), &gh.ListOptions{PerPage: 1}) + return err == nil && cmp.GetStatus() == "diverged" +} + +func (c *Engine) preparationMayPass(ctx context.Context, owner, repo, base string, candidate, predecessor *gh.PullRequest, callerLane, lane string) bool { + tip, _, err := c.gh.Repositories.GetBranch(ctx, owner, repo, base, 0) + if err != nil || tip.GetCommit().GetSHA() == "" { + return false + } + candidateReady, candidateKnown := c.preparationReadiness(ctx, owner, repo, candidate, callerLane) + if !candidateReady || !candidateKnown { + return false + } + ready, known := c.preparationReadiness(ctx, owner, repo, predecessor, lane) + if !known || ready || !c.preparationIndependent(ctx, owner, repo, candidate, predecessor) { + return false + } + // Paths and dependency reads are not a head/base admission proof. + // Recheck both PR payloads, including mutable bodies and labels. + for _, before := range []*gh.PullRequest{candidate, predecessor} { + after, _, err := c.gh.PullRequests.Get(ctx, owner, repo, before.GetNumber()) + if err != nil || after.GetHead().GetSHA() != before.GetHead().GetSHA() || + after.GetBody() != before.GetBody() || after.GetTitle() != before.GetTitle() || + after.GetChangedFiles() != before.GetChangedFiles() || !slices.Equal(labelNames(after.Labels), labelNames(before.Labels)) || + c.preparationInvalidPR(after, base, false) != "" { + return false + } + } + ready, known = c.preparationReadiness(ctx, owner, repo, predecessor, lane) + if !known || ready { + return false + } + ac, ak := preparationClaims(candidate, owner+"/"+repo) + bc, bk := preparationClaims(predecessor, owner+"/"+repo) + if !ak || !bk { + return false + } + for _, n := range append(ac, bc...) { + blocked, known, err := hgithub.ReadIssueDependencyEvidence(ctx, c.gh, owner, repo, n) + if err != nil || !known || blocked { + return false + } + } + candidateReady, candidateKnown = c.preparationReadiness(ctx, owner, repo, candidate, callerLane) + liveTip, _, err := c.gh.Repositories.GetBranch(ctx, owner, repo, base, 0) + return candidateReady && candidateKnown && err == nil && liveTip.GetCommit().GetSHA() == tip.GetCommit().GetSHA() +} diff --git a/src/pkg/github/automerge/automerge_preparation_readiness_test.go b/src/pkg/github/automerge/automerge_preparation_readiness_test.go new file mode 100644 index 0000000000..42e527b876 --- /dev/null +++ b/src/pkg/github/automerge/automerge_preparation_readiness_test.go @@ -0,0 +1,196 @@ +package automerge + +import ( + "context" + "errors" + "path/filepath" + "sync" + "testing" + + gh "github.com/google/go-github/v72/github" + hgithub "github.com/hivecommons/hive/pkg/github" +) + +func readinessFixture(t *testing.T, durable bool) (*preparationAPI, *Engine, string) { + t.Helper() + api, srv := newPreparationAPI(t) + dir := "" + if durable { + dir = filepath.Join(t.TempDir(), "preparation") + } + + c := preparationEngine(srv.URL, NewBasePreparationStore(dir)) + c.requiredChecksForRepo = func(string) (map[string]bool, bool) { return map[string]bool{"build": true}, true } + if ev, _, _, err := preparationGate(c, 1, "main", "head1"); err != nil || !ev.BranchUpdated { + t.Fatalf("initial refresh: %+v %v", ev, err) + } + api.prs[1].Head.SHA = gh.Ptr("refreshed1") + api.pending["refreshed1"] = true + api.behind["refreshed1"] = 1 + api.baseSHA = "base2" + return api, c, srv.URL +} + +func TestPreparationCompletedOwnerYieldsAndSurvivesRestart(t *testing.T) { + api, c, url := readinessFixture(t, true) + if ev, reason, _, err := preparationGate(c, 2, "main", "head2"); err != nil || !ev.BranchUpdated { + t.Fatalf("ready independent successor: %+v %q %v", ev, reason, err) + } + st, err := c.preparationStore().ownerSnapshot(preparationKey("acme", "widget", "main")) + if err != nil || st.Owner.Number != 2 || len(st.Waiters) != 1 || st.Waiters[0].Number != 1 || !st.Waiters[0].Yielded { + t.Fatalf("yield lost priority: %+v %v", st, err) + } + restarted := preparationEngine(url, NewBasePreparationStore(c.preparationStore().dir)) + restarted.requiredChecksForRepo = c.requiredChecksForRepo + for i := 0; i < 3; i++ { + if _, reason, _, err := preparationGate(restarted, 1, "main", "refreshed1"); err != nil || reason != preparationQueued(2) { + t.Fatalf("unresolved second refresh: %q %v", reason, err) + } + restarted.observeBasePreparation(stageLaneSelf, "acme", "widget", api.prs[1], "required-check-pending:build", false, nil) + } + if api.updates[1] != 1 || api.updates[2] != 1 { + t.Fatalf("pending-head refresh stampede: %v", api.updates) + } + st, _ = restarted.preparationStore().ownerSnapshot(preparationKey("acme", "widget", "main")) + if len(st.Waiters) != 1 || !st.Waiters[0].Yielded { + t.Fatalf("pending observation erased former owner: %+v", st) + } + api.prs[2].Head.SHA = gh.Ptr("refreshed2") + api.pending["refreshed2"] = true + api.pending["refreshed1"] = false + if _, reason, _, err := preparationGate(restarted, 3, "main", "head3"); err != nil || reason != preparationQueued(1) { + t.Fatalf("ready former owner was overtaken: %q %v", reason, err) + } + if ev, reason, _, err := preparationGate(restarted, 1, "main", "refreshed1"); err != nil || !ev.BranchUpdated { + t.Fatalf("ready former owner could not regain priority: %+v %q %v", ev, reason, err) + } +} + +func TestPreparationCannotYieldOnUnsafeOrMissingEvidence(t *testing.T) { + for _, blocker := range []string{"unresolved", "repair", "legacy-attempt", "dependency", "missing-dependency", "unreadable-dependency", "late-dependency", "shared-claim", "reference", "stack", "pagination", "incomplete-count", "rename", "head-moved", "base-moved", "candidate-ci", "candidate-review"} { + t.Run(blocker, func(t *testing.T) { + api, c, _ := readinessFixture(t, false) + switch blocker { + case "unresolved": + api.prs[1].Head.SHA = gh.Ptr("head1") + case "repair", "legacy-attempt": + _ = c.preparationStore().change(preparationKey("acme", "widget", "main"), func(st *basePreparation) error { + if blocker == "repair" { + st.Owner.Repair = true + } else { + attempt := st.Attempts[1] + attempt.AuthoritativeBase = false + st.Attempts[1] = attempt + } + + return nil + }) + case "dependency": + api.dependencyBlocked = true + case "missing-dependency": + api.dependencyMissing = true + case "unreadable-dependency": + api.dependencyError = true + case "late-dependency": + api.onFiles = func(a *preparationAPI, _ int) { a.dependencyBlocked = true } + case "shared-claim": + api.prs[2].Body = api.prs[1].Body + case "reference": + api.prs[2].Body = gh.Ptr("Closes #102\nDepends on #1") + case "stack": + api.prs[2].Head.SHA = gh.Ptr("refreshed1") + case "pagination": + api.truncated = true + case "incomplete-count": + api.prs[2].ChangedFiles = gh.Ptr(2) + case "rename": + api.files[2][0].PreviousFilename = gh.Ptr("file1.go") + case "head-moved": + api.onFiles = func(a *preparationAPI, n int) { + if n == 2 { + a.prs[2].Head.SHA = gh.Ptr("unexpected") + } + } + + case "base-moved": + api.onFiles = func(a *preparationAPI, _ int) { a.baseSHA = "base2" } + case "candidate-ci": + api.pending["head2"] = true + case "candidate-review": + c.reviewGate = &ReviewGate{RequireApproval: func() bool { return true }, Verdict: func(string, int, string) ReviewGateVerdict { return ReviewGateVerdict{} }} + } + if ev, _, _, err := preparationGate(c, 2, "main", api.prs[2].GetHead().GetSHA()); err != nil || ev.BranchUpdated || api.updates[2] != 0 { + t.Fatalf("unsafe %s advanced: %+v %v %v", blocker, ev, err, api.updates) + } + }) + } +} + +func TestPreparationSweepAndRelayShareDestinationGuard(t *testing.T) { + api, c, url := readinessFixture(t, false) + _, release, err := hgithub.AcquireDestinationMerge(context.Background(), "", "acme/widget", "main") + if err != nil { + t.Fatal(err) + } + defer release() + if _, _, err := c.trySweepSelfAuthoredPR(context.Background(), "acme/widget", "acme", "widget", 2, true); !errors.Is(err, hgithub.ErrDestinationMergeBusy) { + t.Fatalf("sweep escaped destination guard: %v", err) + } + client := hgithub.NewClientForTest(url, "acme", nil, c.logger) + if _, err := client.MergePR(context.Background(), "acme/widget", 2, "squash", "head2"); !errors.Is(err, hgithub.ErrDestinationMergeBusy) { + t.Fatalf("relay merge escaped same destination guard: %v", err) + } + if api.merges[2] != 0 || api.updates[2] != 0 { + t.Fatalf("guard allowed mutation: merges=%v updates=%v", api.merges, api.updates) + } +} + +func TestPreparationConcurrentYieldAdmitsOnlyOneMutation(t *testing.T) { + api, c, _ := readinessFixture(t, false) + var wg sync.WaitGroup + for _, number := range []int{2, 3} { + wg.Add(1) + go func(number int) { + defer wg.Done() + _, _, _, err := preparationGate(c, number, "main", api.prs[number].GetHead().GetSHA()) + if err != nil { + t.Error(err) + } + }(number) + } + wg.Wait() + if api.updates[1] != 1 || api.updates[2]+api.updates[3] != 1 { + t.Fatalf("completed owner yielded to concurrent mutations: %v", api.updates) + } +} + +func TestPreparationReadyFirstWithoutActiveOwner(t *testing.T) { + api, c, _ := readinessFixture(t, false) + _ = c.preparationStore().change(preparationKey("acme", "widget", "main"), func(st *basePreparation) error { + ticket := st.Owner + ticket.Head = "refreshed1" + st.Waiting = []preparationCandidate{ticket} + st.Owner = preparationCandidate{} + return nil + }) + if ev, _, _, err := preparationGate(c, 2, "main", "head2"); err != nil || !ev.BranchUpdated { + t.Fatalf("blocked oldest waiting entry: %+v %v", ev, err) + } + if api.updates[1] != 1 || api.updates[2] != 1 { + t.Fatalf("unexpected refreshes: %v", api.updates) + } +} + +func TestPreparationClaimUsesLiveBaseNotPRBasePayload(t *testing.T) { + api, c, _ := readinessFixture(t, false) + api.baseSHA = "base2" + api.prs[1].Base.SHA = gh.Ptr("stale-base") + api.prs[1].Head.SHA = gh.Ptr("refreshed1") + if err := c.claimBasePreparationUpdate(context.Background(), "acme", "widget", 1, "refreshed1"); err != nil { + t.Fatal(err) + } + st, _ := c.preparationStore().ownerSnapshot(preparationKey("acme", "widget", "main")) + if st.Attempt.Base != "base2" || !st.Attempt.AuthoritativeBase { + t.Fatalf("stale PR base used as authoritative: %+v", st) + } +} diff --git a/src/pkg/github/automerge/automerge_preparation_terminal_test.go b/src/pkg/github/automerge/automerge_preparation_terminal_test.go new file mode 100644 index 0000000000..55892c42c5 --- /dev/null +++ b/src/pkg/github/automerge/automerge_preparation_terminal_test.go @@ -0,0 +1,32 @@ +package automerge + +import ( + "testing" + + gh "github.com/google/go-github/v72/github" +) + +func TestPreparationYieldedHeadChangeDoesNotHideTerminalState(t *testing.T) { + for _, terminal := range []string{"closed", "retargeted", "held"} { + t.Run(terminal, func(t *testing.T) { + api, c, _ := readinessFixture(t, false) + if ev, _, _, err := preparationGate(c, 2, "main", "head2"); err != nil || !ev.BranchUpdated { + t.Fatalf("yield: %+v %v", ev, err) + } + api.prs[1].Head.SHA = gh.Ptr("changed-after-yield") + switch terminal { + case "closed": + api.prs[1].State = gh.Ptr("closed") + case "retargeted": + api.prs[1].Base.Ref = gh.Ptr("other") + case "held": + api.prs[1].Labels = []*gh.Label{{Name: gh.Ptr("hold")}} + } + api.prs[2].Head.SHA = gh.Ptr("refreshed2") + api.pending["refreshed2"] = true + if ev, reason, _, err := preparationGate(c, 3, "main", "head3"); err != nil || !ev.BranchUpdated { + t.Fatalf("terminal yielded ticket blocked successor: %+v %q %v", ev, reason, err) + } + }) + } +} diff --git a/src/pkg/github/automerge/automerge_sweep_guards_test.go b/src/pkg/github/automerge/automerge_sweep_guards_test.go index f8391cd17c..3a0d34a50b 100644 --- a/src/pkg/github/automerge/automerge_sweep_guards_test.go +++ b/src/pkg/github/automerge/automerge_sweep_guards_test.go @@ -90,12 +90,12 @@ func newSelfSweepGuardAPI(t *testing.T, fx selfSweepFixture) *httptest.Server { w.WriteHeader(fx.firstGetStatus) return } - if fetches > 1 && fx.recheckGetStatus != 0 { + if fetches > 2 && fx.recheckGetStatus != 0 { w.WriteHeader(fx.recheckGetStatus) return } head := *fx.headSHA - if fetches > 1 { + if fetches > 2 { head = *fx.recheckHeadSHA } var prLabels []map[string]string @@ -114,6 +114,7 @@ func newSelfSweepGuardAPI(t *testing.T, fx selfSweepFixture) *httptest.Server { "mergeable": fx.mergeableState == "clean", "user": map[string]string{"login": fx.author}, "head": map[string]string{"sha": head}, + "base": map[string]string{"ref": "main"}, }) case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/pulls/7/files": if fx.filesHTTPCode != 0 { diff --git a/src/pkg/github/automerge/automerge_sweep_queued.go b/src/pkg/github/automerge/automerge_sweep_queued.go index bbab9b48f3..d624a6d596 100644 --- a/src/pkg/github/automerge/automerge_sweep_queued.go +++ b/src/pkg/github/automerge/automerge_sweep_queued.go @@ -16,13 +16,14 @@ import ( ) 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) + pr, ctx, unlock, err := c.guardedPR(ctx, owner, repo, number) if err != nil { if isGitHubStatus(err, http.StatusNotFound) { return AutoMergeSweepEvent{}, "gone", nil } return AutoMergeSweepEvent{}, "fetch-pr", err } + defer unlock() defer func() { c.reconcileNativeQueueGate(ctx, nativeQueueLaneQueued, owner, repo, pr, reasonOut) }() c.noteStagePR(owner, repo, number, pr) if !strings.EqualFold(pr.GetState(), "open") { @@ -593,6 +594,13 @@ func mergeAPIErrorNotApplied(err error) error { // brackets it with a merge attempt (hivecommons/hive#15245) so the tracker // shows "Merging" for the call's duration and a refusal's reason after. func (c *Engine) executeMergeEffect(ctx context.Context, claim effects.Claim, owner, repo string, number int, head string, merge func(context.Context) (effects.Result, error)) (effects.Result, error) { + original := merge + merge = func(ctx context.Context) (effects.Result, error) { + if err := hgithub.VerifyDestinationMerge(ctx, c.gh, owner, repo, number, head); err != nil { + return effects.Result{}, effects.NotApplied(err) + } + return original(ctx) + } slug := owner + "/" + repo hgithub.BeginMergeAttemptOnHead(slug, number, hgithub.PRAuditPathSweep, head) out, err := c.executeMergeEffectOnce(ctx, claim, owner, repo, number, merge) diff --git a/src/pkg/github/automerge/automerge_sweep_test.go b/src/pkg/github/automerge/automerge_sweep_test.go index bce088ee0d..0128ce5774 100644 --- a/src/pkg/github/automerge/automerge_sweep_test.go +++ b/src/pkg/github/automerge/automerge_sweep_test.go @@ -1852,7 +1852,7 @@ func newSelfAuthoredAutoMergeAPI(t *testing.T, prs []selfAuthoredPR, merged *[]i pr := byNumber[number] fetchCount[number]++ headSHA := "sha" + strconv.Itoa(pr.number) - if fetchCount[number] > 1 && pr.headSHAOverride != "" { + if fetchCount[number] > 2 && pr.headSHAOverride != "" { headSHA = pr.headSHAOverride } body := map[string]any{ diff --git a/src/pkg/github/automerge/automerge_sweep_trusted.go b/src/pkg/github/automerge/automerge_sweep_trusted.go index 747553c040..90f8d16d95 100644 --- a/src/pkg/github/automerge/automerge_sweep_trusted.go +++ b/src/pkg/github/automerge/automerge_sweep_trusted.go @@ -17,13 +17,14 @@ import ( ) func (c *Engine) trySweepSelfAuthoredPR(ctx context.Context, displayRepo, owner, repo string, number int, branchUpdateAllowed bool) (AutoMergeSweepEvent, string, error) { - pr, _, err := c.gh.PullRequests.Get(hgithub.WithRESTCaller(ctx, "hive:automerge_sweep"), owner, repo, number) + pr, ctx, unlock, err := c.guardedPR(ctx, owner, repo, number) if err != nil { if isGitHubStatus(err, http.StatusNotFound) { return AutoMergeSweepEvent{}, "gone", nil } return AutoMergeSweepEvent{}, "fetch-pr", err } + defer unlock() c.noteStagePR(owner, repo, number, pr) if !strings.EqualFold(pr.GetState(), "open") { return AutoMergeSweepEvent{}, "closed", nil @@ -575,13 +576,14 @@ func (c *Engine) listQueuedPullRequestIssues(ctx context.Context, owner, repo, l } func (c *Engine) trySweepTrustedAuthorPR(ctx context.Context, displayRepo, owner, repo string, number int, policy TrustedAuthorPolicy) (AutoMergeSweepEvent, string, error) { - pr, _, err := c.gh.PullRequests.Get(hgithub.WithRESTCaller(ctx, "hive:trusted_author_automerge_sweep"), owner, repo, number) + pr, ctx, unlock, err := c.guardedPR(ctx, owner, repo, number) if err != nil { if isGitHubStatus(err, http.StatusNotFound) { return AutoMergeSweepEvent{}, "gone", nil } return AutoMergeSweepEvent{}, "fetch-pr", err } + defer unlock() c.noteStagePR(owner, repo, number, pr) author := hgithub.SafeGetLogin(pr.GetUser()) if lane, _ := c.sweepLaneForAuthor(author); lane == stageLaneSelf { diff --git a/src/pkg/github/automerge/automerge_sweep_trusted_author_test.go b/src/pkg/github/automerge/automerge_sweep_trusted_author_test.go index 2db8deb2f2..bfb1b1e0a2 100644 --- a/src/pkg/github/automerge/automerge_sweep_trusted_author_test.go +++ b/src/pkg/github/automerge/automerge_sweep_trusted_author_test.go @@ -84,12 +84,12 @@ func newTrustedSweepAPI(t *testing.T, fx *trustedSweepFixture) *httptest.Server http.Error(w, "get error", fx.getStatus) return } - if fetches > 1 && fx.recheckStatus != 0 { + if fetches > 2 && fx.recheckStatus != 0 { http.Error(w, "recheck error", fx.recheckStatus) return } sha := "sha7" - if fetches > 1 && fx.recheckHeadSHA != "" { + if fetches > 2 && fx.recheckHeadSHA != "" { sha = fx.recheckHeadSHA } json.NewEncoder(w).Encode(pull(sha)) diff --git a/src/pkg/github/automerge/base_preparation_store.go b/src/pkg/github/automerge/base_preparation_store.go index 9f3a2bd718..7d7f7cbe4e 100644 --- a/src/pkg/github/automerge/base_preparation_store.go +++ b/src/pkg/github/automerge/base_preparation_store.go @@ -25,17 +25,19 @@ type BasePreparationStore struct { } type preparationCandidate struct { - Number int `json:"number"` - Head string `json:"head"` - Lane string `json:"lane"` - Repair bool `json:"repair,omitempty"` - Since time.Time `json:"since"` + Number int `json:"number"` + Head string `json:"head"` + Lane string `json:"lane"` + Repair bool `json:"repair,omitempty"` + Yielded bool `json:"yielded,omitempty"` + Since time.Time `json:"since"` } type preparationAttempt struct { - Head string `json:"head"` - Base string `json:"base"` - At time.Time `json:"at"` + Head string `json:"head"` + Base string `json:"base"` + At time.Time `json:"at"` + AuthoritativeBase bool `json:"authoritative_base,omitempty"` } type basePreparation struct { diff --git a/src/pkg/github/destination_merge.go b/src/pkg/github/destination_merge.go new file mode 100644 index 0000000000..e4f814847f --- /dev/null +++ b/src/pkg/github/destination_merge.go @@ -0,0 +1,112 @@ +package github + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "os" + "path/filepath" + "strings" + "sync" + "sync/atomic" + "syscall" + + gh "github.com/google/go-github/v72/github" +) + +var ErrDestinationMergeBusy = errors.New("destination merge already in progress") + +var destinationMergeDirectory atomic.Value +var destinationMergeLocks sync.Map + +type destinationMergeLease struct{ key, dir string } +type destinationMergeLeaseKey struct{} + +// ConfigureDestinationMerges is called before starting production relays. +// All writers of a destination must share this durable directory. An empty +// directory is an isolated process-local guard for test clients. +func ConfigureDestinationMerges(dir string) { destinationMergeDirectory.Store(dir) } + +func DestinationMergeDirectory() string { + dir, _ := destinationMergeDirectory.Load().(string) + return dir +} + +func VerifyDestinationMerge(ctx context.Context, client *gh.Client, owner, repo string, number int, head string) error { + lease, ok := ctx.Value(destinationMergeLeaseKey{}).(destinationMergeLease) + if !ok { + return nil + } + pr, _, err := client.PullRequests.Get(WithRESTCaller(ctx, "hive:merge_destination_recheck"), owner, repo, number) + if err != nil { + return err + } + if !DestinationMergeHeld(ctx, lease.dir, owner+"/"+repo, pr.GetBase().GetRef()) || + (head != "" && head != pr.GetHead().GetSHA()) { + return errors.New("merge head or destination changed under lease") + } + return nil +} + +func DestinationMergeHeld(ctx context.Context, dir, repo, base string) bool { + held, ok := ctx.Value(destinationMergeLeaseKey{}).(destinationMergeLease) + return ok && held == (destinationMergeLease{key: strings.ToLower(strings.TrimSpace(repo)) + ":" + strings.TrimSpace(base), dir: dir}) +} + +// AcquireDestinationMerge holds a destination across its final admission +// reads and mutation. It never waits on another merge while retaining stale +// admission evidence. Nested calls on the same leased context borrow it. +func AcquireDestinationMerge(ctx context.Context, dir, repo, base string) (context.Context, func(), error) { + key := strings.ToLower(strings.TrimSpace(repo)) + ":" + strings.TrimSpace(base) + if strings.TrimSpace(repo) == "" || strings.TrimSpace(base) == "" { + return ctx, nil, errors.New("merge destination missing") + } + lease := destinationMergeLease{key: key, dir: dir} + if held, ok := ctx.Value(destinationMergeLeaseKey{}).(destinationMergeLease); ok { + if held == lease { + return ctx, func() {}, nil + } + return ctx, nil, errors.New("merge destination changed under lease") + } + if err := ctx.Err(); err != nil { + return ctx, nil, err + } + value, _ := destinationMergeLocks.LoadOrStore(dir+"\n"+key, &sync.Mutex{}) + lock, ok := value.(*sync.Mutex) + if !ok { + return ctx, nil, errors.New("invalid destination merge lock") + } + if !lock.TryLock() { + return ctx, nil, ErrDestinationMergeBusy + } + unlock := lock.Unlock + if dir != "" { + const directoryMode = 0o755 + const fileMode = 0o644 + if err := os.MkdirAll(dir, directoryMode); err != nil { + unlock() + return ctx, nil, err + } + sum := sha256.Sum256([]byte(key)) + f, err := os.OpenFile(filepath.Join(dir, hex.EncodeToString(sum[:])+".lock"), os.O_CREATE|os.O_RDWR, fileMode) + if err != nil { + unlock() + return ctx, nil, err + } + if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil { + _ = f.Close() + unlock() + if errors.Is(err, syscall.EWOULDBLOCK) { + err = ErrDestinationMergeBusy + } + return ctx, nil, err + } + unlock = func() { + _ = syscall.Flock(int(f.Fd()), syscall.LOCK_UN) + _ = f.Close() + lock.Unlock() + } + } + return context.WithValue(ctx, destinationMergeLeaseKey{}, lease), unlock, nil +} diff --git a/src/pkg/github/destination_merge_test.go b/src/pkg/github/destination_merge_test.go new file mode 100644 index 0000000000..26f87d42b6 --- /dev/null +++ b/src/pkg/github/destination_merge_test.go @@ -0,0 +1,84 @@ +package github + +import ( + "context" + "errors" + "os" + "os/exec" + "path/filepath" + "testing" +) + +func TestDestinationMergeLeaseExclusiveAndReentrant(t *testing.T) { + ctx, release, err := AcquireDestinationMerge(context.Background(), "", "ACME/widget", "main") + if err != nil { + t.Fatal(err) + } + defer release() + if _, _, err := AcquireDestinationMerge(context.Background(), "", "acme/widget", "main"); !errors.Is(err, ErrDestinationMergeBusy) { + t.Fatalf("overlapping mutation allowed: %v", err) + } + _, nested, err := AcquireDestinationMerge(ctx, "", "acme/widget", "main") + if err != nil { + t.Fatal(err) + } + nested() + if _, _, err := AcquireDestinationMerge(ctx, "", "acme/widget", "other"); err == nil { + t.Fatal("retarget borrowed a different destination") + } + _, other, err := AcquireDestinationMerge(context.Background(), "", "acme/widget", "MAIN") + if err != nil { + t.Fatal(err) + } + other() +} + +func TestDestinationMergeDurableCrossProcess(t *testing.T) { + dir := filepath.Join(t.TempDir(), "destination-merges") + _, release, err := AcquireDestinationMerge(context.Background(), dir, "acme/widget", "main") + if err != nil { + t.Fatal(err) + } + executable, err := os.Executable() + if err != nil { + release() + t.Fatal(err) + } + run := func(mode string) { + t.Helper() + cmd := exec.Command(executable, "-test.run=^TestDestinationMergeChild$") + cmd.Env = append(os.Environ(), "HIVE_MERGE_LOCK_TEST_DIR="+dir, "HIVE_MERGE_LOCK_TEST_MODE="+mode) + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("cross-process %s: %v\n%s", mode, err, out) + } + } + run("busy") + release() + run("acquire") + // The child exited without explicitly releasing the file descriptor. + _, release, err = AcquireDestinationMerge(context.Background(), dir, "acme/widget", "main") + if err != nil { + t.Fatal(err) + } + release() +} + +func TestDestinationMergeChild(t *testing.T) { + dir := os.Getenv("HIVE_MERGE_LOCK_TEST_DIR") + if dir == "" { + t.Skip("subprocess helper") + } + _, _, err := AcquireDestinationMerge(context.Background(), dir, "acme/widget", "main") + switch os.Getenv("HIVE_MERGE_LOCK_TEST_MODE") { + case "busy": + if !errors.Is(err, ErrDestinationMergeBusy) { + t.Fatalf("guard did not exclude another process: %v", err) + } + case "acquire": + if err != nil { + t.Fatal(err) + } + default: + t.Fatal("unknown helper mode") + } +} diff --git a/src/pkg/github/issue_dependencies.go b/src/pkg/github/issue_dependencies.go index e4175ac8cf..f4bf0291d1 100644 --- a/src/pkg/github/issue_dependencies.go +++ b/src/pkg/github/issue_dependencies.go @@ -59,6 +59,27 @@ type issueWithDependencies struct { SubIssuesSummary *IssueChildren `json:"sub_issues_summary,omitempty"` } +// ReadIssueDependencyEvidence requires an explicit native summary. Unlike +// work enumeration's compatibility fallback, unknown is never independence. +func ReadIssueDependencyEvidence(ctx context.Context, client *gh.Client, owner, repo string, number int) (blocked, known bool, err error) { + path := fmt.Sprintf("repos/%s/%s/issues/%d", owner, repo, number) + req, err := client.NewRequest(http.MethodGet, path, nil) + if err != nil { + return false, false, err + } + var payload issueWithDependencies + if _, err := client.Do(ctx, req, &payload); err != nil { + return false, false, err + } + if payload.PullRequestLinks != nil || payload.DependenciesSummary == nil { + return false, false, nil + } + if payload.DependenciesSummary.BlockedBy != 0 { + return true, true, nil + } + return false, true, nil +} + // listOpenIssuesWithBlockedCounts is Issues.ListByRepo(state=open) with the // dependency and sub-issue summaries preserved. It returns the issues // go-github would have and, keyed by issue number, how many OPEN blockers diff --git a/src/pkg/github/merge_request_watcher.go b/src/pkg/github/merge_request_watcher.go index e69c81a012..bc9e061a7b 100644 --- a/src/pkg/github/merge_request_watcher.go +++ b/src/pkg/github/merge_request_watcher.go @@ -409,6 +409,20 @@ func (c *Client) handleOneMergeRequest(ctx context.Context, path string, nowFn f c.handleForgeMergeRequest(ctx, path, req, attempts, merge, nowFn) return } + owner, repoName := c.SplitRepo(req.Repo) + destination, _, err := c.client.PullRequests.Get(WithRESTCaller(ctx, "hive:merge_destination"), owner, repoName, req.Number) + if err != nil { + c.recordMergeFailure(path, req, attempts, err.Error(), nowFn) + return + } + ctx, releaseDestination, err := AcquireDestinationMerge(ctx, DestinationMergeDirectory(), owner+"/"+repoName, destination.GetBase().GetRef()) + if err != nil { + if !errors.Is(err, ErrDestinationMergeBusy) { + c.recordMergeFailure(path, req, attempts, err.Error(), nowFn) + } + return + } + defer releaseDestination() // Human-merge paths (#11039): a PR touching a path listed in // auto_merge.human_merge_paths for its repo is never merged by the App, diff --git a/src/pkg/github/pullrequest.go b/src/pkg/github/pullrequest.go index 1baafe8768..e85e62e876 100644 --- a/src/pkg/github/pullrequest.go +++ b/src/pkg/github/pullrequest.go @@ -435,6 +435,18 @@ func (c *Client) MergePRVia(ctx context.Context, repo string, number int, mergeM } opts := &gh.PullRequestOptions{MergeMethod: mergeMethod} + if fetched == nil { + var err error + fetched, _, err = c.client.PullRequests.Get(WithRESTCaller(ctx, "hive:merge_destination"), owner, repo, number) + if err != nil { + return MergePRResult{}, err + } + } + ctx, releaseDestination, err := AcquireDestinationMerge(ctx, DestinationMergeDirectory(), owner+"/"+repo, fetched.GetBase().GetRef()) + if err != nil { + return MergePRResult{}, err + } + defer releaseDestination() if expectSHA = strings.TrimSpace(expectSHA); expectSHA != "" { opts.SHA = expectSHA } @@ -461,6 +473,9 @@ func (c *Client) MergePRVia(ctx context.Context, repo string, number int, mergeM Inputs: map[string]string{"method": mergeMethod, "expect_sha": expectSHA}, } merge := func(ctx context.Context) (effects.Result, error) { + if err := VerifyDestinationMerge(ctx, c.client, owner, repo, number, expectSHA); err != nil { + return effects.Result{}, effects.NotApplied(err) + } var apiErr error res, _, apiErr = c.client.PullRequests.Merge(ctx, owner, repo, number, commitMessage, opts) if apiErr != nil { diff --git a/src/pkg/github/pullrequest_merge_method_test.go b/src/pkg/github/pullrequest_merge_method_test.go index 532410a3b2..cb1904aac5 100644 --- a/src/pkg/github/pullrequest_merge_method_test.go +++ b/src/pkg/github/pullrequest_merge_method_test.go @@ -40,6 +40,7 @@ func TestMergePR_ForwardMergeGuard(t *testing.T) { "number": 7, "title": tt.title, "head": map[string]any{"ref": tt.head}, + "base": map[string]any{"ref": "main"}, }) }) mux.HandleFunc("PUT /repos/o/r/pulls/7/merge", func(w http.ResponseWriter, r *http.Request) { From 82f0c0e11c239f1898e01cf4ebe385f786fa1452 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Sat, 31 Oct 2026 11:25:11 -0400 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=90=9B=20fix:=20distinguish=20observe?= =?UTF-8?q?d=20preparation=20waits=20and=20repair=20gate=20fixtures?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andrew Anderson --- docs/readiness-preparation.md | 8 +- src/cmd/hive/boot_agents_deps.go | 4 +- .../automerge/automerge_base_preparation.go | 9 ++- .../automerge_base_preparation_test.go | 43 +++++++++- .../automerge_contributor_adopt_test.go | 7 ++ .../automerge/automerge_contributor_lane.go | 8 ++ .../automerge_contributor_lane_test.go | 5 +- .../automerge/automerge_destination_merge.go | 16 ++-- .../automerge_preparation_readiness.go | 12 ++- .../automerge_preparation_readiness_test.go | 79 ++++++++++++++++++- .../automerge/automerge_sweep_guards_test.go | 9 +++ .../automerge/automerge_sweep_queued.go | 13 ++- .../github/automerge/automerge_sweep_test.go | 21 ++++- .../automerge_sweep_trusted_author_test.go | 5 +- src/pkg/github/destination_merge.go | 7 ++ src/pkg/github/merge_request_watcher.go | 28 +++---- .../pullrequest_merge_reconcile_test.go | 2 +- 17 files changed, 229 insertions(+), 47 deletions(-) diff --git a/docs/readiness-preparation.md b/docs/readiness-preparation.md index b1ce4775f9..fd0e079e57 100644 --- a/docs/readiness-preparation.md +++ b/docs/readiness-preparation.md @@ -6,7 +6,13 @@ refresh, ambiguous mutation outcome, or active repair retains exclusive preparation ownership. A completed refresh can yield while its new head has a known pending CI -verdict. Completion requires a changed live head containing both the pinned +verdict. +Observed `status-pending`/`check-pending` on an explicitly unprotected base +is a known pending verdict too; missing required contexts, absent evidence, +stale conclusions and failed API reads cannot establish readiness. +Configured fallback requirements do not establish the authoritative required +set for overtaking; ordinary merge-gate fallback behavior is unchanged. +Completion requires a changed live head containing both the pinned pre-refresh head and the authoritative refresh-target base commit. Legacy attempts without authoritative base evidence remain exclusive. The former owner keeps its ticket, original priority, and head/base attempt deduplication; diff --git a/src/cmd/hive/boot_agents_deps.go b/src/cmd/hive/boot_agents_deps.go index ab22aa9f79..e5fdbda951 100644 --- a/src/cmd/hive/boot_agents_deps.go +++ b/src/cmd/hive/boot_agents_deps.go @@ -4,7 +4,6 @@ import ( "context" "log/slog" "os" - "path/filepath" "github.com/hivecommons/hive/crux/api/callback" @@ -16,7 +15,6 @@ import ( "github.com/hivecommons/hive/pkg/github" "github.com/hivecommons/hive/pkg/github/automerge" "github.com/hivecommons/hive/pkg/github/requestwatch" - "github.com/hivecommons/hive/pkg/stagelabels" ) // bootAgentsDeps carries the effects of bootAgents that outlive the call @@ -100,7 +98,7 @@ func defaultBootAgentsDeps() bootAgentsDeps { github.StartTokenAccessAuditWatcher(ctx, logger) }, startRequestRelays: func(ctx context.Context, c *github.Client, r requestRelays) <-chan struct{} { - github.ConfigureDestinationMerges(filepath.Join(stagelabels.DefaultStateDir, "destination-merges")) + github.ConfigureDestinationMerges(automerge.DefaultDestinationMergeDirectory()) if r.mergePolicy != nil { c.SetMergeRequestPolicyGate(r.mergePolicy(c)) } diff --git a/src/pkg/github/automerge/automerge_base_preparation.go b/src/pkg/github/automerge/automerge_base_preparation.go index 69ec41b172..e7faa362ca 100644 --- a/src/pkg/github/automerge/automerge_base_preparation.go +++ b/src/pkg/github/automerge/automerge_base_preparation.go @@ -102,6 +102,7 @@ func preparationQueued(front int) string { // durable owner is not a ready merge-train seat. func (c *Engine) admitBasePreparation(ctx context.Context, owner, repo, base string, number int, head, lane string, repair bool) (string, error) { reason := "" + yieldedOwner := preparationCandidate{} now := c.now() key := preparationKey(owner, repo, base) err := c.preparationStore().change(key, func(st *basePreparation) error { @@ -184,9 +185,6 @@ func (c *Engine) admitBasePreparation(ctx context.Context, owner, repo, base str index++ continue } - if yielded.Number != 0 && st.Owner.Number == number { - c.info("automerge base preparation completed owner yielded", "repo", owner+"/"+repo, "base", base, "pr", number, "former_owner", yielded.Number, "former_since", yielded.Since) - } reason = preparationQueued(next.Number) break } @@ -204,6 +202,8 @@ func (c *Engine) admitBasePreparation(ctx context.Context, owner, repo, base str // relinquish a completed owner's durable priority for nothing. st.Owner = yielded st.Waiting = slices.DeleteFunc(st.Waiting, func(w preparationCandidate) bool { return w.Number == yielded.Number }) + } else if yielded.Number != 0 { + yieldedOwner = yielded } if st.Owner.Number != number { if reason == "" { @@ -217,6 +217,9 @@ func (c *Engine) admitBasePreparation(ctx context.Context, owner, repo, base str } return nil }) + if err == nil && yieldedOwner.Number != 0 { + c.info("automerge base preparation completed owner yielded", "repo", owner+"/"+repo, "base", base, "pr", number, "head", head, "former_owner", yieldedOwner.Number, "former_since", yieldedOwner.Since) + } return reason, err } diff --git a/src/pkg/github/automerge/automerge_base_preparation_test.go b/src/pkg/github/automerge/automerge_base_preparation_test.go index 5794e2d91c..065d169932 100644 --- a/src/pkg/github/automerge/automerge_base_preparation_test.go +++ b/src/pkg/github/automerge/automerge_base_preparation_test.go @@ -23,6 +23,12 @@ type preparationAPI struct { prs map[int]*gh.PullRequest behind map[string]int pending map[string]bool + missing map[string]bool + stale map[string]bool + stacked bool + requiredContexts []string + requirementsError bool + additionalChecks map[string][]string updates map[int]int merges map[int]int files map[int][]*gh.CommitFile @@ -49,7 +55,10 @@ func newPreparationAPI(t *testing.T) (*preparationAPI, *httptest.Server) { t.Helper() api := &preparationAPI{ prs: map[int]*gh.PullRequest{}, behind: map[string]int{}, pending: map[string]bool{}, - updates: map[int]int{}, merges: map[int]int{}, + missing: map[string]bool{}, + stale: map[string]bool{}, + additionalChecks: map[string][]string{}, + updates: map[int]int{}, merges: map[int]int{}, files: map[int][]*gh.CommitFile{}, baseSHA: "base1", } for n := 1; n <= 3; n++ { @@ -111,6 +120,9 @@ func newPreparationAPI(t *testing.T) (*preparationAPI, *httptest.Server) { case "compare": base, head, _ := strings.Cut(path[4], "...") status := "diverged" + if api.stacked && base == "head2" && head == "refreshed1" { + status = "ahead" + } if (base == "head1" || base == "base1") && head == "refreshed1" || (base == "head2" || base == "base1" || base == "base2") && head == "refreshed2" { status = "ahead" @@ -133,21 +145,48 @@ func newPreparationAPI(t *testing.T) (*preparationAPI, *httptest.Server) { write(payload) case "branches": if len(path) > 5 { + if api.requirementsError { + w.WriteHeader(http.StatusServiceUnavailable) + return + } + if len(api.requiredContexts) != 0 { + write(map[string]any{"strict": true, "contexts": api.requiredContexts}) + return + } w.WriteHeader(http.StatusNotFound) write(map[string]string{"message": "Branch not protected"}) } else { + if api.requirementsError { + w.WriteHeader(http.StatusServiceUnavailable) + return + } write(map[string]any{"name": path[4], "commit": map[string]string{"sha": api.baseSHA}}) } case "rules": + if api.requirementsError { + w.WriteHeader(http.StatusServiceUnavailable) + return + } write([]any{}) case "commits": head := path[4] + if api.missing[head] { + write(map[string]any{"state": "pending", "total_count": 0, "statuses": []any{}, "check_runs": []any{}}) + return + } state, conclusion := "success", "success" if api.pending[head] { state, conclusion = "pending", "" } + if api.stale[head] { + state, conclusion = "success", "stale" + } if path[5] == "status" { - write(map[string]any{"state": state, "total_count": 1, "statuses": []map[string]string{{"context": "build", "state": state}}}) + statuses := []map[string]string{{"context": "build", "state": state}} + for _, name := range api.additionalChecks[head] { + statuses = append(statuses, map[string]string{"context": name, "state": "success"}) + } + write(map[string]any{"state": state, "total_count": len(statuses), "statuses": statuses}) } else { status := "completed" if conclusion == "" { diff --git a/src/pkg/github/automerge/automerge_contributor_adopt_test.go b/src/pkg/github/automerge/automerge_contributor_adopt_test.go index 3bab0e58fd..0d496bc52c 100644 --- a/src/pkg/github/automerge/automerge_contributor_adopt_test.go +++ b/src/pkg/github/automerge/automerge_contributor_adopt_test.go @@ -255,6 +255,13 @@ func (a *adoptAPI) handle(w http.ResponseWriter, r *http.Request) bool { } _ = json.NewEncoder(w).Encode(payload) return true + case r.Method == http.MethodGet && path == "/repos/acme/widget/branches/v5": + base := a.compareBase + if base == "" { + base = "base1" + } + _ = json.NewEncoder(w).Encode(map[string]any{"name": "v5", "commit": map[string]string{"sha": base}}) + return true case r.Method == http.MethodGet && strings.HasPrefix(path, "/repos/acme/widget/compare/"): if a.failCompare { w.WriteHeader(http.StatusInternalServerError) diff --git a/src/pkg/github/automerge/automerge_contributor_lane.go b/src/pkg/github/automerge/automerge_contributor_lane.go index 269f2bce89..6029337342 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane.go +++ b/src/pkg/github/automerge/automerge_contributor_lane.go @@ -56,6 +56,7 @@ package automerge import ( "context" + "errors" "fmt" "net/http" "path" @@ -1488,6 +1489,13 @@ func (c *Engine) evaluateContributorLanePR(ctx context.Context, displayRepo, own func (c *Engine) trySweepContributorLanePR(ctx context.Context, displayRepo, owner, repo string, number int, policy ContributorLanePolicy) (AutoMergeSweepEvent, string, error) { ctx, unlock, err := c.acquireDestinationMerge(ctx, owner, repo, number) if err != nil { + if isGitHubStatus(err, http.StatusNotFound) { + return AutoMergeSweepEvent{}, "gone", nil + } + var response *gh.ErrorResponse + if errors.As(err, &response) { + return AutoMergeSweepEvent{}, "fetch-pr", err + } return AutoMergeSweepEvent{}, basePreparationPrefix + "merge-guard", err } defer unlock() diff --git a/src/pkg/github/automerge/automerge_contributor_lane_test.go b/src/pkg/github/automerge/automerge_contributor_lane_test.go index 04fe2c00f6..7d8972cf5a 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane_test.go +++ b/src/pkg/github/automerge/automerge_contributor_lane_test.go @@ -185,7 +185,7 @@ func newContributorLaneAPI(t *testing.T, fx *contributorLaneFixture) *httptest.S "mergeable": state == "clean", "user": map[string]string{"login": author}, "head": map[string]any{"sha": sha, "repo": map[string]string{"full_name": headRepo}}, - "base": map[string]any{"repo": map[string]string{"full_name": "acme/widget"}}, + "base": map[string]any{"ref": "main", "sha": "base1", "repo": map[string]string{"full_name": "acme/widget"}}, "labels": labels, } if fx.baseRef != "" { @@ -512,6 +512,9 @@ func newContributorLaneEngineRepos(apiURL string, repos []string, policy Contrib client := hgithub.NewClient("token", "acme", repos, nil, apiURL) client.SetAppBotLogin(testHiveAppBotLogin) opts.ContributorLanePolicy = func() ContributorLanePolicy { return policy } + if opts.BasePreparation == nil { + opts.BasePreparation = NewBasePreparationStore("") + } if opts.ContributorLaneVerdict == nil { opts.ContributorLaneVerdict = approvedVerdict } diff --git a/src/pkg/github/automerge/automerge_destination_merge.go b/src/pkg/github/automerge/automerge_destination_merge.go index 796f93683a..cf098a0f75 100644 --- a/src/pkg/github/automerge/automerge_destination_merge.go +++ b/src/pkg/github/automerge/automerge_destination_merge.go @@ -3,7 +3,6 @@ package automerge import ( "context" "errors" - "path/filepath" gh "github.com/google/go-github/v72/github" hgithub "github.com/hivecommons/hive/pkg/github" @@ -14,11 +13,7 @@ func (c *Engine) acquireDestinationMerge(ctx context.Context, owner, repo string if err != nil { return ctx, nil, err } - dir := "" - if stateDir := c.preparationStore().dir; stateDir != "" { - dir = filepath.Join(filepath.Dir(stateDir), "destination-merges") - } - return hgithub.AcquireDestinationMerge(ctx, dir, owner+"/"+repo, pr.GetBase().GetRef()) + return hgithub.AcquireDestinationMerge(ctx, destinationDirectory(c), owner+"/"+repo, pr.GetBase().GetRef()) } func (c *Engine) guardedPR(ctx context.Context, owner, repo string, number int) (*gh.PullRequest, context.Context, func(), error) { @@ -41,8 +36,9 @@ func (c *Engine) guardedPR(ctx context.Context, owner, repo string, number int) } func destinationDirectory(c *Engine) string { - if dir := c.preparationStore().dir; dir != "" { - return filepath.Join(filepath.Dir(dir), "destination-merges") - } - return "" + return hgithub.DestinationMergeDirectoryForPreparation(c.preparationStore().dir) +} + +func DefaultDestinationMergeDirectory() string { + return hgithub.DestinationMergeDirectoryForPreparation(sharedBasePreparation.dir) } diff --git a/src/pkg/github/automerge/automerge_preparation_readiness.go b/src/pkg/github/automerge/automerge_preparation_readiness.go index 06c7b49cd6..7b1203b494 100644 --- a/src/pkg/github/automerge/automerge_preparation_readiness.go +++ b/src/pkg/github/automerge/automerge_preparation_readiness.go @@ -24,12 +24,16 @@ func (c *Engine) preparationReadiness(ctx context.Context, owner, repo string, p if lane != stageLaneSelf && lane != stageLaneTrusted { return false, false } - green, reason, err := c.commitGreenForPR(ctx, owner, repo, pr.GetBase().GetRef(), pr.GetHead().GetSHA(), pr.GetNumber(), time.Time{}, newExpectedCheckCache()) - if err != nil { + st, requirementsKnown, err := c.commitCIForPRSkipping(ctx, owner, repo, pr.GetBase().GetRef(), pr.GetHead().GetSHA(), pr.GetNumber(), time.Time{}, newExpectedCheckCache(), nil) + if err != nil || !requirementsKnown { return false, false } - if !green { - return false, stageengine.ClassifyCIReason(reason) == stageengine.CIReasonPending && strings.HasPrefix(reason, "required-check-pending:") + if !st.Green { + // Unprotected bases use observed CI evidence rather than required + // context names. These are explicit in-flight observations too; + // missing/stale evidence and dispatch-age reasons remain unknown. + pending := strings.HasPrefix(st.Reason, "required-check-pending:") || st.Reason == "status-pending" || st.Reason == "check-pending" + return false, len(st.MissingRequired) == 0 && stageengine.ClassifyCIReason(st.Reason) == stageengine.CIReasonPending && pending } if gate := c.currentReviewGate(); gate != nil && gate.effectiveMode() == ReviewGateModeEnforce { if gate.Verdict == nil { diff --git a/src/pkg/github/automerge/automerge_preparation_readiness_test.go b/src/pkg/github/automerge/automerge_preparation_readiness_test.go index 42e527b876..6ce6216a17 100644 --- a/src/pkg/github/automerge/automerge_preparation_readiness_test.go +++ b/src/pkg/github/automerge/automerge_preparation_readiness_test.go @@ -1,11 +1,15 @@ package automerge import ( + "bytes" "context" "errors" + "log/slog" "path/filepath" + "strings" "sync" "testing" + "time" gh "github.com/google/go-github/v72/github" hgithub "github.com/hivecommons/hive/pkg/github" @@ -21,6 +25,12 @@ func readinessFixture(t *testing.T, durable bool) (*preparationAPI, *Engine, str c := preparationEngine(srv.URL, NewBasePreparationStore(dir)) c.requiredChecksForRepo = func(string) (map[string]bool, bool) { return map[string]bool{"build": true}, true } + c.reviewGate = &ReviewGate{RequireApproval: func() bool { return true }, Verdict: func(_ string, number int, head string) ReviewGateVerdict { + api.mu.Lock() + defer api.mu.Unlock() + exact := api.prs[number].GetHead().GetSHA() == head + return ReviewGateVerdict{Loaded: true, Found: exact, Approved: exact} + }} if ev, _, _, err := preparationGate(c, 1, "main", "head1"); err != nil || !ev.BranchUpdated { t.Fatalf("initial refresh: %+v %v", ev, err) } @@ -28,20 +38,27 @@ func readinessFixture(t *testing.T, durable bool) (*preparationAPI, *Engine, str api.pending["refreshed1"] = true api.behind["refreshed1"] = 1 api.baseSHA = "base2" + api.additionalChecks["head2"] = []string{"unreported"} return api, c, srv.URL } func TestPreparationCompletedOwnerYieldsAndSurvivesRestart(t *testing.T) { api, c, url := readinessFixture(t, true) + var logs bytes.Buffer + c.logger = slog.New(slog.NewTextHandler(&logs, nil)) if ev, reason, _, err := preparationGate(c, 2, "main", "head2"); err != nil || !ev.BranchUpdated { t.Fatalf("ready independent successor: %+v %q %v", ev, reason, err) } + if !strings.Contains(logs.String(), "completed owner yielded") || !strings.Contains(logs.String(), "former_owner=1") { + t.Fatalf("successful yielding lacks distinct durable observability: %s", logs.String()) + } st, err := c.preparationStore().ownerSnapshot(preparationKey("acme", "widget", "main")) if err != nil || st.Owner.Number != 2 || len(st.Waiters) != 1 || st.Waiters[0].Number != 1 || !st.Waiters[0].Yielded { t.Fatalf("yield lost priority: %+v %v", st, err) } restarted := preparationEngine(url, NewBasePreparationStore(c.preparationStore().dir)) restarted.requiredChecksForRepo = c.requiredChecksForRepo + restarted.reviewGate = c.reviewGate for i := 0; i < 3; i++ { if _, reason, _, err := preparationGate(restarted, 1, "main", "refreshed1"); err != nil || reason != preparationQueued(2) { t.Fatalf("unresolved second refresh: %q %v", reason, err) @@ -66,10 +83,47 @@ func TestPreparationCompletedOwnerYieldsAndSurvivesRestart(t *testing.T) { } } +func TestPreparationReadinessDistinguishesObservedPendingFromMissing(t *testing.T) { + api, c, _ := readinessFixture(t, false) + green, reason, err := c.commitGreenForPR(context.Background(), "acme", "widget", "main", "refreshed1", 1, time.Time{}, newExpectedCheckCache()) + if err != nil || green || reason != "status-pending" { + t.Fatalf("unprotected fixture's actual CI blocker: green=%v reason=%q err=%v", green, reason, err) + } + if ready, known := c.preparationReadiness(context.Background(), "acme", "widget", api.prs[1], stageLaneSelf); ready || !known { + t.Fatalf("observed pending was mistaken for absent evidence: ready=%v known=%v", ready, known) + } + api.missing["refreshed1"] = true + if ready, known := c.preparationReadiness(context.Background(), "acme", "widget", api.prs[1], stageLaneSelf); ready || known { + t.Fatalf("absent evidence was mistaken for observed pending: ready=%v known=%v", ready, known) + } + api.missing["refreshed1"] = false + api.requirementsError = true + if ready, known := c.preparationReadiness(context.Background(), "acme", "widget", api.prs[1], stageLaneSelf); ready || known { + t.Fatalf("configuration fallback was mistaken for authoritative requirements: ready=%v known=%v", ready, known) + } +} + +func TestPreparationAndBootUseTheSameDestinationDirectory(t *testing.T) { + if got := destinationDirectory(New(nil, Options{})); got != DefaultDestinationMergeDirectory() { + t.Fatalf("default sweep=%q boot/relay=%q", got, DefaultDestinationMergeDirectory()) + } + for _, dir := range []string{"", filepath.Join(t.TempDir(), "preparation")} { + c := New(nil, Options{BasePreparation: NewBasePreparationStore(dir)}) + if got, want := destinationDirectory(c), hgithub.DestinationMergeDirectoryForPreparation(dir); got != want { + t.Fatalf("configured sweep=%q shared derivation=%q", got, want) + } + } +} + func TestPreparationCannotYieldOnUnsafeOrMissingEvidence(t *testing.T) { - for _, blocker := range []string{"unresolved", "repair", "legacy-attempt", "dependency", "missing-dependency", "unreadable-dependency", "late-dependency", "shared-claim", "reference", "stack", "pagination", "incomplete-count", "rename", "head-moved", "base-moved", "candidate-ci", "candidate-review"} { + for _, blocker := range []string{"unresolved", "repair", "legacy-attempt", "dependency", "missing-dependency", "unreadable-dependency", "late-dependency", "shared-claim", "reference", "stack", "pagination", "incomplete-count", "rename", "head-moved", "base-moved", "candidate-ci", "candidate-review", "owner-ci-missing", "candidate-ci-missing", "owner-ci-stale", "required-ci-missing"} { t.Run(blocker, func(t *testing.T) { api, c, _ := readinessFixture(t, false) + if !c.preparationMayPass(context.Background(), "acme", "widget", "main", api.prs[2], api.prs[1], stageLaneSelf, stageLaneSelf) { + t.Fatal("negative test lacks a working independent-ready positive control") + } + var logs bytes.Buffer + c.logger = slog.New(slog.NewTextHandler(&logs, nil)) switch blocker { case "unresolved": api.prs[1].Head.SHA = gh.Ptr("head1") @@ -98,7 +152,7 @@ func TestPreparationCannotYieldOnUnsafeOrMissingEvidence(t *testing.T) { case "reference": api.prs[2].Body = gh.Ptr("Closes #102\nDepends on #1") case "stack": - api.prs[2].Head.SHA = gh.Ptr("refreshed1") + api.stacked = true case "pagination": api.truncated = true case "incomplete-count": @@ -113,15 +167,32 @@ func TestPreparationCannotYieldOnUnsafeOrMissingEvidence(t *testing.T) { } case "base-moved": - api.onFiles = func(a *preparationAPI, _ int) { a.baseSHA = "base2" } + api.onFiles = func(a *preparationAPI, _ int) { a.baseSHA = "base3" } case "candidate-ci": api.pending["head2"] = true case "candidate-review": - c.reviewGate = &ReviewGate{RequireApproval: func() bool { return true }, Verdict: func(string, int, string) ReviewGateVerdict { return ReviewGateVerdict{} }} + c.reviewGate.Verdict = func(string, int, string) ReviewGateVerdict { return ReviewGateVerdict{} } + case "owner-ci-missing": + api.missing["refreshed1"] = true + case "candidate-ci-missing": + api.missing["head2"] = true + case "owner-ci-stale": + api.stale["refreshed1"] = true + case "required-ci-missing": + api.requiredContexts = []string{"build", "unreported"} + if ready, known := c.preparationReadiness(context.Background(), "acme", "widget", api.prs[2], stageLaneSelf); !ready || !known { + t.Fatalf("missing-owner-evidence test blocked the candidate instead: ready=%v known=%v", ready, known) + } + if ready, known := c.preparationReadiness(context.Background(), "acme", "widget", api.prs[1], stageLaneSelf); ready || known { + t.Fatalf("owner's missing required context remained known: ready=%v known=%v", ready, known) + } } if ev, _, _, err := preparationGate(c, 2, "main", api.prs[2].GetHead().GetSHA()); err != nil || ev.BranchUpdated || api.updates[2] != 0 { t.Fatalf("unsafe %s advanced: %+v %v %v", blocker, ev, err, api.updates) } + if strings.Contains(logs.String(), "completed owner yielded") { + t.Fatalf("blocked admission emitted a successful yield: %s", logs.String()) + } }) } } diff --git a/src/pkg/github/automerge/automerge_sweep_guards_test.go b/src/pkg/github/automerge/automerge_sweep_guards_test.go index 3a0d34a50b..c621d737f4 100644 --- a/src/pkg/github/automerge/automerge_sweep_guards_test.go +++ b/src/pkg/github/automerge/automerge_sweep_guards_test.go @@ -84,6 +84,15 @@ func newSelfSweepGuardAPI(t *testing.T, fx selfSweepFixture) *httptest.Server { fetches := 0 return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch { + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/branches/main": + json.NewEncoder(w).Encode(map[string]any{"name": "main", "protected": false, "commit": map[string]string{"sha": "base1"}}) + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/branches/main/protection/required_status_checks": + w.WriteHeader(http.StatusNotFound) + json.NewEncoder(w).Encode(map[string]string{"message": "Branch not protected"}) + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/rules/branches/main": + json.NewEncoder(w).Encode([]any{}) + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/commits/base1/status": + json.NewEncoder(w).Encode(map[string]any{"state": "success", "total_count": 1, "statuses": []map[string]string{{"context": "ci/build", "state": "success"}}}) case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/pulls/7": fetches++ if fetches == 1 && fx.firstGetStatus != 0 { diff --git a/src/pkg/github/automerge/automerge_sweep_queued.go b/src/pkg/github/automerge/automerge_sweep_queued.go index d624a6d596..2c16034ef7 100644 --- a/src/pkg/github/automerge/automerge_sweep_queued.go +++ b/src/pkg/github/automerge/automerge_sweep_queued.go @@ -328,6 +328,14 @@ func (c *Engine) commitGreenForPR(ctx context.Context, owner, repo, branch, sha } func (c *Engine) commitGreenForPRSkipping(ctx context.Context, owner, repo, branch, sha string, prNumber int, headPushedAt time.Time, expectedChecks *expectedCheckCache, skippedByDesign map[string]bool) (bool, string, error) { + st, _, err := c.commitCIForPRSkipping(ctx, owner, repo, branch, sha, prNumber, headPushedAt, expectedChecks, skippedByDesign) + if err != nil { + return false, st.Reason, err + } + return st.Green, st.Reason, err +} + +func (c *Engine) commitCIForPRSkipping(ctx context.Context, owner, repo, branch, sha string, prNumber int, headPushedAt time.Time, expectedChecks *expectedCheckCache, skippedByDesign map[string]bool) (hgithub.CommitCIState, bool, error) { // The walk itself lives in hgithub.EvaluateCommitCI so the merge-request // watcher's positive-confirmation gate (#6173) evaluates a SHA with the // identical rules; only the required-set precedence is engine-specific. @@ -364,7 +372,7 @@ func (c *Engine) commitGreenForPRSkipping(ctx context.Context, owner, repo, bran Now: c.now, }) if err != nil { - return false, st.Reason, err + return st, actualKnown, err } if len(st.Reporting) > 0 { c.info("automerge CI reporting observation", "repo", owner+"/"+repo, "pr", prNumber, @@ -396,7 +404,8 @@ func (c *Engine) commitGreenForPRSkipping(ctx context.Context, owner, repo, bran reason = baseBranchRedReason(checks) } } - return st.Green, reason, nil + st.Reason = reason + return st, actualKnown, nil } func isFreshHeadOrMissingExpectedReason(reason string) bool { diff --git a/src/pkg/github/automerge/automerge_sweep_test.go b/src/pkg/github/automerge/automerge_sweep_test.go index 0128ce5774..78cb55cdfb 100644 --- a/src/pkg/github/automerge/automerge_sweep_test.go +++ b/src/pkg/github/automerge/automerge_sweep_test.go @@ -2900,7 +2900,14 @@ func newAutoMergeSweepClient(apiURL string) *Engine { func newAutoMergeSweepAPI(t *testing.T, expectedLabel string, prs []sweepPR, merged *[]int, extras ...func(http.ResponseWriter, *http.Request)) *httptest.Server { t.Helper() byNumber := make(map[int]sweepPR, len(prs)) - for _, pr := range prs { + for i, pr := range prs { + if pr.baseRef == "" { + pr.baseRef = "main" + } + if pr.baseSHA == "" { + pr.baseSHA = "base1" + } + prs[i] = pr byNumber[pr.number] = pr } return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -3032,6 +3039,18 @@ func newAutoMergeSweepAPI(t *testing.T, expectedLabel string, prs []sweepPR, mer extra(w, r) return } + switch r.URL.Path { + case "/repos/acme/widget/branches/main": + json.NewEncoder(w).Encode(map[string]any{"name": "main", "protected": false, "commit": map[string]string{"sha": "base1"}}) + return + case "/repos/acme/widget/branches/main/protection/required_status_checks": + w.WriteHeader(http.StatusNotFound) + json.NewEncoder(w).Encode(map[string]string{"message": "Branch not protected"}) + return + case "/repos/acme/widget/rules/branches/main": + json.NewEncoder(w).Encode([]any{}) + return + } t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) } })) diff --git a/src/pkg/github/automerge/automerge_sweep_trusted_author_test.go b/src/pkg/github/automerge/automerge_sweep_trusted_author_test.go index bfb1b1e0a2..b076b70b9e 100644 --- a/src/pkg/github/automerge/automerge_sweep_trusted_author_test.go +++ b/src/pkg/github/automerge/automerge_sweep_trusted_author_test.go @@ -67,7 +67,7 @@ func newTrustedSweepAPI(t *testing.T, fx *trustedSweepFixture) *httptest.Server "mergeable": state == "clean", "user": map[string]string{"login": author}, "head": map[string]any{"sha": sha, "repo": map[string]string{"full_name": headRepo}}, - "base": map[string]any{"repo": map[string]string{"full_name": "acme/widget"}}, + "base": map[string]any{"ref": "main", "sha": "base1", "repo": map[string]string{"full_name": "acme/widget"}}, "labels": prLabelObjects(fx.labels), } } @@ -156,6 +156,9 @@ func newTrustedSweepEngine(apiURL string, policy TrustedAuthorPolicy, opts Optio client := hgithub.NewClient("token", "acme", []string{"widget"}, nil, apiURL) client.SetAppBotLogin(testHiveAppBotLogin) opts.TrustedAuthorPolicy = func() TrustedAuthorPolicy { return policy } + if opts.BasePreparation == nil { + opts.BasePreparation = NewBasePreparationStore("") + } opts.TrustedAuthorizer = func(login, requireRole string) TrustedAuthorDecision { return TrustedAuthorDecision{Allowed: login == "alice", Role: requireRole} } diff --git a/src/pkg/github/destination_merge.go b/src/pkg/github/destination_merge.go index e4f814847f..630d1b9035 100644 --- a/src/pkg/github/destination_merge.go +++ b/src/pkg/github/destination_merge.go @@ -28,6 +28,13 @@ type destinationMergeLeaseKey struct{} // directory is an isolated process-local guard for test clients. func ConfigureDestinationMerges(dir string) { destinationMergeDirectory.Store(dir) } +func DestinationMergeDirectoryForPreparation(dir string) string { + if dir == "" { + return "" + } + return filepath.Join(filepath.Dir(dir), "destination-merges") +} + func DestinationMergeDirectory() string { dir, _ := destinationMergeDirectory.Load().(string) return dir diff --git a/src/pkg/github/merge_request_watcher.go b/src/pkg/github/merge_request_watcher.go index bc9e061a7b..2ef7aac2f9 100644 --- a/src/pkg/github/merge_request_watcher.go +++ b/src/pkg/github/merge_request_watcher.go @@ -409,6 +409,20 @@ func (c *Client) handleOneMergeRequest(ctx context.Context, path string, nowFn f c.handleForgeMergeRequest(ctx, path, req, attempts, merge, nowFn) return } + // Human-merge paths (#11039): a PR touching a path listed in + // auto_merge.human_merge_paths for its repo is never merged by the App, + // whatever agent asks. A match is terminal (the request is refused, the + // PR gets `hold` and one marker comment); an unknown changed-file list on + // a configured repo fails closed through the bounded retry path. Checked + // before the optional branch-update so a refused PR receives no push. + if refusal, err := c.mergeRequestHumanMergePathRefusal(ctx, req); err != nil { + c.recordMergeFailure(path, req, attempts, err.Error(), nowFn) + return + } else if refusal != "" { + c.denyMergeRequest(path, req, refusal, nowFn) + return + } + owner, repoName := c.SplitRepo(req.Repo) destination, _, err := c.client.PullRequests.Get(WithRESTCaller(ctx, "hive:merge_destination"), owner, repoName, req.Number) if err != nil { @@ -424,20 +438,6 @@ func (c *Client) handleOneMergeRequest(ctx context.Context, path string, nowFn f } defer releaseDestination() - // Human-merge paths (#11039): a PR touching a path listed in - // auto_merge.human_merge_paths for its repo is never merged by the App, - // whatever agent asks. A match is terminal (the request is refused, the - // PR gets `hold` and one marker comment); an unknown changed-file list on - // a configured repo fails closed through the bounded retry path. Checked - // before the optional branch-update so a refused PR receives no push. - if refusal, err := c.mergeRequestHumanMergePathRefusal(ctx, req); err != nil { - c.recordMergeFailure(path, req, attempts, err.Error(), nowFn) - return - } else if refusal != "" { - c.denyMergeRequest(path, req, refusal, nowFn) - return - } - // Lane policy: the contributor-lane gates the sweep enforces — tier, // soak, min_head_age, review verdict — apply to a relayed merge too // (hivecommons/docs#401 landed an untrusted-tier PR through this path diff --git a/src/pkg/github/pullrequest_merge_reconcile_test.go b/src/pkg/github/pullrequest_merge_reconcile_test.go index 3ee1fc6d31..94f147ac64 100644 --- a/src/pkg/github/pullrequest_merge_reconcile_test.go +++ b/src/pkg/github/pullrequest_merge_reconcile_test.go @@ -19,7 +19,7 @@ func mergeReconcileServer(t *testing.T, mergeStatuses []int, merged bool, puts * t.Helper() mux := http.NewServeMux() mux.HandleFunc("GET /repos/o/r/pulls/7", func(w http.ResponseWriter, _ *http.Request) { - pr := map[string]any{"number": 7, "merged": merged, "head": map[string]any{"ref": "feature/x"}} + pr := map[string]any{"number": 7, "merged": merged, "head": map[string]any{"ref": "feature/x", "sha": "headsha"}, "base": map[string]any{"ref": "main"}} if merged { pr["merge_commit_sha"] = "reconciledsha" } From dec860f549d4eef77f0683193128a9b1ec938b4d Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Sat, 31 Oct 2026 11:42:33 -0400 Subject: [PATCH 3/6] =?UTF-8?q?=F0=9F=90=9B=20fix:=20make=20readiness=20gu?= =?UTF-8?q?ard=20fixtures=20hermetic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andrew Anderson --- .../dashboard/api_repo_item_decision_test.go | 7 ++- .../rebase_merge_followthrough_test.go | 2 +- .../automerge_adoption_recovery_test.go | 5 ++ .../automerge_contributor_lane_test.go | 16 ++++-- .../automerge/automerge_merge_forward_test.go | 2 + .../github/automerge/automerge_sweep_test.go | 57 ++++++++++++++----- .../automerge_sweep_trusted_author_test.go | 3 + src/pkg/github/automerge/main_test.go | 25 ++++++++ 8 files changed, 94 insertions(+), 23 deletions(-) create mode 100644 src/pkg/github/automerge/main_test.go diff --git a/src/pkg/dashboard/api_repo_item_decision_test.go b/src/pkg/dashboard/api_repo_item_decision_test.go index 13aa600438..325fda1bdb 100644 --- a/src/pkg/dashboard/api_repo_item_decision_test.go +++ b/src/pkg/dashboard/api_repo_item_decision_test.go @@ -39,6 +39,7 @@ type repoItemDecisionMock struct { prNoMaintainerEdits bool workflowPath string prMergeState string + prHeadSHA string updates int prRedCI bool prNoCI bool @@ -151,8 +152,12 @@ func newRepoItemDecisionServer(t *testing.T, m *repoItemDecisionMock) *Server { if m.prFork { headRepo = "someone/testrepo" } + headSHA := m.prHeadSHA + if headSHA == "" { + headSHA = "feedfacefeedfacefeedfacefeedfacefeedface" + } _ = json.NewEncoder(w).Encode(map[string]any{"number": n, "state": "open", "body": m.prBody, "labels": prLabels, "user": author, "mergeable": mergeState != "dirty", "mergeable_state": mergeState, "maintainer_can_modify": !m.prNoMaintainerEdits, - "head": map[string]any{"sha": "feedfacefeedfacefeedfacefeedfacefeedface", "repo": map[string]any{"full_name": headRepo}}, + "head": map[string]any{"sha": headSHA, "repo": map[string]any{"full_name": headRepo}}, "base": map[string]any{"ref": "main", "repo": map[string]any{"full_name": "testorg/testrepo"}}}) case r.Method == http.MethodGet && strings.Contains(path, "/compare/"): files := []map[string]string{} diff --git a/src/pkg/dashboard/rebase_merge_followthrough_test.go b/src/pkg/dashboard/rebase_merge_followthrough_test.go index 8bab66b4e4..2b29ad0a3c 100644 --- a/src/pkg/dashboard/rebase_merge_followthrough_test.go +++ b/src/pkg/dashboard/rebase_merge_followthrough_test.go @@ -81,7 +81,7 @@ func TestRepoItemDecisionMergeNowOnDirtyPRArmsRebaseThenMerge(t *testing.T) { // fails with the files and the two options, and the PR gets the note. func TestDriveRebaseMergeGreenMergesAndConflictFails(t *testing.T) { store := useRebaseMergeStore(t) - mock := &repoItemDecisionMock{prForeign: true, commitBy: map[string]string{"0123456789abcdef0123456789abcdef01234567": "hive[bot]"}} + mock := &repoItemDecisionMock{prForeign: true, prHeadSHA: "0123456789abcdef0123456789abcdef01234567", commitBy: map[string]string{"0123456789abcdef0123456789abcdef01234567": "hive[bot]"}} srv := newRepoItemDecisionServer(t, mock) srv.operatorActionStore = newOperatorActionStore(filepath.Join(t.TempDir(), "ops.json")) now := time.Date(2026, 10, 29, 9, 0, 0, 0, time.UTC) diff --git a/src/pkg/github/automerge/automerge_adoption_recovery_test.go b/src/pkg/github/automerge/automerge_adoption_recovery_test.go index 0ff1fd3f93..442997cda4 100644 --- a/src/pkg/github/automerge/automerge_adoption_recovery_test.go +++ b/src/pkg/github/automerge/automerge_adoption_recovery_test.go @@ -160,6 +160,11 @@ func TestAdoptedRecoveryPreparationWaitDoesNotConsumeAttempt(t *testing.T) { h.e.basePreparation = NewBasePreparationStore("") h.adopt.compareBehind = 2 h.adopt.baseChecks = map[string]string{"coverage": "success"} + h.adopt.superseders[7] = map[string]any{ + "number": 7, "state": "open", + "head": map[string]string{"sha": "aaa"}, + "base": map[string]string{"ref": "v5", "sha": "base1"}, + } front, head, wantReason := 8, "head8", preparationQueued(8) if duplicate { front, head, wantReason = 7, "aaa", basePreparationReady diff --git a/src/pkg/github/automerge/automerge_contributor_lane_test.go b/src/pkg/github/automerge/automerge_contributor_lane_test.go index 7d8972cf5a..98ae2c7f80 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane_test.go +++ b/src/pkg/github/automerge/automerge_contributor_lane_test.go @@ -174,6 +174,10 @@ func newContributorLaneAPI(t *testing.T, fx *contributorLaneFixture) *httptest.S if headStatus == "" { headStatus = "success" } + baseRef := fx.baseRef + if baseRef == "" { + baseRef = "main" + } pull := func(sha string) map[string]any { p := map[string]any{ "node_id": fx.nodeID, @@ -342,9 +346,9 @@ func newContributorLaneAPI(t *testing.T, fx *contributorLaneFixture) *httptest.S return } json.NewEncoder(w).Encode(map[string]any{"merged": !fx.mergeNotApplied, "sha": "merge7"}) - case fx.baseRef != "" && r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/branches/"+fx.baseRef: - json.NewEncoder(w).Encode(map[string]any{"name": fx.baseRef, "commit": map[string]string{"sha": "base1"}}) - case fx.baseRef != "" && r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/branches/"+fx.baseRef+"/protection/required_status_checks": + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/branches/"+baseRef: + json.NewEncoder(w).Encode(map[string]any{"name": baseRef, "commit": map[string]string{"sha": "base1"}}) + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/branches/"+baseRef+"/protection/required_status_checks": if len(fx.requiredContexts) > 0 { json.NewEncoder(w).Encode(map[string]any{"strict": false, "contexts": fx.requiredContexts}) return @@ -369,11 +373,11 @@ func newContributorLaneAPI(t *testing.T, fx *contributorLaneFixture) *httptest.S return } w.WriteHeader(http.StatusCreated) - case fx.baseRef != "" && r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/rules/branches/"+fx.baseRef: + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/rules/branches/"+baseRef: json.NewEncoder(w).Encode([]any{}) - case fx.baseRef != "" && r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/commits/base1/status": + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/commits/base1/status": json.NewEncoder(w).Encode(map[string]any{"state": "success", "total_count": 0, "statuses": []any{}}) - case fx.baseRef != "" && r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/compare/"+fx.baseRef+"...sha7": + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/compare/"+baseRef+"...sha7": fx.compares++ json.NewEncoder(w).Encode(map[string]any{"behind_by": fx.behindBy, "ahead_by": 1}) case r.Method == http.MethodPut && r.URL.Path == "/repos/acme/widget/pulls/7/update-branch": diff --git a/src/pkg/github/automerge/automerge_merge_forward_test.go b/src/pkg/github/automerge/automerge_merge_forward_test.go index abc0167723..b087ec1a58 100644 --- a/src/pkg/github/automerge/automerge_merge_forward_test.go +++ b/src/pkg/github/automerge/automerge_merge_forward_test.go @@ -48,6 +48,8 @@ func newMergeForwardServer(t *testing.T, a *mergeForwardAPI) *httptest.Server { a.mu.Lock() defer a.mu.Unlock() switch { + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/branches/main": + _ = json.NewEncoder(w).Encode(map[string]any{"name": "main", "commit": map[string]string{"sha": "base1"}}) case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/pulls/7": pr := mergeForwardPR(false, "author/human") pr.State = gh.Ptr("open") diff --git a/src/pkg/github/automerge/automerge_sweep_test.go b/src/pkg/github/automerge/automerge_sweep_test.go index 78cb55cdfb..aafeed7a00 100644 --- a/src/pkg/github/automerge/automerge_sweep_test.go +++ b/src/pkg/github/automerge/automerge_sweep_test.go @@ -932,6 +932,9 @@ func TestTrySweepQueuedPRSkipBranches(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if serveUnprotectedSweepBase(w, r) { + return + } switch { case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/pulls/7": json.NewEncoder(w).Encode(map[string]any{ @@ -941,6 +944,7 @@ func TestTrySweepQueuedPRSkipBranches(t *testing.T) { "mergeable": tt.pr.mergeableState == "clean", "user": map[string]string{"login": tt.pr.author}, "head": map[string]string{"sha": "sha7"}, + "base": map[string]string{"ref": "main", "sha": "base1"}, "labels": tt.labels, }) case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/pulls/7/reviews": @@ -983,6 +987,9 @@ func TestTrySweepQueuedPRMissingHeadAndMergeNotApplied(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if serveUnprotectedSweepBase(w, r) { + return + } switch { case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/pulls/7": json.NewEncoder(w).Encode(map[string]any{ @@ -992,6 +999,7 @@ func TestTrySweepQueuedPRMissingHeadAndMergeNotApplied(t *testing.T) { "mergeable": true, "user": map[string]string{"login": "alice"}, "head": tt.head, + "base": map[string]string{"ref": "main", "sha": "base1"}, "labels": []map[string]string{{"name": hgithub.AutoMergeQueuedLabel}}, }) case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/pulls/7/reviews": @@ -1021,12 +1029,16 @@ func TestTrySweepQueuedPRMissingHeadAndMergeNotApplied(t *testing.T) { func TestTrySweepQueuedPRMergeError(t *testing.T) { api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if serveUnprotectedSweepBase(w, r) { + return + } switch { case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/pulls/7": json.NewEncoder(w).Encode(map[string]any{ "number": 7, "state": "open", "mergeable_state": "clean", "mergeable": true, "user": map[string]string{"login": "alice"}, "head": map[string]string{"sha": "sha7"}, + "base": map[string]string{"ref": "main", "sha": "base1"}, "labels": []map[string]string{{"name": hgithub.AutoMergeQueuedLabel}}, }) case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/pulls/7/reviews": @@ -1052,12 +1064,16 @@ func TestTrySweepQueuedPRMergeError(t *testing.T) { func TestTrySweepQueuedPRRequiredChecksExpectedIsRemembered(t *testing.T) { var merges int api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if serveUnprotectedSweepBase(w, r) { + return + } switch { case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/pulls/7": json.NewEncoder(w).Encode(map[string]any{ "number": 7, "state": "open", "mergeable_state": "clean", "mergeable": true, "user": map[string]string{"login": "alice"}, "head": map[string]string{"sha": "sha7"}, + "base": map[string]string{"ref": "main", "sha": "base1"}, "labels": []map[string]string{{"name": hgithub.AutoMergeQueuedLabel}}, }) case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/pulls/7/reviews": @@ -1087,7 +1103,7 @@ func TestTrySweepQueuedPRRequiredChecksExpectedIsRemembered(t *testing.T) { if merges != 1 { t.Fatalf("merges = %d, want 1 (the remembered refusal must not be re-PUT)", merges) } - attempt := mergeAttempt{owner: "acme", repo: "widget", number: 7, head: "sha7", state: "clean"} + attempt := mergeAttempt{owner: "acme", repo: "widget", base: "main", number: 7, head: "sha7", state: "clean"} if got := c.knownMergeRefusal(attempt); got != autoMergeReasonChecksExpected { t.Fatalf("knownMergeRefusal = %q, want %q for the queued head", got, autoMergeReasonChecksExpected) } @@ -2235,8 +2251,8 @@ func TestSweepSelfAuthoredAutoMergesFetchesUnheldCandidate(t *testing.T) { if result.Seen != 1 || result.Skipped != 1 || result.Candidates != 1 || len(result.Merged) != 0 || len(merged) != 0 { t.Fatalf("result=%+v merge calls=%v, want unheld PR fetched as one candidate then skipped", result, merged) } - if got := getCounts[11]; got != 1 { - t.Fatalf("/pulls/11 GET count = %d, want 1 for unheld candidate", got) + if got := getCounts[11]; got != 2 { + t.Fatalf("/pulls/11 GET count = %d, want destination lookup and guarded fetch for unheld candidate", got) } } @@ -2886,6 +2902,26 @@ func TestAutoMergeSweepPrefilterHelpers(t *testing.T) { // gate (audit F3). Anyone NOT listed is untrusted and must not merge. var testTrustedMergers = map[string]bool{"bob": true, "carol": true} +func serveUnprotectedSweepBase(w http.ResponseWriter, r *http.Request) bool { + if r.Method != http.MethodGet { + return false + } + switch r.URL.Path { + case "/repos/acme/widget/branches/main": + json.NewEncoder(w).Encode(map[string]any{"name": "main", "protected": false, "commit": map[string]string{"sha": "base1"}}) + case "/repos/acme/widget/commits/base1/status": + json.NewEncoder(w).Encode(map[string]any{"state": "success", "total_count": 0, "statuses": []any{}}) + case "/repos/acme/widget/branches/main/protection/required_status_checks": + w.WriteHeader(http.StatusNotFound) + json.NewEncoder(w).Encode(map[string]string{"message": "Branch not protected"}) + case "/repos/acme/widget/rules/branches/main": + json.NewEncoder(w).Encode([]any{}) + default: + return false + } + return true +} + func newAutoMergeSweepClient(apiURL string) *Engine { client := hgithub.NewClient("token", "acme", []string{"widget"}, nil, apiURL) client.SetAppBotLogin(testHiveAppBotLogin) @@ -3035,20 +3071,11 @@ func newAutoMergeSweepAPI(t *testing.T, expectedLabel string, prs []sweepPR, mer *merged = append(*merged, number) json.NewEncoder(w).Encode(map[string]any{"merged": true, "sha": "merge" + strconv.Itoa(number)}) default: - for _, extra := range extras { - extra(w, r) + if serveUnprotectedSweepBase(w, r) { return } - switch r.URL.Path { - case "/repos/acme/widget/branches/main": - json.NewEncoder(w).Encode(map[string]any{"name": "main", "protected": false, "commit": map[string]string{"sha": "base1"}}) - return - case "/repos/acme/widget/branches/main/protection/required_status_checks": - w.WriteHeader(http.StatusNotFound) - json.NewEncoder(w).Encode(map[string]string{"message": "Branch not protected"}) - return - case "/repos/acme/widget/rules/branches/main": - json.NewEncoder(w).Encode([]any{}) + for _, extra := range extras { + extra(w, r) return } t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) diff --git a/src/pkg/github/automerge/automerge_sweep_trusted_author_test.go b/src/pkg/github/automerge/automerge_sweep_trusted_author_test.go index b076b70b9e..51ab9fe4ed 100644 --- a/src/pkg/github/automerge/automerge_sweep_trusted_author_test.go +++ b/src/pkg/github/automerge/automerge_sweep_trusted_author_test.go @@ -46,6 +46,9 @@ func newTrustedSweepAPI(t *testing.T, fx *trustedSweepFixture) *httptest.Server t.Helper() fetches := 0 return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if serveUnprotectedSweepBase(w, r) { + return + } state := fx.mergeableState if state == "" { state = "clean" diff --git a/src/pkg/github/automerge/main_test.go b/src/pkg/github/automerge/main_test.go new file mode 100644 index 0000000000..21245f358e --- /dev/null +++ b/src/pkg/github/automerge/main_test.go @@ -0,0 +1,25 @@ +package automerge + +import ( + "fmt" + "os" + "path/filepath" + "testing" +) + +// Default engines must not read or write a deployed hive's preparation state. +func TestMain(m *testing.M) { + dir, err := os.MkdirTemp("", "automerge-hermetic-*") + if err != nil { + fmt.Fprintln(os.Stderr, "automerge TestMain:", err) + os.Exit(1) + } + sharedBasePreparation = NewBasePreparationStore(filepath.Join(dir, "base-preparation")) + + code := m.Run() + if err := os.RemoveAll(dir); err != nil { + fmt.Fprintln(os.Stderr, "automerge TestMain cleanup:", err) + code = 1 + } + os.Exit(code) +} From 19c632f4c58c347ad02100c3c0b5334b93336ff1 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Sat, 31 Oct 2026 11:51:40 -0400 Subject: [PATCH 4/6] =?UTF-8?q?=F0=9F=90=9B=20fix:=20retain=20head=20movem?= =?UTF-8?q?ent=20and=20unknown=20protection=20fixture=20coverage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andrew Anderson --- .../automerge_contributor_lane_test.go | 2 +- .../github/automerge/automerge_sweep_test.go | 54 ++++++++++++------- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/pkg/github/automerge/automerge_contributor_lane_test.go b/src/pkg/github/automerge/automerge_contributor_lane_test.go index 98ae2c7f80..e1dc417319 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane_test.go +++ b/src/pkg/github/automerge/automerge_contributor_lane_test.go @@ -262,7 +262,7 @@ func newContributorLaneAPI(t *testing.T, fx *contributorLaneFixture) *httptest.S return } sha := "sha7" - if fetches > 1 && fx.recheckHeadSHA != "" { + if fetches > 2 && fx.recheckHeadSHA != "" { sha = fx.recheckHeadSHA } json.NewEncoder(w).Encode(pull(sha)) diff --git a/src/pkg/github/automerge/automerge_sweep_test.go b/src/pkg/github/automerge/automerge_sweep_test.go index aafeed7a00..408fbc68e6 100644 --- a/src/pkg/github/automerge/automerge_sweep_test.go +++ b/src/pkg/github/automerge/automerge_sweep_test.go @@ -21,24 +21,25 @@ import ( const testHiveAppBotLogin = "kubestellar-hive[bot]" type sweepPR struct { - nodeID string - number int - author string - queuedBy string - reviewAuthor string - reviewState string - headSHA string - baseRef string - baseSHA string - reviewCommitID *string - label bool - extraLabels []string - draft bool - mergeableState string - statusState string - checkStatus string - checkConclusion string - headRepo string + nodeID string + number int + author string + queuedBy string + reviewAuthor string + reviewState string + headSHA string + baseRef string + baseSHA string + protectionUnknown bool + reviewCommitID *string + label bool + extraLabels []string + draft bool + mergeableState string + statusState string + checkStatus string + checkConclusion string + headRepo string } func issueLabels(primary string, extra []string) []map[string]string { @@ -619,13 +620,16 @@ func TestSweepQueuedAutoMergesDequeuesWhenApprovalHeadMissing(t *testing.T) { func TestSweepQueuedAutoMergesDefersRedUnknownChecksToMergeEndpoint(t *testing.T) { var merged []int api := newAutoMergeSweepAPI(t, hgithub.AutoMergeQueuedLabel, []sweepPR{ - {number: 7, author: "alice", queuedBy: "bob", label: true, mergeableState: "clean", statusState: "failure", checkStatus: "completed", checkConclusion: "success"}, + {number: 7, author: "alice", queuedBy: "bob", label: true, mergeableState: "clean", statusState: "failure", checkStatus: "completed", checkConclusion: "success", protectionUnknown: true}, {number: 8, author: "carol", queuedBy: "dave", label: false, mergeableState: "clean", statusState: "success", checkStatus: "completed", checkConclusion: "success"}, {number: 9, author: "erin", queuedBy: "frank", label: true, draft: true, mergeableState: "clean", statusState: "success", checkStatus: "completed", checkConclusion: "success"}, }, &merged) defer api.Close() c := newAutoMergeSweepClient(api.URL) + if _, known, _, _, source := hgithub.RequiredStatusCheckContextsDetailedWithSource(context.Background(), c.gh, "acme", "widget", "main", nil, false); known || source != "none" { + t.Fatalf("required checks known=%v source=%q, want unreadable protected-branch metadata", known, source) + } result, err := c.SweepQueuedAutoMerges(context.Background(), AutoMergeSweepOptions{}) if err != nil { t.Fatalf("SweepQueuedAutoMerges returned error: %v", err) @@ -2936,7 +2940,9 @@ func newAutoMergeSweepClient(apiURL string) *Engine { func newAutoMergeSweepAPI(t *testing.T, expectedLabel string, prs []sweepPR, merged *[]int, extras ...func(http.ResponseWriter, *http.Request)) *httptest.Server { t.Helper() byNumber := make(map[int]sweepPR, len(prs)) + protectionUnknown := false for i, pr := range prs { + protectionUnknown = protectionUnknown || pr.protectionUnknown if pr.baseRef == "" { pr.baseRef = "main" } @@ -3071,6 +3077,16 @@ func newAutoMergeSweepAPI(t *testing.T, expectedLabel string, prs []sweepPR, mer *merged = append(*merged, number) json.NewEncoder(w).Encode(map[string]any{"merged": true, "sha": "merge" + strconv.Itoa(number)}) default: + if protectionUnknown && r.Method == http.MethodGet { + switch r.URL.Path { + case "/repos/acme/widget/branches/main": + json.NewEncoder(w).Encode(map[string]any{"name": "main", "protected": true, "commit": map[string]string{"sha": "base1"}}) + return + case "/repos/acme/widget/branches/main/protection/required_status_checks": + http.Error(w, "protection metadata unavailable", http.StatusForbidden) + return + } + } if serveUnprotectedSweepBase(w, r) { return } From 4e334612634fb14933b200a2b8dbd512961e07f4 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Sat, 31 Oct 2026 12:00:07 -0400 Subject: [PATCH 5/6] =?UTF-8?q?=F0=9F=90=9B=20fix:=20cover=20readiness=20e?= =?UTF-8?q?vidence=20and=20destination=20lease=20failures?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andrew Anderson --- .../automerge_destination_merge_test.go | 77 +++++++++++++++++++ .../automerge_preparation_readiness_test.go | 39 ++++++++++ 2 files changed, 116 insertions(+) create mode 100644 src/pkg/github/automerge/automerge_destination_merge_test.go diff --git a/src/pkg/github/automerge/automerge_destination_merge_test.go b/src/pkg/github/automerge/automerge_destination_merge_test.go new file mode 100644 index 0000000000..ac37175082 --- /dev/null +++ b/src/pkg/github/automerge/automerge_destination_merge_test.go @@ -0,0 +1,77 @@ +package automerge + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "path/filepath" + "strings" + "testing" + + gh "github.com/google/go-github/v72/github" + hgithub "github.com/hivecommons/hive/pkg/github" +) + +func TestGuardedPRRevalidatesDestinationAndReleasesFailedLease(t *testing.T) { + for _, scenario := range []string{"success", "initial-fetch-failed", "guarded-fetch-failed", "retargeted"} { + t.Run(scenario, func(t *testing.T) { + fetches := 0 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet || r.URL.Path != "/repos/acme/widget/pulls/2" { + t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path) + w.WriteHeader(http.StatusNotFound) + return + } + fetches++ + if scenario == "initial-fetch-failed" || scenario == "guarded-fetch-failed" && fetches == 2 { + http.Error(w, `{"message":"fixture not found"}`, http.StatusNotFound) + return + } + pr := preparationPR(2, "main", "head2") + if scenario == "retargeted" && fetches == 2 { + pr.Base.Ref = gh.Ptr("release") + } + if err := json.NewEncoder(w).Encode(pr); err != nil { + t.Errorf("encode pull request: %v", err) + } + })) + defer srv.Close() + c := preparationEngine(srv.URL, NewBasePreparationStore(filepath.Join(t.TempDir(), "preparation"))) + ctx := context.Background() + pr, guarded, unlock, err := c.guardedPR(ctx, "acme", "widget", 2) + if scenario == "success" { + if err != nil || unlock == nil || pr.GetHead().GetSHA() != "head2" || + !hgithub.DestinationMergeHeld(guarded, destinationDirectory(c), "acme/widget", "main") { + if unlock != nil { + unlock() + } + t.Fatalf("successful reread lacks destination lease: pr=%+v err=%v", pr, err) + } + unlock() + } else { + if err == nil || pr != nil || unlock != nil { + if unlock != nil { + unlock() + } + t.Fatalf("failed reread returned a usable admission: pr=%+v err=%v", pr, err) + } + if scenario == "retargeted" && !strings.Contains(err.Error(), "destination changed") { + t.Fatalf("retargeting did not fail at the destination guard: %v", err) + } + } + wantFetches := 2 + if scenario == "initial-fetch-failed" { + wantFetches = 1 + } + if fetches != wantFetches { + t.Fatalf("fetches=%d, want %d", fetches, wantFetches) + } + _, release, err := hgithub.AcquireDestinationMerge(ctx, destinationDirectory(c), "acme/widget", "main") + if err != nil { + t.Fatalf("destination lease leaked after %s: %v", scenario, err) + } + release() + }) + } +} diff --git a/src/pkg/github/automerge/automerge_preparation_readiness_test.go b/src/pkg/github/automerge/automerge_preparation_readiness_test.go index 6ce6216a17..f450cefc00 100644 --- a/src/pkg/github/automerge/automerge_preparation_readiness_test.go +++ b/src/pkg/github/automerge/automerge_preparation_readiness_test.go @@ -103,6 +103,45 @@ func TestPreparationReadinessDistinguishesObservedPendingFromMissing(t *testing. } } +func TestPreparationReadinessRequiresSupportedLaneAndReviewEvidence(t *testing.T) { + api, c, _ := readinessFixture(t, false) + ctx := context.Background() + if ready, known := c.preparationReadiness(ctx, "acme", "widget", api.prs[2], stageLaneSelf); !ready || !known { + t.Fatalf("missing positive control: ready=%v known=%v", ready, known) + } + if ready, known := c.preparationReadiness(ctx, "acme", "widget", api.prs[2], stageLaneContributor); ready || known { + t.Fatalf("unsupported lane admitted: ready=%v known=%v", ready, known) + } + c.reviewGate.Verdict = nil + if ready, known := c.preparationReadiness(ctx, "acme", "widget", api.prs[2], stageLaneSelf); ready || known { + t.Fatalf("missing review provider admitted: ready=%v known=%v", ready, known) + } +} + +func TestPreparationFilesRejectsUnknownAndMissingPaths(t *testing.T) { + for _, invalid := range []string{"unknown-count", "empty-path", "missing-files"} { + t.Run(invalid, func(t *testing.T) { + api, srv := newPreparationAPI(t) + c := preparationEngine(srv.URL, NewBasePreparationStore("")) + ctx := context.Background() + if files, err := c.preparationFiles(ctx, "acme", "widget", api.prs[2]); err != nil || len(files) != 1 || files[0] != "file2.go" { + t.Fatalf("missing complete-path positive control: %v %v", files, err) + } + switch invalid { + case "unknown-count": + api.prs[2].ChangedFiles = nil + case "empty-path": + api.files[2][0].Filename = gh.Ptr("") + case "missing-files": + api.files[2] = nil + } + if files, err := c.preparationFiles(ctx, "acme", "widget", api.prs[2]); err == nil || files != nil { + t.Fatalf("incomplete path evidence accepted: %v %v", files, err) + } + }) + } +} + func TestPreparationAndBootUseTheSameDestinationDirectory(t *testing.T) { if got := destinationDirectory(New(nil, Options{})); got != DefaultDestinationMergeDirectory() { t.Fatalf("default sweep=%q boot/relay=%q", got, DefaultDestinationMergeDirectory()) From a7e2588e305389f4a1ab4c81fb1eccb765132965 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 2 Nov 2026 10:15:51 -0500 Subject: [PATCH 6/6] =?UTF-8?q?=F0=9F=90=9B=20fix:=20classify=20guard=20co?= =?UTF-8?q?ntention=20across=20remaining=20sweep=20lanes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Apply the owner-directed sentinel-only merge-guard reason to self-authored and trusted-author sweeps, retaining gone and unrelated fetch failures. Run the existing five reason-regression scenarios against all three guarded lanes and align pending-review documentation. Preserve the accepted whole-evaluation serialization boundary and deliberate hold. Signed-off-by: Andy Anderson --- docs/readiness-preparation.md | 6 +++--- .../automerge_destination_merge_test.go | 21 ++++++++++++++++++- .../automerge/automerge_sweep_trusted.go | 6 ++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/docs/readiness-preparation.md b/docs/readiness-preparation.md index 36eac56df3..b4402d6c12 100644 --- a/docs/readiness-preparation.md +++ b/docs/readiness-preparation.md @@ -5,8 +5,8 @@ lane gates on the exact head before requesting a base refresh. An unresolved refresh, ambiguous mutation outcome, or active repair retains exclusive preparation ownership. -A completed refresh can yield while its new head has a known pending CI -verdict. +A completed refresh can yield while its new head has a known pending CI or +review verdict. Observed `status-pending`/`check-pending` on an explicitly unprotected base is a known pending verdict too; missing required contexts, absent evidence, stale conclusions and failed API reads cannot establish readiness. @@ -32,7 +32,7 @@ larger or ambiguous fronts retain FIFO instead of spending unbounded API work. Final sweep admission and merge calls share a per-repository/base guard with the GitHub merge-request relay. Production uses stable flock inodes under `/data/destination-merges`, shared by writers using the same durable volume; -isolated test engines use process-local locks. Queued-lane contention returns +isolated test engines use process-local locks. Across sweep lanes, contention returns `base-preparation:merge-guard` with `ErrDestinationMergeBusy`, not a `fetch-pr` failure; the caller retries on a later sweep rather than retaining stale evidence while waiting. Unrelated fetch failures keep their existing reasons. diff --git a/src/pkg/github/automerge/automerge_destination_merge_test.go b/src/pkg/github/automerge/automerge_destination_merge_test.go index 5a2f7d5341..c3dfcb709a 100644 --- a/src/pkg/github/automerge/automerge_destination_merge_test.go +++ b/src/pkg/github/automerge/automerge_destination_merge_test.go @@ -15,6 +15,25 @@ import ( ) func TestQueuedSweepDestinationGuardReasons(t *testing.T) { + testSweepDestinationGuardReasons(t, func(c *Engine, ctx context.Context) (AutoMergeSweepEvent, string, error) { + return c.trySweepQueuedPR(ctx, "acme/widget", "acme", "widget", 2, hgithub.AutoMergeQueuedLabel) + }) +} + +func TestSelfAuthoredSweepDestinationGuardReasons(t *testing.T) { + testSweepDestinationGuardReasons(t, func(c *Engine, ctx context.Context) (AutoMergeSweepEvent, string, error) { + return c.trySweepSelfAuthoredPR(ctx, "acme/widget", "acme", "widget", 2, true) + }) +} + +func TestTrustedAuthorSweepDestinationGuardReasons(t *testing.T) { + testSweepDestinationGuardReasons(t, func(c *Engine, ctx context.Context) (AutoMergeSweepEvent, string, error) { + return c.trySweepTrustedAuthorPR(ctx, "acme/widget", "acme", "widget", 2, TrustedAuthorPolicy{}) + }) +} + +func testSweepDestinationGuardReasons(t *testing.T, sweep func(*Engine, context.Context) (AutoMergeSweepEvent, string, error)) { + t.Helper() for _, scenario := range []struct { name string status int @@ -61,7 +80,7 @@ func TestQueuedSweepDestinationGuardReasons(t *testing.T) { } defer release() } - _, reason, err := c.trySweepQueuedPR(ctx, "acme/widget", "acme", "widget", 2, hgithub.AutoMergeQueuedLabel) + _, reason, err := sweep(c, ctx) if reason != scenario.wantReason { t.Fatalf("reason=%q, want %q (err=%v)", reason, scenario.wantReason, err) } diff --git a/src/pkg/github/automerge/automerge_sweep_trusted.go b/src/pkg/github/automerge/automerge_sweep_trusted.go index 90f8d16d95..994b15b3a6 100644 --- a/src/pkg/github/automerge/automerge_sweep_trusted.go +++ b/src/pkg/github/automerge/automerge_sweep_trusted.go @@ -22,6 +22,9 @@ func (c *Engine) trySweepSelfAuthoredPR(ctx context.Context, displayRepo, owner, if isGitHubStatus(err, http.StatusNotFound) { return AutoMergeSweepEvent{}, "gone", nil } + if errors.Is(err, hgithub.ErrDestinationMergeBusy) { + return AutoMergeSweepEvent{}, basePreparationPrefix + "merge-guard", err + } return AutoMergeSweepEvent{}, "fetch-pr", err } defer unlock() @@ -581,6 +584,9 @@ func (c *Engine) trySweepTrustedAuthorPR(ctx context.Context, displayRepo, owner if isGitHubStatus(err, http.StatusNotFound) { return AutoMergeSweepEvent{}, "gone", nil } + if errors.Is(err, hgithub.ErrDestinationMergeBusy) { + return AutoMergeSweepEvent{}, basePreparationPrefix + "merge-guard", err + } return AutoMergeSweepEvent{}, "fetch-pr", err } defer unlock()