diff --git a/changelog.d/fixed-base-refresh-carry-multi-hop.md b/changelog.d/fixed-base-refresh-carry-multi-hop.md new file mode 100644 index 0000000000..1c35bbf3f5 --- /dev/null +++ b/changelog.d/fixed-base-refresh-carry-multi-hop.md @@ -0,0 +1 @@ +- The reviewer now carries a person's recorded decision across up to five consecutive hive base refreshes, not just one, so two App merge-from-base commits landing before any gate reads the PR no longer force a re-decision; every hop must still be an App-authored merge of the base that leaves the diff unchanged ([#15766](https://github.com/hivecommons/hive/issues/15766)). diff --git a/src/docs/hive-merge.md b/src/docs/hive-merge.md index 437196f78b..e0831daf13 100644 --- a/src/docs/hive-merge.md +++ b/src/docs/hive-merge.md @@ -224,7 +224,12 @@ that does not supersede it is the hive's own `require_current_base` refresh: a merge commit authored by the App whose parents are the decided head and a commit on the base branch, and whose three-dot diff against the base is file-for-file (name, status, blob, line counts, patch) identical to the -decided head's. Before any gate reads the new head — the reviewer-verdict +decided head's. Up to five such refreshes may stack before any gate reads +the PR: the carry walks the first-parent chain back from the new head and +every hop must itself pass the same check, so two App merge-from-base +commits in a row still carry while a content commit anywhere in the chain +does not ([#15766](https://github.com/hivecommons/hive/issues/15766)). +Before any gate reads the new head — the reviewer-verdict stage route, the automerge review gate, operator admission and the hold guard — the decision is re-recorded on it with `carried_from_head` naming the original, and one INFO line `decision carried over base refresh` is diff --git a/src/pkg/github/pr_decision_base_refresh.go b/src/pkg/github/pr_decision_base_refresh.go index b0fdb1f121..a1871adae7 100644 --- a/src/pkg/github/pr_decision_base_refresh.go +++ b/src/pkg/github/pr_decision_base_refresh.go @@ -25,6 +25,10 @@ const baseRefreshCacheLimit = 4096 // baseRefreshMergeParents is the parent count of a merge commit. const baseRefreshMergeParents = 2 +// baseRefreshMaxHops bounds how many consecutive hive base refreshes the +// carry walks back through to reach the decided head (hivecommons/hive#15766). +const baseRefreshMaxHops = 5 + // BaseRefreshRepos is the slice of the repositories API the base-refresh // check reads: one commit lookup and two three-dot compares per new head. type BaseRefreshRepos interface { @@ -58,7 +62,7 @@ func (b *BaseRefreshChecker) HeadIsBaseRefreshOf(ctx context.Context, api BaseRe if got, seen := b.Known(owner, repo, number, oldHead, newHead); seen { return got } - ok := headIsBaseRefreshOf(ctx, api, owner, repo, baseRef, oldHead, newHead, appBotLogin) + ok := headIsBaseRefreshChainOf(ctx, api, owner, repo, baseRef, oldHead, newHead, appBotLogin) b.mu.Lock() if b.cache == nil || len(b.cache) >= baseRefreshCacheLimit { b.cache = map[string]bool{} @@ -85,10 +89,42 @@ func baseRefreshKey(owner, repo string, number int, oldHead, newHead string) str return owner + "/" + repo + "#" + strconv.Itoa(number) + "@" + newHead + "<-" + oldHead } -func headIsBaseRefreshOf(ctx context.Context, api BaseRefreshRepos, owner, repo, baseRef, oldHead, newHead, appBotLogin string) bool { +// headIsBaseRefreshChainOf walks back from newHead along the first-parent +// chain for at most baseRefreshMaxHops hops. Each commit is fetched exactly +// once and must itself be a hive base refresh of its first parent +// (App-authored merge, base parent on the base branch, diff unchanged); the +// walk succeeds when a hop's non-base parent is oldHead. The first hop keeps +// the single-refresh tolerance of oldHead in either parent position. +func headIsBaseRefreshChainOf(ctx context.Context, api BaseRefreshRepos, owner, repo, baseRef, oldHead, newHead, appBotLogin string) bool { ctx = WithRESTCaller(ctx, "hive:decision_base_refresh") - commit, _, err := api.GetCommit(ctx, owner, repo, newHead, nil) - if err != nil || commit == nil || len(commit.Parents) != baseRefreshMergeParents { + cur := newHead + for hop := 0; hop < baseRefreshMaxHops; hop++ { + commit, _, err := api.GetCommit(ctx, owner, repo, cur, nil) + if err != nil || commit == nil || len(commit.Parents) != baseRefreshMergeParents { + return false + } + first, second := strings.TrimSpace(commit.Parents[0].GetSHA()), strings.TrimSpace(commit.Parents[1].GetSHA()) + prev := first + if oldHead == first || oldHead == second { + prev = oldHead + } + if prev == "" || prev == cur || !mergeIsBaseRefreshOf(ctx, api, owner, repo, baseRef, commit, prev, cur, appBotLogin) { + return false + } + if prev == oldHead { + return true + } + cur = prev + } + return false +} + +// mergeIsBaseRefreshOf is the single-hop check over an already-fetched +// commit (the merge commit at newHead): authored by the App, oldHead is one +// parent and the other parent is the merge-base with baseRef, and the +// three-dot diff against baseRef is unchanged from oldHead's. +func mergeIsBaseRefreshOf(ctx context.Context, api BaseRefreshRepos, owner, repo, baseRef string, commit *gh.RepositoryCommit, oldHead, newHead, appBotLogin string) bool { + if commit == nil || len(commit.Parents) != baseRefreshMergeParents { return false } if !CommitByGitHubLogin(commit, appBotLogin) { diff --git a/src/pkg/github/pr_decision_base_refresh_test.go b/src/pkg/github/pr_decision_base_refresh_test.go index c9ae74f50e..582b0840c1 100644 --- a/src/pkg/github/pr_decision_base_refresh_test.go +++ b/src/pkg/github/pr_decision_base_refresh_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "log/slog" + "strconv" "sync" "testing" @@ -225,6 +226,87 @@ func TestDecisionCarrierResolvesBaseOnDemand(t *testing.T) { } } +// refreshChain builds n consecutive App base refreshes on top of "aaaa": +// h1 refreshes aaaa, h2 refreshes h1, and so on. broken, when > 0, makes +// that hop's author someone other than the App. +func refreshChain(n, broken int, app string) (*fakeBaseRefreshRepos, string) { + same := []*gh.CommitFile{refreshFile("a.go", "blob1", "@@")} + api := &fakeBaseRefreshRepos{commits: map[string]*gh.RepositoryCommit{}, compares: map[string]*gh.CommitsComparison{"v5...aaaa": refreshCompare("0ld", same...)}} + prev := "aaaa" + for i := 1; i <= n; i++ { + sha, tip, login := "h"+strconv.Itoa(i), "tip"+strconv.Itoa(i), app + if i == broken { + login = "mendezr" + } + api.commits[sha] = refreshCommit(sha, login, prev, tip) + api.compares["v5..."+sha] = refreshCompare(tip, same...) + prev = sha + } + return api, prev +} + +// hivecommons/hive#15766: two (or more) App refreshes landed before any +// lane read the PR, so the decided head is not a direct parent. +func TestDecisionCarrierCarriesAcrossConsecutiveBaseRefreshes(t *testing.T) { + const app = "kubestellar-hive[bot]" + for _, tc := range []struct { + name string + hops int + broken int + want bool + }{ + {name: "two hops", hops: 2, want: true}, + {name: "depth bound reached", hops: baseRefreshMaxHops, want: true}, + {name: "beyond the depth bound", hops: baseRefreshMaxHops + 1, want: false}, + {name: "newest hop not by the App", hops: 3, broken: 3, want: false}, + {name: "middle hop not by the App", hops: 3, broken: 2, want: false}, + {name: "oldest hop not by the App", hops: 3, broken: 1, want: false}, + } { + t.Run(tc.name, func(t *testing.T) { + api, head := refreshChain(tc.hops, tc.broken, app) + store := review.NewHumanChangeRequests("") + if err := store.Record(review.HumanChangeRequest{Repo: "hivecommons/hive", Number: 15766, HeadSHA: "aaaa", Source: review.HumanChangeRequestSourceDecision, SourceID: 1, Actor: "clubanderson", Body: "approve", Kind: review.HumanChangeRequestKindApprove}); err != nil { + t.Fatal(err) + } + carrier := &DecisionCarrier{Store: store, API: api, AppBotLogin: app, Checker: &BaseRefreshChecker{}} + got, ok := carrier.Carry(context.Background(), "hivecommons", "hive", 15766, "v5", head) + if ok != tc.want { + t.Fatalf("carried = %v, want %v (%+v)", ok, tc.want, got) + } + if tc.want && (got.HeadSHA != head || got.CarriedFromHead != "aaaa" || !got.IsApproval()) { + t.Fatalf("carried record = %+v", got) + } + }) + } +} + +// A content commit between two refreshes breaks the chain. +func TestDecisionCarrierRefusesChainBrokenByContentCommit(t *testing.T) { + const app = "kubestellar-hive[bot]" + same := []*gh.CommitFile{refreshFile("a.go", "blob1", "@@")} + changed := []*gh.CommitFile{refreshFile("a.go", "blob9", "@@ +")} + api := &fakeBaseRefreshRepos{ + commits: map[string]*gh.RepositoryCommit{ + "h2": refreshCommit("h2", app, "h1", "tip2"), + "h1": refreshCommit("h1", app, "c0de", "tip1"), + "c0de": refreshCommit("c0de", "someone", "aaaa"), + }, + compares: map[string]*gh.CommitsComparison{ + "v5...aaaa": refreshCompare("0ld", same...), + "v5...h1": refreshCompare("tip1", changed...), + "v5...h2": refreshCompare("tip2", changed...), + }, + } + store := review.NewHumanChangeRequests("") + if err := store.Record(review.HumanChangeRequest{Repo: "hivecommons/hive", Number: 15766, HeadSHA: "aaaa", Source: review.HumanChangeRequestSourceDecision, SourceID: 1, Actor: "clubanderson", Body: "approve", Kind: review.HumanChangeRequestKindApprove}); err != nil { + t.Fatal(err) + } + carrier := &DecisionCarrier{Store: store, API: api, AppBotLogin: app, Checker: &BaseRefreshChecker{}} + if got, ok := carrier.Carry(context.Background(), "hivecommons", "hive", 15766, "v5", "h2"); ok { + t.Fatalf("decision carried across a content commit: %+v", got) + } +} + func TestSameChangedFilesIgnoresOrderButNotContent(t *testing.T) { a := []*gh.CommitFile{refreshFile("a.go", "1", "p1"), refreshFile("b.go", "2", "p2")} b := []*gh.CommitFile{refreshFile("b.go", "2", "p2"), refreshFile("a.go", "1", "p1")}