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-approval-carryover-first-seen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- An approval no longer carries onto a later head on a pusher-chosen commit date alone (follow-up to hivecommons/hive#16817). The date fallback now also requires that the hive had listed that commit as the PR's head by the time of the review; the first listing of each head is kept in a new persisted record, `pr-head-sightings.json` in the stage state directory. A commit with no listing on record (for example one from before the upgrade) carries nothing by date, so a backdated content push under a later base merge cannot inherit an approval. A review on a non-merge head, and the walk over diff-preserving base merges to the approved commit, are unchanged.
13 changes: 10 additions & 3 deletions src/docs/hive-merge.md
Original file line number Diff line number Diff line change
Expand Up @@ -964,9 +964,16 @@ A PR merges through this tier only when all of the following hold:
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
counts when GitHub reports it on a head that is not a merge, when it was
submitted after both the head commit's date and the hive's first listing
of that commit as the PR head, or when the head adds nothing but base
merges (by anyone) that leave the PR's diff unchanged since the commit it
approved (or since a commit both dated and first listed before the
review). The first listing of each head is kept in
`pr-head-sightings.json` in the stage state directory; a commit with no
listing on record (for example from before the upgrade) carries nothing by
date, so a backdated content push cannot inherit an approval. 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
Expand Down
31 changes: 31 additions & 0 deletions src/pkg/github/automerge/automerge_forgestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,37 @@ func pullRequestFromItem(it forgestate.Item) *gh.PullRequest {
// Without a source it lists /pulls as before. The cache and the targets
// are keyed by owner/name, never by the configured (possibly bare) name.
func (c *Engine) openPullRequests(ctx context.Context, owner, repoName string, targets SweepTargets) ([]*gh.PullRequest, string, error) {
prs, skip, err := c.listOpenPullRequestsForSweep(ctx, owner, repoName, targets)
if err == nil && skip == "" {
c.sightHeads(owner+"/"+repoName, prs)
}
return prs, skip, err
}

// sightHeads records the first time the hive listed each PR head
// (head_sightings.go).
func (c *Engine) sightHeads(fullRepo string, prs []*gh.PullRequest) {
heads := make(map[int]string, len(prs))
for _, pr := range prs {
if pr != nil {
heads[pr.GetNumber()] = pr.GetHead().GetSHA()
}
}
now := time.Now
if c.now != nil {
now = c.now
}
c.headSightingsOrShared().Sight(fullRepo, heads, now())
}

func (c *Engine) headSightingsOrShared() *HeadSightings {
if c != nil && c.headSightings != nil {
return c.headSightings
}
return sharedHeadSightings
}

func (c *Engine) listOpenPullRequestsForSweep(ctx context.Context, owner, repoName string, targets SweepTargets) ([]*gh.PullRequest, string, error) {
fullRepo := owner + "/" + repoName
if c.openPRs == nil {
prs, err := c.listOpenPullRequests(ctx, owner, repoName)
Expand Down
23 changes: 21 additions & 2 deletions src/pkg/github/automerge/automerge_review_head_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ package automerge
// 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.
// "Submitted at or after" a commit (the date fallback, for a merge head or a
// walked ancestor) needs both the commit's date and the hive's first
// sighting of it as the PR head to be no later than the review
// (contentPredates): a backdated content commit under a later base merge
// is refused, and so is a commit the hive has no sighting of.
// A review with no submitted_at keeps the old commit_id rule.

import (
Expand Down Expand Up @@ -96,7 +101,8 @@ func (c *Engine) walkApprovalToHead(ctx context.Context, owner, repo string, num
if err != nil || commit == nil {
return approvalReach{}, false
}
if reviewed == head && (len(commit.Parents) < 2 || !submitted.Before(commitDate(commit))) {
fullRepo := owner + "/" + repo
if reviewed == head && (len(commit.Parents) < 2 || c.contentPredates(fullRepo, number, head, commit, submitted)) {
// 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.
Expand All @@ -119,7 +125,7 @@ func (c *Engine) walkApprovalToHead(ctx context.Context, owner, repo string, num
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)) {
if reviewed == head && c.contentPredates(fullRepo, number, prev, commit, submitted) {
// GitHub moved the review off this commit onto the merges above.
return approvalReach{ok: true, carried: true}, true
}
Expand All @@ -128,6 +134,19 @@ func (c *Engine) walkApprovalToHead(ctx context.Context, owner, repo string, num
return approvalReach{}, true
}

// contentPredates is the date fallback: commit sha existed when the review
// was submitted at t. Its date is pusher-chosen, so the hive must also have
// listed it as the PR's head by t (head_sightings.go); a head with no
// sighting on record carries nothing by date (Doug's decision on the
// adjudicator's backdating finding, hivecommons/hive#16817).
func (c *Engine) contentPredates(fullRepo string, number int, sha string, commit *gh.RepositoryCommit, t time.Time) bool {
if t.Before(commitDate(commit)) {
return false
}
seen, ok := c.headSightingsOrShared().FirstSeen(fullRepo, number, sha)
return ok && !t.Before(seen)
}

func commitDate(commit *gh.RepositoryCommit) time.Time {
if commit == nil || commit.GetCommit() == nil {
return time.Time{}
Expand Down
68 changes: 64 additions & 4 deletions src/pkg/github/automerge/automerge_review_head_binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"sync"
"testing"
"time"
)

// The PR's commit graph for the binding tests. f166 is the content the owner
Expand Down Expand Up @@ -40,12 +41,22 @@ func newBindingAPI(t *testing.T, g *bindingGraph) *httptest.Server {
"merge1": bindingCommit("merge1", "2026-11-05T19:50:10Z", "f166", "b2"),
"content1": bindingCommit("content1", "2026-11-05T10:00:00Z", "f166"),
"merge2": bindingCommit("merge2", "2026-11-05T19:50:10Z", "content1", "b2"),
// backdated is a content push the hive first listed at 2026-11-05
// 10:00Z whose committer date claims 2026-11-03; merge3 merges the
// base into it.
"backdated": bindingCommit("backdated", "2026-11-03T00:00:00Z", "f166"),
"merge3": bindingCommit("merge3", "2026-11-05T19:50:10Z", "backdated", "b2"),
// evilmerge merges a side branch (not the base) with a backdated date.
"evilmerge": bindingCommit("evilmerge", "2026-11-03T00:00:00Z", "f166", "side"),
}
compares := map[string]map[string]any{
"f166": {"merge_base_commit": map[string]string{"sha": "b1"}, "files": prDiff},
"merge1": {"merge_base_commit": map[string]string{"sha": "b2"}, "files": prDiff},
"content1": {"merge_base_commit": map[string]string{"sha": "b1"}, "files": changedDiff},
"merge2": {"merge_base_commit": map[string]string{"sha": "b2"}, "files": changedDiff},
"f166": {"merge_base_commit": map[string]string{"sha": "b1"}, "files": prDiff},
"merge1": {"merge_base_commit": map[string]string{"sha": "b2"}, "files": prDiff},
"content1": {"merge_base_commit": map[string]string{"sha": "b1"}, "files": changedDiff},
"merge2": {"merge_base_commit": map[string]string{"sha": "b2"}, "files": changedDiff},
"backdated": {"merge_base_commit": map[string]string{"sha": "b1"}, "files": changedDiff},
"merge3": {"merge_base_commit": map[string]string{"sha": "b2"}, "files": changedDiff},
"evilmerge": {"merge_base_commit": map[string]string{"sha": "b1"}, "files": changedDiff},
}
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
g.mu.Lock()
Expand Down Expand Up @@ -89,16 +100,24 @@ func ownerReview(commit, submitted string) map[string]any {
// base merges to the content it approved.
func TestApprovalBindsToApprovedContent(t *testing.T) {
const approvedAt = "2026-11-04T01:17:18Z"
// The hive listed f166 as the head shortly after its push, and the
// backdated content push only on 11-05.
seen := map[string]string{"f166": "2026-11-02T23:50:00Z", "content1": "2026-11-05T10:01:00Z", "backdated": "2026-11-05T10:00:00Z", "merge1": "2026-11-05T19:51:00Z", "evilmerge": "2026-11-05T11:00:00Z"}
for _, tc := range []struct {
name string
review map[string]any
head string
unseen bool // no sightings on record (seen before the record existed)
wantOK bool
wantCarried bool
}{
{name: "submitted after the head commit", review: ownerReview("f166", approvedAt), head: "f166", wantOK: true},
{name: "non-merge head dated after the review (pusher clock ahead)", review: ownerReview("f166", "2026-11-02T23:40:00Z"), head: "f166", wantOK: true},
{name: "GitHub moved it onto a merge-only head", review: ownerReview("merge1", approvedAt), head: "merge1", wantOK: true, wantCarried: true},
{name: "moved onto a merge-only head, first sighting unknown", review: ownerReview("merge1", approvedAt), head: "merge1", unseen: true},
{name: "backdated content push under a later base merge", review: ownerReview("merge3", approvedAt), head: "merge3"},
{name: "backdated non-base merge head the hive saw after the review", review: ownerReview("evilmerge", approvedAt), head: "evilmerge"},
{name: "merge head approved after the hive saw it", review: ownerReview("merge1", "2026-11-06T00:00:00Z"), head: "merge1", wantOK: true},
{name: "GitHub moved it onto a head with a content delta", review: ownerReview("merge2", approvedAt), head: "merge2"},
{name: "on the previous head, base merge since", review: ownerReview("f166", approvedAt), head: "merge1", wantOK: true, wantCarried: true},
{name: "on the previous head, content push since", review: ownerReview("f166", approvedAt), head: "content1"},
Expand All @@ -111,6 +130,12 @@ func TestApprovalBindsToApprovedContent(t *testing.T) {
defer api.Close()
c := newAutoMergeSweepClient(api.URL)
c.SetMergerAuthorizer(func(login string) bool { return strings.EqualFold(login, "danathar") })
c.headSightings = NewHeadSightings("")
if !tc.unseen {
for sha, at := range seen {
c.headSightings.Sight("acme/widget", map[int]string{7: sha}, mustParseTime(t, at))
}
}
login, carried, ok := c.latestTrustedHumanApprovalReach(context.Background(), "acme", "widget", 7, tc.head)
if ok != tc.wantOK || carried != tc.wantCarried {
t.Fatalf("approval = %q carried=%v ok=%v, want ok=%v carried=%v", login, carried, ok, tc.wantOK, tc.wantCarried)
Expand Down Expand Up @@ -147,3 +172,38 @@ func TestWorkflowApprovalCommentSaysCarried(t *testing.T) {
t.Fatalf("plain comment:\n%s", plain)
}
}

// Sightings persist across a restart and keep the first time a head was
// listed; openPullRequests records every listed head.
func TestHeadSightingsPersistFirstSeen(t *testing.T) {
path := t.TempDir() + "/sightings.json"
s := NewHeadSightings(path)
first := mustParseTime(t, "2026-11-02T23:50:00Z")
s.Sight("Acme/Widget", map[int]string{7: "f166"}, first)
s.Sight("acme/widget", map[int]string{7: "f166"}, first.Add(time.Hour))
reloaded := NewHeadSightings(path)
if at, ok := reloaded.FirstSeen("acme/widget", 7, "f166"); !ok || !at.Equal(first) {
t.Fatalf("first seen after reload = %v %v, want %v", at, ok, first)
}
if _, ok := reloaded.FirstSeen("acme/widget", 7, "other"); ok {
t.Fatal("an unseen head must be unknown")
}

api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.URL.Path == "/repos/acme/widget/pulls" {
_ = json.NewEncoder(w).Encode([]map[string]any{{"number": 9, "state": "open", "head": map[string]string{"sha": "h9"}}})
return
}
w.WriteHeader(http.StatusNotFound)
}))
defer api.Close()
c := newAutoMergeSweepClient(api.URL)
c.headSightings = NewHeadSightings("")
if _, _, err := c.openPullRequests(context.Background(), "acme", "widget", nil); err != nil {
t.Fatal(err)
}
if _, ok := c.headSightings.FirstSeen("acme/widget", 9, "h9"); !ok {
t.Fatal("a listed head was not sighted")
}
}
3 changes: 3 additions & 0 deletions src/pkg/github/automerge/automerge_sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ type Engine struct {
// approvalReach memoises whether a trusted approving review reaches a
// head (automerge_review_head_binding.go).
approvalReach approvalReachCache
// headSightings overrides the process-wide first-sighting record of PR
// heads (head_sightings.go); nil shares it.
headSightings *HeadSightings

humanMergePathsMu sync.RWMutex
humanMergePaths func(repo string) []string
Expand Down
128 changes: 128 additions & 0 deletions src/pkg/github/automerge/head_sightings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package automerge

// Durable first sighting of each PR head (Doug's decision on hivecommons/hive#16817,
// the adjudicator's backdating finding).
//
// An approval reaches a later base-merge head by the date fallback only when
// the content it approved existed when it was given. A commit's date is
// whatever the pusher's git wrote, so it is trusted only together with the
// hive's own clock: the time the hive first listed that commit as the PR's
// head. HeadFirstSeenTracker holds a similar time in memory, pruned after a
// week and lost on restart; this record survives both, so a carry-over can
// be judged days later. A head with no record (seen before this existed, or
// never seen as a head) is unknown and carries nothing by date.

import (
"encoding/json"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"

"github.com/hivecommons/hive/pkg/stagelabels"
)

// headSightingsRetention drops sightings older than this: a PR head the
// hive first saw half a year ago is not going to carry an approval forward.
const headSightingsRetention = 180 * 24 * time.Hour

// HeadSightings is the persisted (repo#pr@sha → first seen) record.
type HeadSightings struct {
mu sync.Mutex
path string
loaded bool
first map[string]time.Time
}

var sharedHeadSightings = NewHeadSightings(filepath.Join(stagelabels.DefaultStateDir, "pr-head-sightings.json"))

// NewHeadSightings returns a record persisted at path ("" keeps it in memory).
func NewHeadSightings(path string) *HeadSightings {
return &HeadSightings{path: path, first: map[string]time.Time{}}
}

func headSightingKey(fullRepo string, number int, sha string) string {
return strings.ToLower(strings.TrimSpace(fullRepo)) + "#" + strconv.Itoa(number) + "@" + strings.TrimSpace(sha)
}

func (s *HeadSightings) loadLocked() {
if s.loaded {
return
}
s.loaded = true
if s.path == "" {
return
}
data, err := os.ReadFile(s.path)
if err != nil {
return
}
var file map[string]time.Time
if json.Unmarshal(data, &file) == nil {
for k, v := range file {
s.first[k] = v
}
}
}

func (s *HeadSightings) saveLocked() {
if s.path == "" {
return
}
data, err := json.Marshal(s.first)
if err != nil {
return
}
if os.MkdirAll(filepath.Dir(s.path), 0o755) != nil {
return
}
tmp := s.path + ".tmp"
if os.WriteFile(tmp, data, 0o644) == nil {
_ = os.Rename(tmp, s.path)
}
}

// Sight records now for each head not seen before and saves once.
func (s *HeadSightings) Sight(fullRepo string, heads map[int]string, now time.Time) {
if s == nil || len(heads) == 0 {
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.loadLocked()
changed := false
for number, sha := range heads {
if strings.TrimSpace(sha) == "" || number <= 0 {
continue
}
k := headSightingKey(fullRepo, number, sha)
if _, ok := s.first[k]; !ok {
s.first[k] = now
changed = true
}
}
if !changed {
return
}
for k, at := range s.first {
if now.Sub(at) > headSightingsRetention {
delete(s.first, k)
}
}
s.saveLocked()
}

// FirstSeen is when the hive first listed sha as the PR's head, false when
// it has no record.
func (s *HeadSightings) FirstSeen(fullRepo string, number int, sha string) (time.Time, bool) {
if s == nil {
return time.Time{}, false
}
s.mu.Lock()
defer s.mu.Unlock()
s.loadLocked()
at, ok := s.first[headSightingKey(fullRepo, number, sha)]
return at, ok
}
1 change: 1 addition & 0 deletions src/pkg/github/automerge/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestMain(m *testing.M) {
os.Exit(1)
}
sharedBasePreparation = NewBasePreparationStore(filepath.Join(dir, "base-preparation"))
sharedHeadSightings = NewHeadSightings(filepath.Join(dir, "pr-head-sightings.json"))

code := m.Run()
if err := os.RemoveAll(dir); err != nil {
Expand Down
Loading