Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-owner-approval-carryover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- An owner's approving review counts for a PR head only when it approved that content. GitHub moves an existing review onto a later merge of the base, so on projectbluefin/utah#1078 a review submitted 2026-11-04 01:17Z was read as approval of 9d8ceab, a commit made the next day, and released the workflow-path fence. Now the review counts when it was submitted at or after the head commit's date, or when the head adds only base merges that leave the PR's diff unchanged; the fence comment then says the approval was carried over a base merge. The same rule lets an approval on the previous head answer `requires_human` after such a merge (it is no longer re-asked), and the 085 **What to do** no longer asks for an approval that is already recorded.
18 changes: 14 additions & 4 deletions src/docs/hive-merge.md
Original file line number Diff line number Diff line change
Expand Up @@ -960,9 +960,17 @@ A PR merges through this tier only when all of the following hold:
has explicitly approved the current head
(`owner_approval_releases_workflow_fence`, default `true`,
[#14664](https://github.com/hivecommons/hive/issues/14664)). Explicit
approval is a GitHub pull-request review in state `APPROVED` whose commit
is the current head by a login the hive's merger authorizer trusts (the
same bar that releases a `requires_human` verdict), or a dashboard
approval is a GitHub pull-request review in state `APPROVED` that approved
the current head's content, by a login the hive's merger authorizer trusts
(the same bar that releases a `requires_human` verdict). GitHub moves an
existing review's commit onto a later merge of the base, so the review
counts only when it was submitted at or after the head commit's date, or
when the head adds nothing but base merges (by anyone) that leave the PR's
diff unchanged since the commit it approved; the audit comment then says
"carried over a base merge … (PR diff unchanged)"
(projectbluefin/utah#1078). The same rule lets an approval on the previous
head answer the swarm's `requires_human` after such a merge instead of
asking the owner again. The other forms are a dashboard
approval (`/hive decision approve`, which posts `/lgtm` + `/approve`
attributed to the maintainer and bound to the head) or a trusted merger's
own `/hive decision approve` comment posted after the hive first observed
Expand All @@ -982,7 +990,9 @@ A PR merges through this tier only when all of the following hold:
refuses as a reviewer) clears both gates with one command; a decision
recorded on the head after the comment stands instead. The audit comment
says whether the approval answered the verdict or which command still
does
does. When the approval is already recorded and the PR is parked for
another reason, **What to do** says there is nothing more to approve
instead of asking for it again
([Danathar/zfs-aurora-complex#254](https://github.com/Danathar/zfs-aurora-complex/pull/254)). When released
the lane logs `workflow-path fence released by owner approval login=…
head=…`, leaves one `<!-- hive:workflow-approval head=… by=… -->` audit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (c *Engine) workflowFenceReleased(ctx context.Context, displayRepo, owner,
}
if !hasCommentWithPrefix(comments, workflowApprovalMarker(headSHA)) {
c.info("workflow-path fence released by owner approval", "repo", displayRepo, "pr", number, "login", approver, "head", headSHA)
if err := c.commentWorkflowApproval(ctx, owner, repo, number, headSHA, approver, verdictState); err != nil {
if err := c.commentWorkflowApproval(ctx, owner, repo, number, headSHA, approver, verdictState, approval.CarriedOverBaseMerge); err != nil {
c.warn("workflow-path fence: could not post audit comment", "repo", displayRepo, "pr", number, "head", headSHA, "error", err)
}
}
Expand All @@ -149,6 +149,9 @@ type workflowApproval struct {
// AnswersVerdict: the typed comment was recorded as the person's
// answer to the reviewer's requires_human question on the head.
AnswersVerdict bool
// CarriedOverBaseMerge: the review approved an earlier commit and head
// adds only base merges that leave the PR's diff unchanged.
CarriedOverBaseMerge bool
}

// recordTypedApprovalDecision records a typed `/hive decision approve` as
Expand Down Expand Up @@ -226,8 +229,8 @@ func (c *Engine) typedDecisionApprovalAnswer(ctx context.Context, displayRepo, o
// bound to headSHA: an APPROVED review on the head first, then an approval
// comment (dashboard-posted or typed) among comments bound to it.
func (c *Engine) workflowFenceApproval(ctx context.Context, owner, repo string, pr *gh.PullRequest, number int, headSHA string, comments []*gh.IssueComment) (workflowApproval, bool) {
if login, ok := c.latestTrustedHumanApproval(ctx, owner, repo, number, headSHA); ok {
return workflowApproval{Login: login}, true
if login, carried, ok := c.latestTrustedHumanApprovalReach(ctx, owner, repo, number, headSHA); ok {
return workflowApproval{Login: login, CarriedOverBaseMerge: carried}, true
}
approval, ok := c.latestTrustedCommentApproval(owner, repo, pr, number, headSHA, comments)
if !ok {
Expand Down Expand Up @@ -343,23 +346,29 @@ func workflowApprovalMarker(headSHA string) string {
// owner told only "merges once the gates clear" while the verdict still
// held the PR waited hours for a merge that never came
// (Danathar/zfs-aurora-complex#254).
func workflowApprovalComment(headSHA, approver string, verdictState int) string {
func workflowApprovalComment(headSHA, approver string, verdictState int, carried bool) string {
gates := "the hive merges this PR itself once the usual gates (CI, soak, holds, review verdict) are clear."
switch verdictState {
case workflowApprovalVerdictAnswered:
gates = "the same approval answers the review swarm's `requires_human` question on this head, and the hive merges this PR itself once the usual gates (CI, soak, holds) are clear."
case workflowApprovalVerdictOpen:
gates = "the review swarm's `requires_human` verdict on this head still holds the PR. A trusted merger's `/hive decision approve` reply (or the dashboard's approval) answers it, as does a `/hive approve` reply posted after the verdict; then the hive merges this PR itself once the usual gates (CI, soak, holds) are clear."
}
approved := "approved head `" + shortSHA(headSHA) + "`"
if carried {
// The review was on an earlier commit; head only merges the base in
// (reviewReachesHead), so say so rather than claim this head.
approved = "approved this PR's change, carried over a base merge to head `" + shortSHA(headSHA) + "` (PR diff unchanged)"
}
return workflowApprovalMarker(headSHA) + " by=" + strings.ToLower(approver) + " -->\n" +
"@" + approver + " approved head `" + shortSHA(headSHA) + "`, which edits CI workflow / action files. That explicit owner approval releases the contributor lane's workflow-path fence: " + gates + " A new push re-arms the fence until the new head is approved again."
"@" + approver + " " + approved + ", which edits CI workflow / action files. That explicit owner approval releases the contributor lane's workflow-path fence: " + gates + " A new push re-arms the fence until the new head is approved again."
}

// commentWorkflowApproval posts the audit comment once per head: an existing
// marker on the PR, or a prior effects claim, suppresses it.
func (c *Engine) commentWorkflowApproval(ctx context.Context, owner, repo string, number int, headSHA, approver string, verdictState int) error {
func (c *Engine) commentWorkflowApproval(ctx context.Context, owner, repo string, number int, headSHA, approver string, verdictState int, carried bool) error {
marker := workflowApprovalMarker(headSHA)
body := workflowApprovalComment(headSHA, approver, verdictState)
body := workflowApprovalComment(headSHA, approver, verdictState, carried)
_, err := effects.Execute(hgithub.WithRESTCaller(ctx, workflowApprovalRESTCaller), c.mutation, effects.Claim{
Repo: owner + "/" + repo,
Kind: effects.KindIssueComment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func TestContributorLaneWorkflowFenceMarkerOncePerHead(t *testing.T) {
fx := validContributorFixture()
fx.files = workflowPathFiles()
fx.reviews = []map[string]any{ownerApprovedReview("clubanderson", "sha7")}
fx.prComments = []map[string]any{{"id": 5, "user": map[string]string{"login": testHiveAppBotLogin}, "body": workflowApprovalComment("sha7", "clubanderson", workflowApprovalVerdictNone)}}
fx.prComments = []map[string]any{{"id": 5, "user": map[string]string{"login": testHiveAppBotLogin}, "body": workflowApprovalComment("sha7", "clubanderson", workflowApprovalVerdictNone, false)}}
api := newContributorLaneAPI(t, &fx)
defer api.Close()
policy := defaultContributorLanePolicy()
Expand Down
13 changes: 12 additions & 1 deletion src/pkg/github/automerge/automerge_human_changes_requested.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,17 @@ func (c *Engine) latestTrustedHumanReview(ctx context.Context, owner, repo strin
opts.Page = resp.NextPage
}
c.noteStageReviews(owner, repo, number, seen)
approval := strings.EqualFold(state, "APPROVED")
var found *gh.PullRequestReview
for login, rv := range latest {
if !strings.EqualFold(rv.GetState(), state) || strings.TrimSpace(rv.GetCommitID()) != strings.TrimSpace(head) {
if !strings.EqualFold(rv.GetState(), state) {
continue
}
// A change request binds to its commit; an approval binds to the
// content it approved, which a base merge carries and a content push
// does not, whatever commit_id GitHub reports
// (automerge_review_head_binding.go).
if !approval && strings.TrimSpace(rv.GetCommitID()) != strings.TrimSpace(head) {
continue
}
if c.isHiveAppReviewAuthor(rv) || strings.HasSuffix(login, "[bot]") {
Expand All @@ -70,6 +78,9 @@ func (c *Engine) latestTrustedHumanReview(ctx context.Context, owner, repo strin
if !configured || !trusted {
continue
}
if approval && !c.reviewReachesHead(ctx, owner, repo, number, head, rv).ok {
continue
}
if found == nil || rv.GetSubmittedAt().After(found.GetSubmittedAt().Time) {
found = rv
}
Expand Down
12 changes: 10 additions & 2 deletions src/pkg/github/automerge/automerge_review_gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,21 @@ func reviewGateRecordLane(lane string) string {
}

func (c *Engine) latestTrustedHumanApproval(ctx context.Context, owner, repo string, number int, head string) (string, bool) {
login, _, ok := c.latestTrustedHumanApprovalReach(ctx, owner, repo, number, head)
return login, ok
}

// latestTrustedHumanApprovalReach is latestTrustedHumanApproval plus whether
// the approval reaches head only by being carried over base merges
// (reviewReachesHead), so the fence comment can say so.
func (c *Engine) latestTrustedHumanApprovalReach(ctx context.Context, owner, repo string, number int, head string) (string, bool, bool) {
rv, ok := c.latestTrustedHumanReview(ctx, owner, repo, number, head, "APPROVED")
if !ok {
return "", false
return "", false, false
}
login := strings.TrimSpace(hgithub.SafeGetLogin(rv.GetUser()))
c.noteStageTrustedHumanApproval(owner, repo, number, head, login)
return login, true
return login, c.reviewReachesHead(ctx, owner, repo, number, head, rv).carried, true
}

func (c *Engine) trustedHumanApprovalForRequiresHuman(ctx context.Context, owner, repo string, number int, head string, verdict ContributorLaneVerdict) (string, bool) {
Expand Down
139 changes: 139 additions & 0 deletions src/pkg/github/automerge/automerge_review_head_binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package automerge

// An approving review counts for the head only when it approved that
// content (hivecommons/hive#14664's "a new push re-arms the fence").
//
// GitHub moves a person's existing review onto a merge of the base into the
// branch: projectbluefin/utah#1078's review submitted 2026-11-04 01:17Z is
// reported on 9d8ceab, a base merge made 2026-11-05 19:50Z, and the hive
// said "@Danathar approved head 9d8ceab" and merged it. Read alone,
// commit_id cannot tell that from an approval of the head. The hive's own
// base refresh does the opposite: the review stays on the old head, and the
// owner's answer to requires_human was asked for again
// (projectbluefin/utah-packages#877).
//
// So a trusted APPROVED review reaches head when either
// - GitHub reports it on head and head is not a merge, or it was
// submitted at or after the head commit's date, or
// - head is a chain of base merges (BaseMergeParent: the PR's diff
// unchanged, whoever merged) back to the commit the review approved, or
// to a commit no newer than the review. The approval is then "carried
// over a base merge", and the fence comment says so.
// A review with no submitted_at keeps the old commit_id rule.

import (
"context"
"strconv"
"strings"
"sync"
"time"

gh "github.com/google/go-github/v72/github"
hgithub "github.com/hivecommons/hive/pkg/github"
)

// approvalCarryMaxHops bounds the base merges walked back from the head.
const approvalCarryMaxHops = 5

const approvalReachCacheLimit = 4096

// approvalReach is whether a review reaches a head, and whether only by
// being carried over base merges.
type approvalReach struct {
ok, carried bool
}

type approvalReachCache struct {
mu sync.Mutex
m map[string]approvalReach
}

func (c *approvalReachCache) get(key string) (approvalReach, bool) {
c.mu.Lock()
defer c.mu.Unlock()
r, ok := c.m[key]
return r, ok
}

func (c *approvalReachCache) put(key string, r approvalReach) {
c.mu.Lock()
defer c.mu.Unlock()
if c.m == nil || len(c.m) >= approvalReachCacheLimit {
c.m = map[string]approvalReach{}
}
c.m[key] = r
}

// reviewReachesHead answers for one APPROVED review. The answer for a fixed
// review and head never changes, so it is memoised; a lookup failure is
// not memoised and reads as false (the fence stays, the person is asked).
func (c *Engine) reviewReachesHead(ctx context.Context, owner, repo string, number int, head string, rv *gh.PullRequestReview) approvalReach {
head = strings.TrimSpace(head)
reviewed := strings.TrimSpace(rv.GetCommitID())
submitted := rv.GetSubmittedAt().Time
if submitted.IsZero() {
return approvalReach{ok: reviewed == head}
}
if c == nil || c.gh == nil || head == "" {
return approvalReach{}
}
key := owner + "/" + repo + "#" + strconv.Itoa(number) + "@" + head + "<-" + strconv.FormatInt(rv.GetID(), 10) + ":" + reviewed
if r, ok := c.approvalReach.get(key); ok {
return r
}
r, known := c.walkApprovalToHead(ctx, owner, repo, number, head, reviewed, submitted)
if known {
c.approvalReach.put(key, r)
}
return r
}

// walkApprovalToHead does the reads. known is false when GitHub could not be
// read, so the caller does not memoise the answer.
func (c *Engine) walkApprovalToHead(ctx context.Context, owner, repo string, number int, head, reviewed string, submitted time.Time) (approvalReach, bool) {
ctx = hgithub.WithRESTCaller(ctx, "hive:approval_head_binding")
commit, _, err := c.gh.Repositories.GetCommit(ctx, owner, repo, head, nil)
if err != nil || commit == nil {
return approvalReach{}, false
}
if reviewed == head && (len(commit.Parents) < 2 || !submitted.Before(commitDate(commit))) {
// GitHub moves a review only onto a merge, so a review it reports on
// a non-merge head approved that head, whatever the pusher's clock
// wrote as its date.
return approvalReach{ok: true}, true
}
pr, _, err := c.gh.PullRequests.Get(ctx, owner, repo, number)
if err != nil || pr == nil {
return approvalReach{}, false
}
baseRef := strings.TrimSpace(pr.GetBase().GetRef())
cur := head
for hop := 0; hop < approvalCarryMaxHops; hop++ {
prev, ok := hgithub.BaseMergeParent(ctx, c.gh.Repositories, owner, repo, baseRef, commit, cur)
if !ok {
return approvalReach{}, true
}
if prev == reviewed {
return approvalReach{ok: true, carried: true}, true
}
if commit, _, err = c.gh.Repositories.GetCommit(ctx, owner, repo, prev, nil); err != nil || commit == nil {
return approvalReach{}, false
}
if reviewed == head && !submitted.Before(commitDate(commit)) {
// GitHub moved the review off this commit onto the merges above.
return approvalReach{ok: true, carried: true}, true
}
cur = prev
}
return approvalReach{}, true
}

func commitDate(commit *gh.RepositoryCommit) time.Time {
if commit == nil || commit.GetCommit() == nil {
return time.Time{}
}
if d := commit.GetCommit().GetCommitter().GetDate().Time; !d.IsZero() {
return d
}
return commit.GetCommit().GetAuthor().GetDate().Time
}
Loading
Loading