From e5bddfd4715f69a4803a4ff9a17a4ba62f764398 Mon Sep 17 00:00:00 2001 From: Danathar Date: Thu, 5 Nov 2026 17:51:35 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(automerge):=20a=20carried=20?= =?UTF-8?q?approval=20needs=20the=20hive's=20own=20first=20sighting=20of?= =?UTF-8?q?=20the=20commit,=20not=20just=20its=20date?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Doug's decision on the adjudicator's HUMAN DECISION on #16817 (pullrequestreview-5792808876): keep the carry-over, but close the backdating gap. The date fallback (merge head, walked ancestor) now also requires that the hive first listed the commit as the PR head no later than the review. HeadSightings persists repo#pr@sha -> first listing (pr-head-sightings.json in the stage state dir), written by openPullRequests, the listing every lane sweeps; no sighting on record means nothing carries by date (fail closed). A non-merge head's review and the base-merge walk to the reviewed commit are unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BQYnPGFJx81c8JXR3o1sg1 Signed-off-by: Danathar --- .../fixed-approval-carryover-first-seen.md | 1 + src/docs/hive-merge.md | 13 +- .../github/automerge/automerge_forgestate.go | 31 +++++ .../automerge_review_head_binding.go | 23 +++- .../automerge_review_head_binding_test.go | 68 +++++++++- src/pkg/github/automerge/automerge_sweep.go | 3 + src/pkg/github/automerge/head_sightings.go | 128 ++++++++++++++++++ src/pkg/github/automerge/main_test.go | 1 + 8 files changed, 259 insertions(+), 9 deletions(-) create mode 100644 changelog.d/fixed-approval-carryover-first-seen.md create mode 100644 src/pkg/github/automerge/head_sightings.go diff --git a/changelog.d/fixed-approval-carryover-first-seen.md b/changelog.d/fixed-approval-carryover-first-seen.md new file mode 100644 index 0000000000..52f49d2e2c --- /dev/null +++ b/changelog.d/fixed-approval-carryover-first-seen.md @@ -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. diff --git a/src/docs/hive-merge.md b/src/docs/hive-merge.md index 4043b48f62..a19ea86202 100644 --- a/src/docs/hive-merge.md +++ b/src/docs/hive-merge.md @@ -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 diff --git a/src/pkg/github/automerge/automerge_forgestate.go b/src/pkg/github/automerge/automerge_forgestate.go index 620fa72672..39982d9ceb 100644 --- a/src/pkg/github/automerge/automerge_forgestate.go +++ b/src/pkg/github/automerge/automerge_forgestate.go @@ -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) diff --git a/src/pkg/github/automerge/automerge_review_head_binding.go b/src/pkg/github/automerge/automerge_review_head_binding.go index a0b3ed715e..c239ad99a5 100644 --- a/src/pkg/github/automerge/automerge_review_head_binding.go +++ b/src/pkg/github/automerge/automerge_review_head_binding.go @@ -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 ( @@ -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. @@ -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 } @@ -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{} diff --git a/src/pkg/github/automerge/automerge_review_head_binding_test.go b/src/pkg/github/automerge/automerge_review_head_binding_test.go index f84c139604..b6ff551fe2 100644 --- a/src/pkg/github/automerge/automerge_review_head_binding_test.go +++ b/src/pkg/github/automerge/automerge_review_head_binding_test.go @@ -8,6 +8,7 @@ import ( "strings" "sync" "testing" + "time" ) // The PR's commit graph for the binding tests. f166 is the content the owner @@ -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() @@ -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"}, @@ -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) @@ -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") + } +} diff --git a/src/pkg/github/automerge/automerge_sweep.go b/src/pkg/github/automerge/automerge_sweep.go index 05cc237a09..8fddd76997 100644 --- a/src/pkg/github/automerge/automerge_sweep.go +++ b/src/pkg/github/automerge/automerge_sweep.go @@ -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 diff --git a/src/pkg/github/automerge/head_sightings.go b/src/pkg/github/automerge/head_sightings.go new file mode 100644 index 0000000000..476abf6244 --- /dev/null +++ b/src/pkg/github/automerge/head_sightings.go @@ -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 +} diff --git a/src/pkg/github/automerge/main_test.go b/src/pkg/github/automerge/main_test.go index 21245f358e..dafacb9364 100644 --- a/src/pkg/github/automerge/main_test.go +++ b/src/pkg/github/automerge/main_test.go @@ -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 {