diff --git a/changelog.d/fixed-15751-decision-carries-over-base-refresh.md b/changelog.d/fixed-15751-decision-carries-over-base-refresh.md new file mode 100644 index 0000000000..8715b7efce --- /dev/null +++ b/changelog.d/fixed-15751-decision-carries-over-base-refresh.md @@ -0,0 +1 @@ +- A recorded operator decision (dashboard Approve, `/hive decision …`) now follows a hive-authored PR onto the head the hive's own `require_current_base` refresh produced — a merge of the base by the App that leaves the PR's diff identical — so the reviewer route reads `decision_answered=true` instead of re-parking the PR at `stage/085-needs-decision`, and the review gate and hold guard accept the carried approval. Any other head change still supersedes the decision ([#15751](https://github.com/hivecommons/hive/issues/15751)). diff --git a/src/cmd/hive/hold_guard_test.go b/src/cmd/hive/hold_guard_test.go index 29ce4b9dbe..e982885bff 100644 --- a/src/cmd/hive/hold_guard_test.go +++ b/src/cmd/hive/hold_guard_test.go @@ -11,6 +11,7 @@ import ( "net/http/httptest" "os" "path/filepath" + "regexp" "strconv" "strings" "sync" @@ -78,6 +79,15 @@ type holdGuardServer struct { // (#14534: proving the merged parent of a hygiene merge is a base commit). baseContains map[string]bool compareCalls int + + // commitBySHA serves GET /repos/{o}/{r}/commits/{sha}, pulls serves + // GET /repos/{o}/{r}/pulls/{n} and compareResult overrides the compare + // payload for "base...head" (#15649: the decision carry proves a head + // is the hive's own base refresh before accepting an approval on the + // previous head). Unknown SHAs and PRs are 404s. + commitBySHA map[string]map[string]any + pulls map[int]map[string]any + compareResult map[string]map[string]any } func (s *holdGuardServer) handler(t *testing.T) http.Handler { @@ -89,12 +99,31 @@ func (s *holdGuardServer) handler(t *testing.T) http.Handler { case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/compare/"): s.compareCalls++ spec := r.URL.Path[strings.Index(r.URL.Path, "/compare/")+len("/compare/"):] + if payload, ok := s.compareResult[spec]; ok { + _ = json.NewEncoder(w).Encode(payload) + return + } _, head, _ := strings.Cut(spec, "...") status := "diverged" if s.baseContains[head] { status = "behind" } _ = json.NewEncoder(w).Encode(map[string]any{"status": status}) + case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/commits/") && !strings.Contains(r.URL.Path, "/pulls/"): + sha := r.URL.Path[strings.LastIndex(r.URL.Path, "/")+1:] + commit, ok := s.commitBySHA[sha] + if !ok { + http.NotFound(w, r) + return + } + _ = json.NewEncoder(w).Encode(commit) + case r.Method == http.MethodGet && holdGuardPullPath.MatchString(r.URL.Path): + pr, ok := s.pulls[pathPRNumber(t, r.URL.Path)] + if !ok { + http.NotFound(w, r) + return + } + _ = json.NewEncoder(w).Encode(pr) case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/commits"): if s.failCommits { http.Error(w, `{"message":"boom"}`, http.StatusInternalServerError) @@ -190,6 +219,9 @@ func (s *holdGuardServer) handler(t *testing.T) http.Handler { }) } +// holdGuardPullPath matches GET /repos/{o}/{r}/pulls/{n} and nothing under it. +var holdGuardPullPath = regexp.MustCompile(`/pulls/\d+$`) + func pathPRNumber(t *testing.T, path string) int { t.Helper() parts := strings.Split(path, "/") @@ -1074,6 +1106,77 @@ func TestEnforceHoldGuardOperatorApprovalOnCurrentHeadAcceptsHiveLift(t *testing } } +// hivecommons/hive#15649: the operator approved the PR on head c1 and the +// hive's require_current_base gate then merged the base into the branch — +// a merge commit by the App that left the PR's diff untouched. That head +// change is the hive's own doing, so the approval follows the PR onto m1 +// and the guard accepts the head. A merge by a person, or a refresh that +// changed the diff, is drift as before. +func TestEnforceHoldGuardOperatorApprovalCarriesOverHiveBaseRefresh(t *testing.T) { + files := func(blob string) []map[string]any { + return []map[string]any{{"filename": "deploy/monitoring.yaml", "status": "modified", "sha": blob, "additions": 2, "deletions": 0, "changes": 2, "patch": "@@ " + blob}} + } + for _, tc := range []struct { + name, mergeAuthor, newBlob string + carried bool + }{ + {name: "pure base refresh by the App", mergeAuthor: "kubestellar-hive[bot]", newBlob: "blob1", carried: true}, + {name: "base merged by a person", mergeAuthor: "odttlnt", newBlob: "blob1"}, + {name: "refresh that changed the diff", mergeAuthor: "kubestellar-hive[bot]", newBlob: "blob9"}, + } { + t.Run(tc.name, func(t *testing.T) { + store := newTestHoldGuardStore(t) + decisions := swapHoldGuardHumanChangeRequests(t) + recordHoldGuardDecision(t, decisions, "c1", "clubanderson", review.HumanChangeRequestKindApprove) + fake := &holdGuardServer{commits: map[int][]map[string]any{ + 7: {ghCommit("c1", "odttlnt", "deploy: monitoring"), ghCommit("m1", tc.mergeAuthor, "Merge branch 'main' into deploy")}, + }} + fake.timeline = map[int][]map[string]any{ + 7: gateLiftTimeline("sentinel-alert", "kubestellar-hive[bot]", "Bot", "2026-10-22T18:19:00Z"), + } + fake.pulls = map[int]map[string]any{7: {"number": 7, "head": map[string]any{"sha": "m1"}, "base": map[string]any{"ref": "main"}}} + fake.commitBySHA = map[string]map[string]any{"m1": { + "sha": "m1", "author": map[string]any{"login": tc.mergeAuthor}, "committer": map[string]any{"login": "web-flow"}, + "parents": []map[string]any{{"sha": "c1"}, {"sha": "b1"}}, + }} + fake.compareResult = map[string]map[string]any{ + "main...c1": {"status": "ahead", "merge_base_commit": map[string]any{"sha": "b0"}, "files": files("blob1")}, + "main...m1": {"status": "ahead", "merge_base_commit": map[string]any{"sha": "b1"}, "files": files(tc.newBlob)}, + } + client := newHoldGuardClient(t, fake) + client.SetAppBotLogin("kubestellar-hive[bot]") + cfg := escalationTestConfig() + cfg.Dashboard.AuthorizedUsers = []string{"clubanderson:owner"} + store.Snapshot("acme/widgets", 7, "c1", []holdguard.Commit{{SHA: "c1", Author: "odttlnt"}}) + + got := enforceHoldGuard(context.Background(), cfg, client, client, + actionableWith(github.PullRequest{Repo: "widgets", Number: 7, Author: "odttlnt", HeadSHA: "m1"}), discardLogger()) + if got["widgets/7"] == tc.carried { + t.Fatalf("re-review-gated=%v, want %v", got["widgets/7"], !tc.carried) + } + if tc.carried { + if len(fake.comments) != 0 || len(fake.labels) != 0 { + t.Fatalf("comments=%v labels=%v, want no drift comment and no re-hold", fake.comments, fake.labels) + } + if _, ok := store.Recorded("acme/widgets", 7); ok { + t.Fatal("snapshot must clear once the carried approval covers the head") + } + carried, ok := decisions.For("acme/widgets", 7, "m1") + if !ok || !carried.IsApproval() || carried.CarriedFromHead != "c1" || carried.Actor != "clubanderson" { + t.Fatalf("carried decision = %+v ok=%v", carried, ok) + } + return + } + if len(fake.labels) != 1 || fake.labels[0] != holdguard.ReHoldLabel { + t.Fatalf("labels=%v, want re-hold", fake.labels) + } + if _, ok := decisions.For("acme/widgets", 7, "c1"); !ok { + t.Fatal("the approval must stay on the head the person actually approved") + } + }) + } +} + // The approval must name the head on the branch now: one recorded on the // pre-push head, or by someone outside the merger tier, is not a review of // the current tree and the drift path stands. diff --git a/src/cmd/hive/main_helpers.go b/src/cmd/hive/main_helpers.go index 275e5db6d1..68886762f0 100644 --- a/src/cmd/hive/main_helpers.go +++ b/src/cmd/hive/main_helpers.go @@ -52,8 +52,11 @@ var holdGuardHumanChangeRequests = func() review.HumanChangeRequestStore { // the person reviewed the tree on the branch now, which the gate-lift // timeline cannot show when the hive removed the label for them. Fails // closed: no record, a change request, an unknown or untrusted actor, or an -// empty head never cover the head. -func holdGuardOperatorApproval(cfg *config.Config, repo string, number int, headSHA string) (string, bool) { +// empty head never cover the head. The one head change that does not +// supersede the record is the hive's own base refresh — a merge of the base +// branch by the App that leaves the PR's diff as the person saw it — which +// carries the approval onto the new head first (hivecommons/hive#15649). +func holdGuardOperatorApproval(ctx context.Context, cfg *config.Config, ghClient *github.Client, repo string, number int, headSHA string) (string, bool) { headSHA = strings.TrimSpace(headSHA) if cfg == nil || headSHA == "" { return "", false @@ -62,7 +65,7 @@ func holdGuardOperatorApproval(cfg *config.Config, repo string, number int, head if store == nil { return "", false } - req, ok := store.For(repo, number, headSHA) + req, ok := ghClient.RecordedDecisionFor(ctx, store, repo, number, headSHA) if !ok || !req.IsApproval() || !strings.EqualFold(strings.TrimSpace(req.HeadSHA), headSHA) { return "", false } @@ -201,7 +204,7 @@ func enforceHoldGuard( // gate label on their behalf, so the timeline below shows a hive // lift that can never cover the head. The head-keyed approval by a // trusted merger is the review the guard asks for: accept it. - if login, ok := holdGuardOperatorApproval(cfg, repo, pr.Number, pr.HeadSHA); ok { + if login, ok := holdGuardOperatorApproval(ctx, cfg, ghClient, repo, pr.Number, pr.HeadSHA); ok { store.Clear(repo, pr.Number) logger.Info("hold guard: current head approved by a trusted merger through the hive's decision record; head accepted", "repo", repo, "pr", pr.Number, diff --git a/src/docs/hive-merge.md b/src/docs/hive-merge.md index 778d5ed941..caaccc4b92 100644 --- a/src/docs/hive-merge.md +++ b/src/docs/hive-merge.md @@ -215,6 +215,27 @@ takes effect without a restart. outcome becomes `human-approved` and the merge proceeds. - **off**: no evaluation. +**A decision is head-scoped — except across the hive's own base refresh.** +A recorded operator decision (dashboard Approve, `/hive decision …`, a +hand-applied return label) names the exact head the person saw; a new head +is a new tree nobody has decided on, so the record is superseded and the +reviewer's `decision_requested` question stands again. The one head change +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 +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 +logged; the reviewer route then reads `decision_answered=true` instead of +parking the PR at `stage/085-needs-decision` a second time +([#15751](https://github.com/hivecommons/hive/issues/15751), seen on #15649). A content push, +a merge by anyone but the App, a rebase, a refresh whose diff differs, and +GitHub review approvals (which are never moved between heads) all keep the +record where it was. The answer for a (PR, head) pair is memoised, so the +check costs one commit read and two compares per new head. + Each evaluation writes one audit line, action `review_evaluated`, detail `lane= would_hold= reason= enforce= head=`, plus `human_approver= overrode=` when a human's diff --git a/src/pkg/github/automerge/automerge_contributor_adopt.go b/src/pkg/github/automerge/automerge_contributor_adopt.go index 2cd184345d..7d06c68a44 100644 --- a/src/pkg/github/automerge/automerge_contributor_adopt.go +++ b/src/pkg/github/automerge/automerge_contributor_adopt.go @@ -655,7 +655,7 @@ func (c *Engine) adoptContributorPR(ctx context.Context, store *stagelabels.Stor } if forced { c.info("stage labels: operator forced adoption; maintainer policy, admission and presence bypassed", "repo", obs.Display, "pr", obs.Number, "author", obs.Author, "by", forcedBy, "online", presence.Online, "policy", policy.maintainerPolicy()) - } else if c.adoptionRefusedByAdmission(store, key, obs, requested, now) { + } else if c.adoptionRefusedByAdmission(ctx, store, key, obs, requested, now) { return 0, nil } else if held, deferred := c.maintainerHoldsAdoption(ctx, obs, policy, requested, now); held || deferred { if held && !deferred { @@ -830,7 +830,7 @@ func (c *Engine) supersedeAdoptedPR(ctx context.Context, store *stagelabels.Stor rec.AdoptReason = reason // A person's approval of this head travels to the copy // (hivecommons/hive#15136 follow-up): adopting is not un-approving. - admittedBy := c.operatorAdmission(owner, repo, number, obs.HeadSHA) + admittedBy := c.operatorAdmission(ctx, owner, repo, number, obs.HeadSHA) if stored, ok := store.Get(key); ok && stored.AdoptDiffFence != "" { // A second takeover (dco-rehome, signoff-rehome, in-place-stalled) // carries the fence the first adoption recorded. diff --git a/src/pkg/github/automerge/automerge_contributor_lane.go b/src/pkg/github/automerge/automerge_contributor_lane.go index f78a190dc9..3e3f249ad8 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane.go +++ b/src/pkg/github/automerge/automerge_contributor_lane.go @@ -660,7 +660,7 @@ func (c *Engine) admitContributor(ctx context.Context, owner, repo string, pr *g if headSHA := pr.GetHead().GetSHA(); c.adoptedHeadOwnedByHive(owner, repo, pr.GetNumber(), headSHA) { return contributorClaimMatch{AdoptedHead: headSHA, ObservedTier: c.observedRegistryTier(author)}, nil } - if login := c.operatorAdmission(owner, repo, pr.GetNumber(), pr.GetHead().GetSHA()); login != "" { + if login := c.operatorAdmission(ctx, owner, repo, pr.GetNumber(), pr.GetHead().GetSHA()); login != "" { return contributorClaimMatch{OperatorLogin: login, ObservedTier: c.observedRegistryTier(author)}, nil } if tier := c.admittedRegistryTier(author, policy); tier != "" { diff --git a/src/pkg/github/automerge/automerge_decision_answered_test.go b/src/pkg/github/automerge/automerge_decision_answered_test.go index 00a56f8f5e..d9324be293 100644 --- a/src/pkg/github/automerge/automerge_decision_answered_test.go +++ b/src/pkg/github/automerge/automerge_decision_answered_test.go @@ -1,6 +1,7 @@ package automerge import ( + "context" "testing" "github.com/hivecommons/hive/pkg/review" @@ -39,7 +40,7 @@ func TestFoldRecordedDecisionAnswersVerdictRoute(t *testing.T) { {Lane: stageLaneSelf, Owner: "hivecommons", Repo: "hive", Number: 14628, HeadSHA: tc.head, Labels: tc.labels, Reason: contributorLaneReasonNoVerdict, Detail: stageDetail{VerdictKnown: true, Verdict: asked}}, {Lane: stageLaneContributor, Owner: "hivecommons", Repo: "hive", Number: 14628, HeadSHA: tc.head, Labels: tc.labels, Reason: contributorLaneReasonAppAuthored, Detail: stageDetail{VerdictKnown: true, Verdict: asked}}, } - c.foldRecordedDecision(list) + c.foldRecordedDecision(context.Background(), list) got, _, obs := resolveStage(list) if got.Stage != tc.wantStage || got.Reason != tc.wantReason { t.Fatalf("stage=%s reason=%q (lane %s), want %s %q", got.Stage, got.Reason, obs.Lane, tc.wantStage, tc.wantReason) diff --git a/src/pkg/github/automerge/automerge_decision_carry.go b/src/pkg/github/automerge/automerge_decision_carry.go new file mode 100644 index 0000000000..c31a622a9e --- /dev/null +++ b/src/pkg/github/automerge/automerge_decision_carry.go @@ -0,0 +1,52 @@ +package automerge + +import ( + "context" + "strings" + + hgithub "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/review" +) + +// decisionCarrier is the engine's carrier of recorded operator decisions +// over the hive's own base refresh (hivecommons/hive#15649). nil when the +// engine has no REST client or App identity to verify the refresh with: a +// decision then stands only on the head it was recorded on, as before. +func (c *Engine) decisionCarrier() *hgithub.DecisionCarrier { + if c == nil || c.humanChangeRequests == nil || c.gh == nil || c.transport == nil { + return nil + } + app := strings.TrimSpace(c.transport.AppBotLogin()) + if app == "" { + return nil + } + return &hgithub.DecisionCarrier{ + Store: c.humanChangeRequests, API: c.gh.Repositories, AppBotLogin: app, Logger: c.logger, Checker: &c.baseRefresh, + ResolveBase: func(ctx context.Context, owner, repo string, number int) (string, error) { + pr, _, err := c.gh.PullRequests.Get(hgithub.WithRESTCaller(ctx, "hive:decision_base_refresh"), owner, repo, number) + if err != nil { + return "", err + } + return pr.GetBase().GetRef(), nil + }, + } +} + +// recordedDecisionFor is review.DecisionAnswerFor for head on +// owner/repo#number, carrying the PR's newest decision onto head first when +// head is nothing but the hive's base refresh of the head it was recorded +// on. baseRef may be "" when the caller does not know it. +func (c *Engine) recordedDecisionFor(ctx context.Context, owner, repo string, number int, baseRef, head string) (review.HumanChangeRequest, bool) { + if c == nil || c.humanChangeRequests == nil { + return review.HumanChangeRequest{}, false + } + fullRepo := owner + "/" + repo + if req, ok := review.DecisionAnswerFor(c.humanChangeRequests, fullRepo, number, head); ok { + return req, true + } + carrier := c.decisionCarrier() + if carrier == nil { + return review.HumanChangeRequest{}, false + } + return carrier.Carry(ctx, owner, repo, number, baseRef, head) +} diff --git a/src/pkg/github/automerge/automerge_decision_carry_test.go b/src/pkg/github/automerge/automerge_decision_carry_test.go new file mode 100644 index 0000000000..ecab540d4a --- /dev/null +++ b/src/pkg/github/automerge/automerge_decision_carry_test.go @@ -0,0 +1,139 @@ +package automerge + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + + hgithub "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/review" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// baseRefreshAPI is the slice of GitHub the decision carry reads: the merge +// commit, the PR (for its base branch) and the base...head compares. +type baseRefreshAPI struct { + mu sync.Mutex + mergeAuthor string + oldHead string + newHead string + baseTip string + newFiles []map[string]any + calls map[string]int +} + +func refreshFiles(blob string) []map[string]any { + return []map[string]any{{"filename": "src/pkg/proxy/private.go", "status": "modified", "sha": blob, "additions": 3, "deletions": 1, "changes": 4, "patch": "@@ -1 +1 @@ " + blob}} +} + +func (a *baseRefreshAPI) handler(t *testing.T) http.Handler { + t.Helper() + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + a.mu.Lock() + defer a.mu.Unlock() + if a.calls == nil { + a.calls = map[string]int{} + } + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.Contains(path, "/compare/"): + a.calls["compare"]++ + spec := path[strings.Index(path, "/compare/")+len("/compare/"):] + _, head, _ := strings.Cut(spec, "...") + files, mergeBase := refreshFiles("blob1"), "0ld" + if head == a.newHead { + files, mergeBase = a.newFiles, a.baseTip + } + _ = json.NewEncoder(w).Encode(map[string]any{"status": "ahead", "merge_base_commit": map[string]any{"sha": mergeBase}, "files": files}) + case r.Method == http.MethodGet && strings.Contains(path, "/commits/"): + a.calls["commit"]++ + sha := path[strings.LastIndex(path, "/")+1:] + if sha != a.newHead { + http.NotFound(w, r) + return + } + _ = json.NewEncoder(w).Encode(map[string]any{"sha": sha, "author": map[string]any{"login": a.mergeAuthor}, "committer": map[string]any{"login": "web-flow"}, + "parents": []map[string]any{{"sha": a.oldHead}, {"sha": a.baseTip}}}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/reviews"): + _ = json.NewEncoder(w).Encode([]map[string]any{}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/pulls/15649"): + a.calls["pull"]++ + _ = json.NewEncoder(w).Encode(map[string]any{"number": 15649, "head": map[string]any{"sha": a.newHead}, "base": map[string]any{"ref": "v5"}}) + default: + t.Errorf("unexpected GitHub API call: %s %s", r.Method, path) + http.NotFound(w, r) + } + }) +} + +// hivecommons/hive#15649: the reviewer verdict route and the review gate +// read a `/hive decision approve` recorded on the previous head as the +// answer for the head the hive's own base refresh produced, and only then. +func TestRecordedDecisionCarriesOverHiveBaseRefresh(t *testing.T) { + const oldHead, newHead, baseTip = "aaaa", "72246f98", "b45e" + asked := ContributorLaneVerdict{Configured: true, Verdict: "requires_human", DecisionRequested: true, HiveAuthoredKnown: true, HiveAuthored: true} + for _, tc := range []struct { + name string + mergeAuthor string + newFiles []map[string]any + carried bool + }{ + {name: "pure base refresh by the App", mergeAuthor: testHiveAppBotLogin, newFiles: refreshFiles("blob1"), carried: true}, + {name: "base refresh that changed the diff", mergeAuthor: testHiveAppBotLogin, newFiles: refreshFiles("blob9")}, + {name: "merge by a person", mergeAuthor: "mendezr", newFiles: refreshFiles("blob1")}, + } { + t.Run(tc.name, func(t *testing.T) { + api := &baseRefreshAPI{mergeAuthor: tc.mergeAuthor, oldHead: oldHead, newHead: newHead, baseTip: baseTip, newFiles: tc.newFiles} + server := httptest.NewServer(api.handler(t)) + defer server.Close() + store := review.NewHumanChangeRequests("") + if err := store.Record(review.HumanChangeRequest{Repo: "hivecommons/hive", Number: 15649, HeadSHA: oldHead, Source: review.HumanChangeRequestSourceDecision, SourceID: 1, Actor: "clubanderson", Body: "approve", Kind: review.HumanChangeRequestKindApprove}); err != nil { + t.Fatal(err) + } + client := hgithub.NewClient("token", "hivecommons", []string{"hive"}, nil, server.URL) + client.SetAppBotLogin(testHiveAppBotLogin) + c := New(client, Options{HeadFirstSeen: NewHeadFirstSeenTracker(), HumanChangeRequests: store, MergerAuthorizer: func(login string) bool { return login == "clubanderson" }}) + + // Reviewer verdict route: the stage pass folds the recorded + // decision into the fresh requires_human verdict on the new head. + list := []stageObservation{{Lane: stageLaneSelf, Owner: "hivecommons", Repo: "hive", Number: 15649, HeadSHA: newHead, BaseRef: "v5", Reason: contributorLaneReasonNoVerdict, Detail: stageDetail{VerdictKnown: true, Verdict: asked}}} + c.foldRecordedDecision(context.Background(), list) + got, _, _ := resolveStage(list) + answered := list[0].Detail.Verdict.DecisionAnswered && list[0].Detail.Verdict.DecisionAnsweredHead == newHead + if answered != tc.carried { + t.Fatalf("decision_answered = %v on %s, want %v (verdict %+v)", answered, newHead, tc.carried, list[0].Detail.Verdict) + } + if parked := got.Stage == stagelabels.NeedsDecision; parked == tc.carried { + t.Fatalf("stage = %s %q, want parked at 085 = %v", got.Stage, got.Reason, !tc.carried) + } + + // Review gate: the same record releases requires_human for the + // sweep's merge decision. + approver, ok := c.trustedHumanApprovalForRequiresHuman(context.Background(), "hivecommons", "hive", 15649, newHead, asked) + if ok != tc.carried || (tc.carried && approver != "clubanderson") { + t.Fatalf("review gate approval = %q ok=%v, want carried=%v", approver, ok, tc.carried) + } + // Operator admission reads the same answer. + if admitted := c.operatorAdmission(context.Background(), "hivecommons", "hive", 15649, newHead); (admitted != "") != tc.carried { + t.Fatalf("operatorAdmission = %q, want carried=%v", admitted, tc.carried) + } + + api.mu.Lock() + commits, pulls := api.calls["commit"], api.calls["pull"] + api.mu.Unlock() + if commits != 1 { + t.Fatalf("merge commit fetched %d times across three gates, want once (memoised)", commits) + } + if pulls != 0 { + t.Fatalf("base branch known from the observation; PR fetched %d times, want 0", pulls) + } + if _, ok := store.For("hivecommons/hive", 15649, oldHead); ok == tc.carried { + t.Fatalf("old-head record present=%v, want superseded only when carried (%v)", ok, tc.carried) + } + }) + } +} diff --git a/src/pkg/github/automerge/automerge_operator_admission.go b/src/pkg/github/automerge/automerge_operator_admission.go index 64d639bf48..0471a562e6 100644 --- a/src/pkg/github/automerge/automerge_operator_admission.go +++ b/src/pkg/github/automerge/automerge_operator_admission.go @@ -1,6 +1,7 @@ package automerge import ( + "context" "fmt" "strings" "time" @@ -27,7 +28,7 @@ const contributorLaneReasonOperatorApproval = prstage.ContributorLaneReasonOpera // merger (`/hive decision approve`, or a dashboard Approve that landed // before the record existed). The second source is written back to the // record so the activity monitor shows the admission. -func (c *Engine) operatorAdmission(owner, repo string, number int, head string) string { +func (c *Engine) operatorAdmission(ctx context.Context, owner, repo string, number int, head string) string { head = strings.TrimSpace(head) if c == nil || head == "" { return "" @@ -41,7 +42,7 @@ func (c *Engine) operatorAdmission(owner, repo string, number int, head string) } } } - login, ok := c.recordedHumanApprovalForHead(owner, repo, number, head) + login, ok := c.recordedHumanApprovalForHead(ctx, owner, repo, number, head) if !ok { return "" } @@ -66,11 +67,11 @@ func (m contributorClaimMatch) admittedByOperator() bool { // conflicting, DCO missing): approval there means "approved to proceed", // so the hive adopts and carries the approval to its copy rather than // deadlocking between "merge as-is" and "cannot merge". -func (c *Engine) adoptionRefusedByAdmission(store *stagelabels.Store, key string, obs stageObservation, requested bool, now time.Time) bool { +func (c *Engine) adoptionRefusedByAdmission(ctx context.Context, store *stagelabels.Store, key string, obs stageObservation, requested bool, now time.Time) bool { if requested { return false } - login := c.operatorAdmission(obs.Owner, obs.Repo, obs.Number, obs.HeadSHA) + login := c.operatorAdmission(ctx, obs.Owner, obs.Repo, obs.Number, obs.HeadSHA) if login == "" { return false } diff --git a/src/pkg/github/automerge/automerge_operator_admission_test.go b/src/pkg/github/automerge/automerge_operator_admission_test.go index 4e5aee4d03..058f9d8bca 100644 --- a/src/pkg/github/automerge/automerge_operator_admission_test.go +++ b/src/pkg/github/automerge/automerge_operator_admission_test.go @@ -1,6 +1,7 @@ package automerge import ( + "context" "strings" "testing" "time" @@ -74,7 +75,7 @@ func TestOperatorAdmissionDerivedFromHumanApproval(t *testing.T) { t.Fatal(err) } h.observe(adoptPR(7, "aaa"), "check-failing:unit") - if got := h.e.operatorAdmission("acme", "widget", 7, "aaa"); got != tc.want { + if got := h.e.operatorAdmission(context.Background(), "acme", "widget", 7, "aaa"); got != tc.want { t.Fatalf("operatorAdmission = %q, want %q", got, tc.want) } rec, _ := h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7)) @@ -93,16 +94,16 @@ func TestOperatorAdmissionDerivedFromHumanApproval(t *testing.T) { // the adoption refusal is a no-op. func TestOperatorAdmissionAbsent(t *testing.T) { var nilEngine *Engine - if got := nilEngine.operatorAdmission("acme", "widget", 7, "aaa"); got != "" { + if got := nilEngine.operatorAdmission(context.Background(), "acme", "widget", 7, "aaa"); got != "" { t.Fatalf("nil engine admitted %q", got) } h, done := newAdoptHarness(t, ContributorNudgeOutcome{Mode: "comment"}, adoptPolicy()) defer done() h.observe(adoptPR(7, "aaa"), "check-failing:unit") - if got := h.e.operatorAdmission("acme", "widget", 7, "aaa"); got != "" { + if got := h.e.operatorAdmission(context.Background(), "acme", "widget", 7, "aaa"); got != "" { t.Fatalf("admitted without approval: %q", got) } - if got := h.e.operatorAdmission("acme", "widget", 7, ""); got != "" { + if got := h.e.operatorAdmission(context.Background(), "acme", "widget", 7, ""); got != "" { t.Fatalf("empty head admitted: %q", got) } } diff --git a/src/pkg/github/automerge/automerge_review_gate.go b/src/pkg/github/automerge/automerge_review_gate.go index 5671beb9b6..f2227a15a2 100644 --- a/src/pkg/github/automerge/automerge_review_gate.go +++ b/src/pkg/github/automerge/automerge_review_gate.go @@ -331,20 +331,22 @@ func (c *Engine) trustedHumanApprovalForRequiresHuman(ctx context.Context, owner // No live trusted approval on head: a persisted review-backed approval // is revoked, so drop it before reading the recorded decision. c.reconcileReviewBackedApproval(ctx, owner, repo, number, head) - return c.recordedHumanApprovalForHead(owner, repo, number, head) + return c.recordedHumanApprovalForHead(ctx, owner, repo, number, head) } // recordedHumanApprovalForHead is a bare `/hive decision approve` the un-park // sweep recorded against head (pr_unpark_decision.go): the owner answered the // swarm's question with "yes" in a comment rather than a GitHub review. It // releases requires_human the way an APPROVED review on the head does -// (hivecommons/hive#14679). -func (c *Engine) recordedHumanApprovalForHead(owner, repo string, number int, head string) (string, bool) { +// (hivecommons/hive#14679). An approval recorded on the previous head +// follows the PR when the hive's own base refresh is all that moved it +// (hivecommons/hive#15649). +func (c *Engine) recordedHumanApprovalForHead(ctx context.Context, owner, repo string, number int, head string) (string, bool) { if c == nil || c.humanChangeRequests == nil || strings.TrimSpace(head) == "" { return "", false } - req, ok := c.humanChangeRequests.For(owner+"/"+repo, number, strings.TrimSpace(head)) - if !ok || !req.IsApproval() || strings.TrimSpace(req.Actor) == "" { + req, ok := c.recordedDecisionFor(ctx, owner, repo, number, "", head) + if !ok || !req.IsApproval() { return "", false } login := strings.ToLower(strings.TrimSpace(req.Actor)) diff --git a/src/pkg/github/automerge/automerge_stage_labels.go b/src/pkg/github/automerge/automerge_stage_labels.go index b02624a933..45428e3cfd 100644 --- a/src/pkg/github/automerge/automerge_stage_labels.go +++ b/src/pkg/github/automerge/automerge_stage_labels.go @@ -31,7 +31,6 @@ import ( "github.com/hivecommons/hive/pkg/authorkind" "github.com/hivecommons/hive/pkg/delegatedecision" hgithub "github.com/hivecommons/hive/pkg/github" - "github.com/hivecommons/hive/pkg/review" "github.com/hivecommons/hive/pkg/stageengine" "github.com/hivecommons/hive/pkg/stageengine/prstage" "github.com/hivecommons/hive/pkg/stagelabels" @@ -547,8 +546,11 @@ func (c *Engine) noteStageHumanChangeRequest(owner, repo string, number int, hea // and the stage pass must not re-derive 085 from the stale decision-requested // verdict in that case — that was the 070↔085 flap on hive#14628 // (hivecommons/hive#14679). Reads the durable store, so it holds across -// restarts and across the drain that clears the tracker's details. -func (c *Engine) foldRecordedDecision(list []stageObservation) { +// restarts and across the drain that clears the tracker's details. A +// decision recorded on the previous head is carried onto this one first +// when the hive's own base refresh is all that moved the head +// (hivecommons/hive#15649). +func (c *Engine) foldRecordedDecision(ctx context.Context, list []stageObservation) { if c == nil || c.humanChangeRequests == nil { return } @@ -557,7 +559,7 @@ func (c *Engine) foldRecordedDecision(list []stageObservation) { if o.HeadSHA == "" { continue } - req, ok := review.DecisionAnswerFor(c.humanChangeRequests, o.Owner+"/"+o.Repo, o.Number, o.HeadSHA) + req, ok := c.recordedDecisionFor(ctx, o.Owner, o.Repo, o.Number, o.BaseRef, o.HeadSHA) if !ok { continue } @@ -880,7 +882,7 @@ func (c *Engine) ApplyStageLabels(ctx context.Context) (StageLabelResult, error) } result.Items++ c.foldHumanReturnLabel(ctx, store, list) - c.foldRecordedDecision(list) + c.foldRecordedDecision(ctx, list) verdict, role, obs := resolveStage(list) key := stageKey(obs.Owner, obs.Repo, obs.Number) if verdict.Rank == stageRankNone { diff --git a/src/pkg/github/automerge/automerge_stage_labels_test.go b/src/pkg/github/automerge/automerge_stage_labels_test.go index c81e4a006e..506cc2dbd8 100644 --- a/src/pkg/github/automerge/automerge_stage_labels_test.go +++ b/src/pkg/github/automerge/automerge_stage_labels_test.go @@ -123,6 +123,15 @@ func newStageAPI(t *testing.T, api *stageAPI) *httptest.Server { out = []map[string]any{} } _ = json.NewEncoder(w).Encode(out) + case r.Method == http.MethodGet && strings.HasPrefix(path, "/repos/acme/widget/pulls/") && !strings.Contains(strings.TrimPrefix(path, "/repos/acme/widget/pulls/"), "/"): + // The decision carry (#15649) asks the PR for its base branch + // before proving a new head is the hive's own base refresh. + n, _ := strconv.Atoi(strings.TrimPrefix(path, "/repos/acme/widget/pulls/")) + _ = json.NewEncoder(w).Encode(map[string]any{"number": n, "base": map[string]any{"ref": "main"}}) + case r.Method == http.MethodGet && strings.HasPrefix(path, "/repos/acme/widget/commits/"): + // No single-commit detail is seeded: a new head is never a base + // refresh here, so a recorded decision stays on its own head. + http.NotFound(w, r) case r.Method == http.MethodGet && strings.HasPrefix(path, "/repos/acme/widget/pulls/") && strings.HasSuffix(path, "/commits"): n, _ := strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(path, "/repos/acme/widget/pulls/"), "/commits")) api.commitReads++ diff --git a/src/pkg/github/automerge/automerge_sweep.go b/src/pkg/github/automerge/automerge_sweep.go index 2c9cfff8e1..f294efb585 100644 --- a/src/pkg/github/automerge/automerge_sweep.go +++ b/src/pkg/github/automerge/automerge_sweep.go @@ -269,6 +269,9 @@ type Engine struct { reviewGate *ReviewGate humanChangeRequests review.HumanChangeRequestStore + // baseRefresh memoises whether a PR head is the hive's own base refresh + // of the head a decision was recorded on (automerge_decision_carry.go). + baseRefresh hgithub.BaseRefreshChecker humanMergePathsMu sync.RWMutex humanMergePaths func(repo string) []string @@ -2259,7 +2262,10 @@ func (c *Engine) prefilterTrustedAuthorPR(owner, repo string, pr *gh.PullRequest if pr.GetHead() == nil || pr.GetHead().GetSHA() == "" { return "missing-head-sha" } - if _, ok := c.trustedAuthorOrOperatorDecision(owner, repo, pr, policy); !ok { + // The prefilter has no caller context; the admission read only reaches + // GitHub to carry a decision over a base refresh, and the lane repeats + // the read with its own context before merging. + if _, ok := c.trustedAuthorOrOperatorDecision(context.Background(), owner, repo, pr, policy); !ok { return "untrusted-author-role" } return "" @@ -2277,13 +2283,13 @@ const trustedAuthorRoleOperatorApprovedPrefix = "operator-approved:" // whose current head an operator admitted (the dashboard's "Approve and // let hive merge") is treated as trusted for this head, attributed to the // operator. Any other untrusted author stays refused (ok=false). -func (c *Engine) trustedAuthorOrOperatorDecision(owner, repo string, pr *gh.PullRequest, policy TrustedAuthorPolicy) (TrustedAuthorDecision, bool) { +func (c *Engine) trustedAuthorOrOperatorDecision(ctx context.Context, owner, repo string, pr *gh.PullRequest, policy TrustedAuthorPolicy) (TrustedAuthorDecision, bool) { author := hgithub.SafeGetLogin(pr.GetUser()) decision := c.trustedAuthorDecision(author, policy.RequireRole) if decision.Allowed { return decision, true } - login := c.operatorAdmission(owner, repo, pr.GetNumber(), pr.GetHead().GetSHA()) + login := c.operatorAdmission(ctx, owner, repo, pr.GetNumber(), pr.GetHead().GetSHA()) if login == "" { return decision, false } @@ -2332,7 +2338,7 @@ func (c *Engine) trySweepTrustedAuthorPR(ctx context.Context, displayRepo, owner if reason := c.prefilterTrustedAuthorPR(owner, repo, pr, policy); reason != "" { return AutoMergeSweepEvent{}, reason, nil } - decision, ok := c.trustedAuthorOrOperatorDecision(owner, repo, pr, policy) + decision, ok := c.trustedAuthorOrOperatorDecision(ctx, owner, repo, pr, policy) if !ok { return AutoMergeSweepEvent{}, "untrusted-author-role", nil } diff --git a/src/pkg/github/client.go b/src/pkg/github/client.go index 7afe628416..79e2f15c9b 100644 --- a/src/pkg/github/client.go +++ b/src/pkg/github/client.go @@ -501,6 +501,9 @@ type Client struct { stageEngineMu sync.Mutex stageWriter *stageWriter stageEngines map[*stagelabels.Store]stageengine.Reconciler + // baseRefresh is the decision carry's memo (pr_decision_base_refresh.go). + baseRefreshOnce sync.Once + baseRefresh *BaseRefreshChecker } func (c *Client) SetCanaryScanner(enabled, failClosed bool, reg *ioscan.CanaryRegistry, onLeak func(ioscan.CanaryLeak)) { diff --git a/src/pkg/github/pr_decision_base_refresh.go b/src/pkg/github/pr_decision_base_refresh.go new file mode 100644 index 0000000000..b0fdb1f121 --- /dev/null +++ b/src/pkg/github/pr_decision_base_refresh.go @@ -0,0 +1,306 @@ +package github + +import ( + "context" + "log/slog" + "sort" + "strconv" + "strings" + "sync" + + gh "github.com/google/go-github/v72/github" + + "github.com/hivecommons/hive/pkg/review" +) + +// DecisionCarriedOverBaseRefreshMessage is the one INFO line the carry logs +// when a recorded operator decision follows the PR onto a base-refreshed +// head (hivecommons/hive#15649). +const DecisionCarriedOverBaseRefreshMessage = "decision carried over base refresh" + +// baseRefreshCacheLimit bounds the per-process (pr, head) memo so a long +// run never grows it without limit; a reset simply re-asks GitHub. +const baseRefreshCacheLimit = 4096 + +// baseRefreshMergeParents is the parent count of a merge commit. +const baseRefreshMergeParents = 2 + +// 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 { + GetCommit(ctx context.Context, owner, repo, sha string, opts *gh.ListOptions) (*gh.RepositoryCommit, *gh.Response, error) + CompareCommits(ctx context.Context, owner, repo, base, head string, opts *gh.ListOptions) (*gh.CommitsComparison, *gh.Response, error) +} + +// BaseRefreshChecker answers whether a PR's new head is nothing but the +// hive's own refresh of the base branch onto the old head, memoised per +// (repo, pr, newHead, oldHead) because the answer for two fixed SHAs never +// changes and every merge lane asks it on every tick. +type BaseRefreshChecker struct { + mu sync.Mutex + cache map[string]bool +} + +// HeadIsBaseRefreshOf reports whether newHead on owner/repo#number is a +// merge commit authored by the App (appBotLogin) whose parents are oldHead +// and a commit on baseRef, and whose three-dot diff against baseRef is +// file-for-file identical to oldHead's. That is exactly the head the +// require_current_base gate produces when it merges the base into the +// branch: the PR's content is unchanged, only its base moved. Any other +// head change — a content commit (even one that also merged the base), a +// merge by anyone but the App, a rebase, a diff that differs — is false. +// Lookup failures are false: the caller falls back to asking the person. +func (b *BaseRefreshChecker) HeadIsBaseRefreshOf(ctx context.Context, api BaseRefreshRepos, owner, repo string, number int, baseRef, oldHead, newHead, appBotLogin string) bool { + oldHead, newHead, baseRef = strings.TrimSpace(oldHead), strings.TrimSpace(newHead), strings.TrimSpace(baseRef) + if b == nil || api == nil || oldHead == "" || newHead == "" || baseRef == "" || oldHead == newHead || strings.TrimSpace(appBotLogin) == "" { + return false + } + if got, seen := b.Known(owner, repo, number, oldHead, newHead); seen { + return got + } + ok := headIsBaseRefreshOf(ctx, api, owner, repo, baseRef, oldHead, newHead, appBotLogin) + b.mu.Lock() + if b.cache == nil || len(b.cache) >= baseRefreshCacheLimit { + b.cache = map[string]bool{} + } + b.cache[baseRefreshKey(owner, repo, number, oldHead, newHead)] = ok + b.mu.Unlock() + return ok +} + +// Known returns the memoised answer for (repo, pr, newHead, oldHead) and +// whether there is one, so callers can skip resolving inputs GitHub would +// otherwise have to be asked for. +func (b *BaseRefreshChecker) Known(owner, repo string, number int, oldHead, newHead string) (ok, seen bool) { + if b == nil { + return false, false + } + b.mu.Lock() + defer b.mu.Unlock() + ok, seen = b.cache[baseRefreshKey(owner, repo, number, strings.TrimSpace(oldHead), strings.TrimSpace(newHead))] + return ok, seen +} + +func baseRefreshKey(owner, repo string, number int, oldHead, newHead string) string { + return owner + "/" + repo + "#" + strconv.Itoa(number) + "@" + newHead + "<-" + oldHead +} + +func headIsBaseRefreshOf(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 { + return false + } + if !CommitByGitHubLogin(commit, appBotLogin) { + return false + } + first, second := strings.TrimSpace(commit.Parents[0].GetSHA()), strings.TrimSpace(commit.Parents[1].GetSHA()) + baseParent := "" + switch oldHead { + case first: + baseParent = second + case second: + baseParent = first + } + if baseParent == "" || baseParent == oldHead { + return false + } + newCmp, newFiles, err := compareAllFiles(ctx, api, owner, repo, baseRef, newHead) + if err != nil || newCmp == nil { + return false + } + // The merge-base of the base branch and the new head is the base commit + // that was merged in: that is what makes the other parent "a commit on + // the base branch" rather than anything someone pushed. + if strings.TrimSpace(newCmp.GetMergeBaseCommit().GetSHA()) != baseParent { + return false + } + _, oldFiles, err := compareAllFiles(ctx, api, owner, repo, baseRef, oldHead) + if err != nil { + return false + } + return sameChangedFiles(oldFiles, newFiles) +} + +// compareAllFiles walks every page of the three-dot compare base...head. +func compareAllFiles(ctx context.Context, api BaseRefreshRepos, owner, repo, base, head string) (*gh.CommitsComparison, []*gh.CommitFile, error) { + opts := &gh.ListOptions{PerPage: 100} + var first *gh.CommitsComparison + var files []*gh.CommitFile + for { + cmp, resp, err := api.CompareCommits(ctx, owner, repo, base, head, opts) + if err != nil { + return nil, nil, err + } + if first == nil { + first = cmp + } + if cmp != nil { + files = append(files, cmp.Files...) + } + if resp == nil || resp.NextPage == 0 { + return first, files, nil + } + opts.Page = resp.NextPage + } +} + +// sameChangedFiles reports whether two three-dot diffs touch the same files +// the same way: name, status, rename source, resulting blob, line counts +// and — when GitHub returned it for both — the patch text itself. +func sameChangedFiles(a, b []*gh.CommitFile) bool { + if len(a) != len(b) { + return false + } + byName := func(files []*gh.CommitFile) []*gh.CommitFile { + out := make([]*gh.CommitFile, 0, len(files)) + for _, f := range files { + if f != nil { + out = append(out, f) + } + } + sort.Slice(out, func(i, j int) bool { return out[i].GetFilename() < out[j].GetFilename() }) + return out + } + a, b = byName(a), byName(b) + if len(a) != len(b) { + return false + } + for i := range a { + x, y := a[i], b[i] + if x.GetFilename() != y.GetFilename() || x.GetStatus() != y.GetStatus() || x.GetPreviousFilename() != y.GetPreviousFilename() { + return false + } + if x.GetSHA() != y.GetSHA() || x.GetAdditions() != y.GetAdditions() || x.GetDeletions() != y.GetDeletions() || x.GetChanges() != y.GetChanges() { + return false + } + if x.Patch != nil && y.Patch != nil && x.GetPatch() != y.GetPatch() { + return false + } + } + return true +} + +// CommitByGitHubLogin reports whether the commit's GitHub-attributed author +// or committer is login ([bot] suffix tolerated, case-insensitive). Only the +// server-resolved logins count, never the forgeable git name or email. +func CommitByGitHubLogin(commit *gh.RepositoryCommit, login string) bool { + want := normalizeGitHubLogin(login) + if commit == nil || want == "" { + return false + } + return normalizeGitHubLogin(commit.GetAuthor().GetLogin()) == want || normalizeGitHubLogin(commit.GetCommitter().GetLogin()) == want +} + +func normalizeGitHubLogin(login string) string { + return strings.TrimSuffix(strings.ToLower(strings.TrimSpace(login)), "[bot]") +} + +// DecisionCarrier moves a person's recorded decision on a hive-authored PR +// forward when the hive itself changed the head by refreshing the base +// (hivecommons/hive#15649). The decision ledger is head-scoped on purpose — +// a new head is a new tree nobody has approved — but a require_current_base +// merge of the base branch by the App leaves the PR's diff exactly as the +// person saw it, so their approve / instruction still answers the reviewer's +// question and the hold guard's "who approved this head" alike. Anything +// else that moved the head keeps the ledger as it was. +type DecisionCarrier struct { + Store review.HumanChangeRequestStore + API BaseRefreshRepos + AppBotLogin string + Logger *slog.Logger + Checker *BaseRefreshChecker + // ResolveBase returns the PR's base branch when the caller does not + // know it; it is consulted only once a carry candidate exists. + ResolveBase func(ctx context.Context, owner, repo string, number int) (string, error) +} + +// Carry returns the decision standing on head, carrying the PR's newest +// decision forward first when head is a pure base refresh of the head it +// was recorded on. baseRef may be "" when unknown. The carried record keeps +// its source, actor, body and kind — so the lanes read it exactly as the +// original — and names the original head in CarriedFromHead. One INFO line +// is logged per carry; later lookups find the record and ask nothing. +func (d *DecisionCarrier) Carry(ctx context.Context, owner, repo string, number int, baseRef, head string) (review.HumanChangeRequest, bool) { + head = strings.TrimSpace(head) + if d == nil || d.Store == nil || head == "" { + return review.HumanChangeRequest{}, false + } + fullRepo := owner + "/" + repo + if req, ok := d.Store.For(fullRepo, number, head); ok { + return req, true + } + newest, ok := d.Store.(review.HumanChangeRequestNewestStore) + if !ok || d.API == nil { + return review.HumanChangeRequest{}, false + } + prev, found := newest.Newest(fullRepo, number) + if !found || prev.HeadSHA == head || !prev.CarriesOverBaseRefresh() { + return review.HumanChangeRequest{}, false + } + checker := d.Checker + if checker == nil { + checker = &BaseRefreshChecker{} + } + if refresh, seen := checker.Known(owner, repo, number, prev.HeadSHA, head); seen && !refresh { + return review.HumanChangeRequest{}, false + } else if !seen { + if baseRef = strings.TrimSpace(baseRef); baseRef == "" && d.ResolveBase != nil { + ref, err := d.ResolveBase(ctx, owner, repo, number) + if err != nil { + return review.HumanChangeRequest{}, false + } + baseRef = strings.TrimSpace(ref) + } + if !checker.HeadIsBaseRefreshOf(ctx, d.API, owner, repo, number, baseRef, prev.HeadSHA, head, d.AppBotLogin) { + return review.HumanChangeRequest{}, false + } + } + carried := prev + carried.HeadSHA = head + carried.CarriedFromHead = prev.HeadSHA + if err := d.Store.Record(carried); err != nil { + if d.Logger != nil { + d.Logger.Warn("decision could not be carried over base refresh", "repo", fullRepo, "pr", number, "from_head", prev.HeadSHA, "head", head, "error", err) + } + return review.HumanChangeRequest{}, false + } + if d.Logger != nil { + d.Logger.Info(DecisionCarriedOverBaseRefreshMessage, "repo", fullRepo, "pr", number, "from_head", prev.HeadSHA, "head", head, "by", prev.Actor, "kind", prev.Directive(), "source", prev.Source) + } + return carried, true +} + +// decisionCarrier is the Client's carrier over its own REST client and the +// process-wide decision ledger. +func (c *Client) decisionCarrier(store review.HumanChangeRequestStore) *DecisionCarrier { + if c == nil || c.client == nil { + return nil + } + c.baseRefreshOnce.Do(func() { c.baseRefresh = &BaseRefreshChecker{} }) + return &DecisionCarrier{ + Store: store, API: c.client.Repositories, AppBotLogin: c.AppBotLogin(), Logger: c.logger, Checker: c.baseRefresh, + ResolveBase: func(ctx context.Context, owner, repo string, number int) (string, error) { + pr, _, err := c.client.PullRequests.Get(WithRESTCaller(ctx, "hive:decision_base_refresh"), owner, repo, number) + if err != nil { + return "", err + } + return pr.GetBase().GetRef(), nil + }, + } +} + +// RecordedDecisionFor returns the decision in store standing on head of +// repo#number, after carrying the PR's newest decision over the hive's own +// base refresh when that is all that moved the head (see DecisionCarrier). +func (c *Client) RecordedDecisionFor(ctx context.Context, store review.HumanChangeRequestStore, repo string, number int, head string) (review.HumanChangeRequest, bool) { + if store == nil { + return review.HumanChangeRequest{}, false + } + carrier := c.decisionCarrier(store) + if carrier == nil { + return store.For(repo, number, strings.TrimSpace(head)) + } + owner, name := c.splitRepo(repo) + return carrier.Carry(ctx, owner, name, number, "", head) +} diff --git a/src/pkg/github/pr_decision_base_refresh_test.go b/src/pkg/github/pr_decision_base_refresh_test.go new file mode 100644 index 0000000000..c9ae74f50e --- /dev/null +++ b/src/pkg/github/pr_decision_base_refresh_test.go @@ -0,0 +1,245 @@ +package github + +import ( + "context" + "errors" + "log/slog" + "sync" + "testing" + + gh "github.com/google/go-github/v72/github" + + "github.com/hivecommons/hive/pkg/review" +) + +// fakeBaseRefreshRepos serves one PR's commit graph for the base-refresh +// check: the commits by SHA and the three-dot compare of base...head. +type fakeBaseRefreshRepos struct { + mu sync.Mutex + commits map[string]*gh.RepositoryCommit + compares map[string]*gh.CommitsComparison + calls int +} + +func (f *fakeBaseRefreshRepos) GetCommit(_ context.Context, _, _, sha string, _ *gh.ListOptions) (*gh.RepositoryCommit, *gh.Response, error) { + f.mu.Lock() + defer f.mu.Unlock() + f.calls++ + if c, ok := f.commits[sha]; ok { + return c, nil, nil + } + return nil, nil, errors.New("not found") +} + +func (f *fakeBaseRefreshRepos) CompareCommits(_ context.Context, _, _, base, head string, _ *gh.ListOptions) (*gh.CommitsComparison, *gh.Response, error) { + f.mu.Lock() + defer f.mu.Unlock() + f.calls++ + if c, ok := f.compares[base+"..."+head]; ok { + return c, nil, nil + } + return nil, nil, errors.New("not found") +} + +func refreshCommit(sha, login string, parents ...string) *gh.RepositoryCommit { + c := &gh.RepositoryCommit{SHA: gh.Ptr(sha), Author: &gh.User{Login: gh.Ptr(login)}, Committer: &gh.User{Login: gh.Ptr("web-flow")}} + for _, p := range parents { + c.Parents = append(c.Parents, &gh.Commit{SHA: gh.Ptr(p)}) + } + return c +} + +func refreshCompare(mergeBase string, files ...*gh.CommitFile) *gh.CommitsComparison { + return &gh.CommitsComparison{MergeBaseCommit: &gh.RepositoryCommit{SHA: gh.Ptr(mergeBase)}, Files: files} +} + +func refreshFile(name, blob, patch string) *gh.CommitFile { + return &gh.CommitFile{Filename: gh.Ptr(name), Status: gh.Ptr("modified"), SHA: gh.Ptr(blob), Additions: gh.Ptr(3), Deletions: gh.Ptr(1), Changes: gh.Ptr(4), Patch: gh.Ptr(patch)} +} + +type captureHandler struct { + slog.Handler + mu sync.Mutex + records []slog.Record +} + +func (h *captureHandler) Enabled(context.Context, slog.Level) bool { return true } +func (h *captureHandler) Handle(_ context.Context, r slog.Record) error { + h.mu.Lock() + defer h.mu.Unlock() + h.records = append(h.records, r) + return nil +} +func (h *captureHandler) WithAttrs([]slog.Attr) slog.Handler { return h } +func (h *captureHandler) WithGroup(string) slog.Handler { return h } + +func (h *captureHandler) count(msg string) int { + h.mu.Lock() + defer h.mu.Unlock() + n := 0 + for _, r := range h.records { + if r.Message == msg { + n++ + } + } + return n +} + +// hivecommons/hive#15649: the operator approved #15649 on head A; the +// require_current_base gate then merged v5 into the branch (a merge commit +// by the App that left the PR's diff untouched) and the reviewer re-asked +// the same question on head B. The decision follows the PR onto B — once — +// but any other head change still supersedes it. +func TestDecisionCarrierCarriesOverPureBaseRefresh(t *testing.T) { + const oldHead, newHead, baseTip, app = "aaaa", "72246f98", "b45e", "kubestellar-hive[bot]" + same := []*gh.CommitFile{refreshFile("src/pkg/proxy/private.go", "blob1", "@@ -1 +1 @@"), refreshFile("src/docs/hive-merge.md", "blob2", "@@ -2 +2 @@")} + api := &fakeBaseRefreshRepos{ + commits: map[string]*gh.RepositoryCommit{newHead: refreshCommit(newHead, app, oldHead, baseTip)}, + compares: map[string]*gh.CommitsComparison{"v5..." + oldHead: refreshCompare("0ld", same...), "v5..." + newHead: refreshCompare(baseTip, same[1], same[0])}, + } + store := review.NewHumanChangeRequests("") + if err := store.Record(review.HumanChangeRequest{Repo: "hivecommons/hive", Number: 15649, HeadSHA: oldHead, Source: review.HumanChangeRequestSourceDecision, SourceID: 1, Actor: "clubanderson", Body: "approve", Kind: review.HumanChangeRequestKindApprove}); err != nil { + t.Fatal(err) + } + logs := &captureHandler{} + carrier := &DecisionCarrier{Store: store, API: api, AppBotLogin: app, Logger: slog.New(logs), Checker: &BaseRefreshChecker{}} + + got, ok := carrier.Carry(context.Background(), "hivecommons", "hive", 15649, "v5", newHead) + if !ok || !got.IsApproval() || got.HeadSHA != newHead || got.CarriedFromHead != oldHead || got.Actor != "clubanderson" || got.SourceID != 1 { + t.Fatalf("carried = %+v ok=%v", got, ok) + } + if again, ok := store.For("hivecommons/hive", 15649, newHead); !ok || again.CarriedFromHead != oldHead { + t.Fatalf("store does not hold the carried decision: %+v ok=%v", again, ok) + } + if _, ok := store.For("hivecommons/hive", 15649, oldHead); ok { + t.Fatal("the old head's record must be superseded by the carried one") + } + calls := api.calls + if _, ok := carrier.Carry(context.Background(), "hivecommons", "hive", 15649, "v5", newHead); !ok { + t.Fatal("second lookup must find the carried decision") + } + if api.calls != calls { + t.Fatalf("second lookup hit GitHub: %d calls, want %d", api.calls, calls) + } + if n := logs.count(DecisionCarriedOverBaseRefreshMessage); n != 1 { + t.Fatalf("carry logged %d times, want once", n) + } +} + +func TestDecisionCarrierRefusesOtherHeadChanges(t *testing.T) { + const oldHead, newHead, baseTip, content, app = "aaaa", "bbbb", "b45e", "c0de", "kubestellar-hive[bot]" + same := []*gh.CommitFile{refreshFile("src/pkg/proxy/private.go", "blob1", "@@ -1 +1 @@")} + changed := []*gh.CommitFile{refreshFile("src/pkg/proxy/private.go", "blob9", "@@ -1 +1,2 @@")} + for _, tc := range []struct { + name string + source string + commits map[string]*gh.RepositoryCommit + compares map[string]*gh.CommitsComparison + }{ + { + name: "content commit pushed, then base merged", + commits: map[string]*gh.RepositoryCommit{newHead: refreshCommit(newHead, app, content, baseTip)}, + compares: map[string]*gh.CommitsComparison{"v5..." + oldHead: refreshCompare("0ld", same...), "v5..." + newHead: refreshCompare(baseTip, changed...)}, + }, + { + name: "base merged but the diff changed with it", + commits: map[string]*gh.RepositoryCommit{newHead: refreshCommit(newHead, app, oldHead, baseTip)}, + compares: map[string]*gh.CommitsComparison{"v5..." + oldHead: refreshCompare("0ld", same...), "v5..." + newHead: refreshCompare(baseTip, changed...)}, + }, + { + name: "merge commit not by the App", + commits: map[string]*gh.RepositoryCommit{newHead: refreshCommit(newHead, "mendezr", oldHead, baseTip)}, + compares: map[string]*gh.CommitsComparison{"v5..." + oldHead: refreshCompare("0ld", same...), "v5..." + newHead: refreshCompare(baseTip, same...)}, + }, + { + name: "other parent is not on the base branch", + commits: map[string]*gh.RepositoryCommit{newHead: refreshCommit(newHead, app, oldHead, content)}, + compares: map[string]*gh.CommitsComparison{"v5..." + oldHead: refreshCompare("0ld", same...), "v5..." + newHead: refreshCompare(baseTip, same...)}, + }, + { + name: "single-parent push", + commits: map[string]*gh.RepositoryCommit{newHead: refreshCommit(newHead, app, oldHead)}, + compares: map[string]*gh.CommitsComparison{"v5..." + oldHead: refreshCompare("0ld", same...), "v5..." + newHead: refreshCompare("0ld", same...)}, + }, + { + name: "GitHub review approvals never travel", + source: review.HumanChangeRequestSourceReview, + commits: map[string]*gh.RepositoryCommit{newHead: refreshCommit(newHead, app, oldHead, baseTip)}, + compares: map[string]*gh.CommitsComparison{"v5..." + oldHead: refreshCompare("0ld", same...), "v5..." + newHead: refreshCompare(baseTip, same...)}, + }, + } { + t.Run(tc.name, func(t *testing.T) { + api := &fakeBaseRefreshRepos{commits: tc.commits, compares: tc.compares} + store := review.NewHumanChangeRequests("") + source := tc.source + if source == "" { + source = review.HumanChangeRequestSourceDecision + } + if err := store.Record(review.HumanChangeRequest{Repo: "hivecommons/hive", Number: 15649, HeadSHA: oldHead, Source: source, SourceID: 1, Actor: "clubanderson", Body: "approve", Kind: review.HumanChangeRequestKindApprove}); err != nil { + t.Fatal(err) + } + logs := &captureHandler{} + carrier := &DecisionCarrier{Store: store, API: api, AppBotLogin: app, Logger: slog.New(logs), Checker: &BaseRefreshChecker{}} + if got, ok := carrier.Carry(context.Background(), "hivecommons", "hive", 15649, "v5", newHead); ok { + t.Fatalf("decision carried onto a head that is not a pure base refresh: %+v", got) + } + if _, ok := store.For("hivecommons/hive", 15649, oldHead); !ok { + t.Fatal("the old head's record must stand untouched") + } + if n := logs.count(DecisionCarriedOverBaseRefreshMessage); n != 0 { + t.Fatalf("carry logged %d times, want none", n) + } + calls := api.calls + carrier.Carry(context.Background(), "hivecommons", "hive", 15649, "v5", newHead) + if api.calls != calls { + t.Fatalf("negative answer not memoised: %d calls, want %d", api.calls, calls) + } + }) + } +} + +// Without a base branch from the caller the carrier asks the PR for it, +// and only once a carry candidate exists. +func TestDecisionCarrierResolvesBaseOnDemand(t *testing.T) { + const oldHead, newHead, baseTip, app = "aaaa", "bbbb", "b45e", "kubestellar-hive[bot]" + same := []*gh.CommitFile{refreshFile("a.go", "blob1", "@@")} + api := &fakeBaseRefreshRepos{ + commits: map[string]*gh.RepositoryCommit{newHead: refreshCommit(newHead, app, oldHead, baseTip)}, + compares: map[string]*gh.CommitsComparison{"v5..." + oldHead: refreshCompare("0ld", same...), "v5..." + newHead: refreshCompare(baseTip, same...)}, + } + store := review.NewHumanChangeRequests("") + resolved := 0 + carrier := &DecisionCarrier{Store: store, API: api, AppBotLogin: app, Checker: &BaseRefreshChecker{}, ResolveBase: func(context.Context, string, string, int) (string, error) { + resolved++ + return "v5", nil + }} + if _, ok := carrier.Carry(context.Background(), "hivecommons", "hive", 1, "", newHead); ok || resolved != 0 || api.calls != 0 { + t.Fatalf("no recorded decision must mean no lookups: ok=%v resolved=%d calls=%d", ok, resolved, api.calls) + } + if err := store.Record(review.HumanChangeRequest{Repo: "hivecommons/hive", Number: 1, HeadSHA: oldHead, Source: review.HumanChangeRequestSourceDecision, SourceID: 1, Actor: "clubanderson", Body: "approve", Kind: review.HumanChangeRequestKindApprove}); err != nil { + t.Fatal(err) + } + got, ok := carrier.Carry(context.Background(), "hivecommons", "hive", 1, "", newHead) + if !ok || got.HeadSHA != newHead || resolved != 1 { + t.Fatalf("carry = %+v ok=%v resolved=%d", got, ok, resolved) + } +} + +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")} + if !sameChangedFiles(a, b) { + t.Fatal("same files in another order must compare equal") + } + renamed := []*gh.CommitFile{refreshFile("a.go", "1", "p1"), refreshFile("c.go", "2", "p2")} + if sameChangedFiles(a, renamed) { + t.Fatal("a different file set must not compare equal") + } + noPatch := []*gh.CommitFile{refreshFile("a.go", "1", "p1"), {Filename: gh.Ptr("b.go"), Status: gh.Ptr("modified"), SHA: gh.Ptr("2"), Additions: gh.Ptr(3), Deletions: gh.Ptr(1), Changes: gh.Ptr(4)}} + if !sameChangedFiles(a, noPatch) { + t.Fatal("a patch GitHub omitted on one side must not fail the comparison when blob and counts agree") + } + if !CommitByGitHubLogin(refreshCommit("x", "Kubestellar-Hive"), "kubestellar-hive[bot]") || CommitByGitHubLogin(refreshCommit("x", "mendezr"), "kubestellar-hive[bot]") { + t.Fatal("App attribution must match the login case-insensitively, [bot] suffix aside, and nobody else") + } +} diff --git a/src/pkg/review/human_change_request.go b/src/pkg/review/human_change_request.go index a09835932a..3f88ad8955 100644 --- a/src/pkg/review/human_change_request.go +++ b/src/pkg/review/human_change_request.go @@ -72,6 +72,20 @@ type HumanChangeRequest struct { // URL is the review's or comment's HTML URL, when known. URL string `json:"url,omitempty"` RecordedAt time.Time `json:"recorded_at"` + // CarriedFromHead is the head the person actually decided on when the + // hive moved this record forward over its own base refresh: the new + // head is a merge of the base branch into CarriedFromHead that left the + // PR's diff untouched (hivecommons/hive#15649 → see CarryOverBaseRefresh). + CarriedFromHead string `json:"carried_from_head,omitempty"` +} + +// CarriesOverBaseRefresh reports whether the request may follow the PR onto +// a head that is a pure base refresh of the one it was recorded on. Only a +// person's own instruction travels — a `/hive decision`, a dashboard Approve +// or a hand-applied stage label. A GitHub review names one commit and is +// never moved to another. +func (r HumanChangeRequest) CarriesOverBaseRefresh() bool { + return r.Source != HumanChangeRequestSourceReview && strings.TrimSpace(r.Actor) != "" } // IsApproval reports whether the request is a bare approval rather than an @@ -258,6 +272,37 @@ func (s *HumanChangeRequests) Forget(repo string, number int, source string, sou return s.persist() } +// Newest returns the newest request recorded against repo#number at any +// head. It is how the base-refresh carry finds the decision the PR's +// previous head still holds. +func (s *HumanChangeRequests) Newest(repo string, number int) (HumanChangeRequest, bool) { + if s == nil { + return HumanChangeRequest{}, false + } + repo = strings.TrimSpace(repo) + s.mu.Lock() + defer s.mu.Unlock() + s.load() + var newest HumanChangeRequest + found := false + for _, item := range s.items { + if item.Number != number || !strings.EqualFold(item.Repo, repo) { + continue + } + if !found || item.RecordedAt.After(newest.RecordedAt) { + newest = item + found = true + } + } + return newest, found +} + +// HumanChangeRequestNewestStore is the optional store capability the +// base-refresh carry needs: the PR's newest decision regardless of head. +type HumanChangeRequestNewestStore interface { + Newest(repo string, number int) (HumanChangeRequest, bool) +} + // Lookup returns the request recorded from source/sourceID against // repo#number at any head: the cheap "already seen" test the un-park // sweep's restart reconciliation runs before it spends a PR read.