diff --git a/changelog.d/fixed-adopted-fixer-reverts-pr.md b/changelog.d/fixed-adopted-fixer-reverts-pr.md
new file mode 100644
index 0000000000..ab5f50d864
--- /dev/null
+++ b/changelog.d/fixed-adopted-fixer-reverts-pr.md
@@ -0,0 +1 @@
+- The hive no longer merges an adopted PR after its own fixer undid the PR's change. On projectbluefin/utah#1091 the adopted fixer made CI pass by re-adding the 25 `baselines/triage.toml` entries the PR removed, leaving a docs-only diff, then announced "sweep will merge when green". Each hive push on an in-place adoption is now checked against the PR's diff at the contributor's head the adoption started from: if fewer than half of those changed lines remain, the push is refused (so is any push on top of it, and stale-red recovery never releases it), the PR goes to `stage/085-needs-decision` (`adoption-fixer-reverts-pr`) with a "Fixing CI would undo this PR's change" note, and the contributor lane will not merge that head. Until the check passes, the lane holds the head (`contributor-lane:adoption-intent-pending`), and an agent's "sweep will merge when green" note on a hive push is rewritten to say the check comes first. The scanner policy tells the fixer to park instead of pushing such a commit.
diff --git a/src/docs/labels-and-control-signals/stage-labels.md b/src/docs/labels-and-control-signals/stage-labels.md
index de13ccd764..1a4a381332 100644
--- a/src/docs/labels-and-control-signals/stage-labels.md
+++ b/src/docs/labels-and-control-signals/stage-labels.md
@@ -1050,7 +1050,26 @@ When a contributor-agent PR goes red the hive does not just relabel it (a human-
admits it under the hive's own authority
(`contributor-lane:adopted-by-hive`, `governor.labels.stage.adoption.auto_merge`,
default `true`) without the original author's trust tier, so 074 moves
- to 095/097 and merges instead of parking on a maintainer `/lgtm`. A later head keeps that ownership
+ to 095/097 and merges instead of parking on a maintainer `/lgtm`.
+ Each hive push is first checked against the PR's own change
+ (`automerge_adoption_intent.go`): the PR's diff against its base
+ (three-dot, so a merge of the base is not counted) must still carry at
+ least half of the lines the PR changed at the contributor's head the
+ adoption started from (`adopted_intent_head`; older records fall back to
+ the adopted head). Every push is judged against that head, so a push on
+ top of a refused one is refused too, and a chain of pushes cannot erode
+ the change below half. A push that undoes more is refused: the PR goes
+ to `stage/085-needs-decision` with reason `adoption-fixer-reverts-pr`
+ and one "Fixing CI would undo this PR's change" note per head, and the
+ contributor lane will not merge that head; stale-red recovery may re-run
+ its CI but never releases the refusal (projectbluefin/utah#1091: the
+ fixer re-added the 25 entries the PR removed). Until the check has
+ passed, the lane refuses the head as
+ `contributor-lane:adoption-intent-pending`, and the relay rewrites an
+ agent's `hive/awaiting-ci: … sweep will merge when green` note on a hive
+ push (recorded, or App-linked on GitHub) to say the check comes first;
+ an author's push keeps the note as written. A diff of 300 files or more
+ is not judged. A later head keeps that ownership
only when the commit's GitHub-linked author/committer is the App bot
(the stage store remembers SHAs that already passed that check; it is
not a separate evidence source)
diff --git a/src/pkg/dashboard/stage_tracker_adoption_fixer_reverts_test.go b/src/pkg/dashboard/stage_tracker_adoption_fixer_reverts_test.go
new file mode 100644
index 0000000000..6c2bf4b997
--- /dev/null
+++ b/src/pkg/dashboard/stage_tracker_adoption_fixer_reverts_test.go
@@ -0,0 +1,29 @@
+package dashboard
+
+import (
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/hivecommons/hive/pkg/stagelabels"
+)
+
+// projectbluefin/utah#1091: an adopted PR whose hive push undid its change
+// reads as a person's decision with that reason, not as the hive fixing it.
+func TestStageTrackerAdoptionFixerRevertsSaysWhy(t *testing.T) {
+ now := time.Date(2026, 11, 3, 21, 15, 0, 0, time.UTC)
+ s, store, status, key := orphanTrackerFixture(t, now, stagelabels.NeedsDecision, stagelabels.LaneReasonAdoptionFixerReverts, []string{"stage/085-needs-decision"})
+ store.MarkAdoptedInPlace(key, "bc3a6ea", "contributor-offline", now.Add(-time.Hour))
+ store.MarkAdoptionEscalated(key, "bc3a6ea", stagelabels.LaneReasonAdoptionFixerReverts)
+
+ it := trackerItem(t, s.buildStageTracker(status, store, now), "pr", 14947)
+ if !strings.Contains(it.Caption, "fixing CI would undo this PR's change") {
+ t.Fatalf("caption = %q", it.Caption)
+ }
+ if it.DecisionNeeded == nil || !strings.Contains(it.DecisionNeeded.Question, "undoes most of the PR's own change") {
+ t.Fatalf("decision_needed = %+v", it.DecisionNeeded)
+ }
+ if got := humanizeStageReason(stagelabels.LaneReasonAdoptionFixerReverts); !strings.Contains(got, "undo this PR's change") {
+ t.Fatalf("humanizeStageReason = %q", got)
+ }
+}
diff --git a/src/pkg/dashboard/stage_tracker_caption.go b/src/pkg/dashboard/stage_tracker_caption.go
index 33b4751b07..aeb6eff50c 100644
--- a/src/pkg/dashboard/stage_tracker_caption.go
+++ b/src/pkg/dashboard/stage_tracker_caption.go
@@ -354,6 +354,8 @@ func humanizeStageReason(reason string) string {
return "the hive's copies of this PR were closed without merging twice within an hour"
case stagelabels.LaneReasonAdoptionCopyFailed:
return "the hive's copy of this PR failed and this PR was reopened"
+ case stagelabels.LaneReasonAdoptionFixerReverts:
+ return "fixing CI would undo this PR's change: the hive's push removed most of what the PR changes"
case "held":
return "held by a hold label"
case "label:needs-rebase":
diff --git a/src/pkg/dashboard/stage_tracker_decision_pr.go b/src/pkg/dashboard/stage_tracker_decision_pr.go
index 9f9dfe02a9..639fc755cf 100644
--- a/src/pkg/dashboard/stage_tracker_decision_pr.go
+++ b/src/pkg/dashboard/stage_tracker_decision_pr.go
@@ -118,6 +118,8 @@ func decisionForPR(it StageTrackerItem, verdict *review.Aggregate, sentinelLabel
d.Question = "The hive's copies of this PR were closed without merging twice within an hour, so the hive stopped opening copies. Reopen the last copy (#" + strconv.Itoa(it.AdoptedBy) + "), give direction, or close it?"
case rebaseFailedTwice:
d.Question = "The hive adopted this PR but its fixer could not resolve the conflicts with the base in two attempts. Adopt the commits into a fresh branch, give direction, or close it as superseded?"
+ case lower == stagelabels.LaneReasonAdoptionFixerReverts:
+ d.Question = "The hive adopted this PR, but the commit it pushed to fix CI undoes most of the PR's own change, so the hive will not merge it. Wait for what the PR depends on, revert the hive's commit, give direction, or close it?"
case lower == stagelabels.LaneReasonAdoptionCopyFailed:
d.Question = "The hive's copy of this PR (#" + strconv.Itoa(it.AdoptedBy) + ") failed, so this PR was reopened. Ask the hive to supersede it again, give direction, or close it?"
case adoptionStalled:
diff --git a/src/pkg/github/automerge/automerge_adoption_intent.go b/src/pkg/github/automerge/automerge_adoption_intent.go
new file mode 100644
index 0000000000..774d49363d
--- /dev/null
+++ b/src/pkg/github/automerge/automerge_adoption_intent.go
@@ -0,0 +1,182 @@
+package automerge
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ gh "github.com/google/go-github/v72/github"
+
+ "github.com/hivecommons/hive/pkg/stagelabels"
+)
+
+// Intent check for a hive push on an in-place adopted PR
+// (projectbluefin/utah#1091). The adopted fixer is there to get the PR's own
+// change merged. On utah#1091 it made CI green by re-adding the 25
+// baselines/triage.toml entries the PR existed to remove, leaving a docs-only
+// diff, and the PR was one green run from merging. Each hive push is checked
+// before the head is taken as the hive's: the PR's net diff against its base
+// must keep at least half of the lines it changed before the push.
+
+// adoptionIntentMaxFiles is the compare page GitHub returns at most; a diff
+// that fills it is not judged (its tail is unknown), and the push stands.
+const adoptionIntentMaxFiles = 300
+
+// adoptionIntent is one push's verdict: Own is how many changed lines the PR
+// carried before the push, Kept how many of them its diff still carries.
+type adoptionIntent struct {
+ Own, Kept int
+ // Unjudged is set when the diff could not be judged (too large).
+ Unjudged bool
+}
+
+// Reverts reports whether the push undid most of the PR's own change.
+func (a adoptionIntent) Reverts() bool {
+ return !a.Unjudged && a.Own > 0 && a.Kept*2 < a.Own
+}
+
+// prChangeUnits is the PR's change as a multiset: one unit per added or
+// removed line of each file's patch ("path\x00+text"). A file GitHub sends
+// without a patch (binary, too large) counts as one unit for its status.
+func prChangeUnits(files []*gh.CommitFile) map[string]int {
+ units := map[string]int{}
+ for _, f := range files {
+ name := f.GetFilename()
+ patch := f.GetPatch()
+ if patch == "" {
+ units[name+"\x00file:"+f.GetStatus()]++
+ continue
+ }
+ for _, line := range strings.Split(patch, "\n") {
+ if line == "" || strings.HasPrefix(line, "+++") || strings.HasPrefix(line, "---") {
+ continue
+ }
+ if line[0] == '+' || line[0] == '-' {
+ units[name+"\x00"+line[:1]+strings.TrimSpace(line[1:])]++
+ }
+ }
+ }
+ return units
+}
+
+// judgeAdoptionIntent compares the PR's change before the push with its
+// change after.
+func judgeAdoptionIntent(before, after []*gh.CommitFile) adoptionIntent {
+ if len(before) >= adoptionIntentMaxFiles || len(after) >= adoptionIntentMaxFiles {
+ return adoptionIntent{Unjudged: true}
+ }
+ own, now := prChangeUnits(before), prChangeUnits(after)
+ var out adoptionIntent
+ for unit, n := range own {
+ out.Own += n
+ out.Kept += min(n, now[unit])
+ }
+ return out
+}
+
+// adoptedPushIntent reads the PR's diff against its base (three-dot, so a
+// merge of the base into the branch is not counted) at the head before the
+// push and at the pushed head.
+func (c *Engine) adoptedPushIntent(ctx context.Context, obs stageObservation, before string) (adoptionIntent, error) {
+ if obs.BaseRef == "" || before == "" || obs.HeadSHA == "" {
+ return adoptionIntent{Unjudged: true}, nil
+ }
+ files := func(head string) ([]*gh.CommitFile, error) {
+ cmp, _, err := c.gh.Repositories.CompareCommits(ctx, obs.Owner, obs.Repo, obs.BaseRef, head, &gh.ListOptions{PerPage: adoptionIntentMaxFiles})
+ if err != nil {
+ return nil, err
+ }
+ return cmp.Files, nil
+ }
+ was, err := files(before)
+ if err != nil {
+ return adoptionIntent{}, err
+ }
+ now, err := files(obs.HeadSHA)
+ if err != nil {
+ return adoptionIntent{}, err
+ }
+ return judgeAdoptionIntent(was, now), nil
+}
+
+// checkAdoptedPushIntent runs when the hive advanced an in-place adopted PR
+// to obs.HeadSHA. The push is judged against the PR's diff at the
+// contributor's head the adoption started from (Record.IntentBaseHead), not
+// the previous hive head: a refused head never becomes the baseline, and a
+// chain of pushes that each keep most of the previous one cannot erode the
+// change below half. A push that keeps the PR's change is taken as the
+// hive's head (MarkAdoptedHead). One that undoes it is escalated to 085 with
+// LaneReasonAdoptionFixerReverts: the contributor lane refuses to merge that
+// head and the author or a maintainer is told why, once; the head is taken
+// only after the note posts, so a failed post is re-checked and retried next
+// sweep. An unreadable diff leaves the head unmarked, so no merge admits it,
+// and is re-read next sweep.
+func (c *Engine) checkAdoptedPushIntent(ctx context.Context, store *stagelabels.Store, key string, obs stageObservation, prev stagelabels.Record) (int, error) {
+ base := prev.IntentBaseHead()
+ intent, err := c.adoptedPushIntent(ctx, obs, base)
+ if err != nil {
+ c.warn("stage labels: adopted PR advanced by the hive; diff unreadable, intent check deferred", "repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA, "error", err)
+ return 0, nil
+ }
+ if !intent.Reverts() {
+ if intent.Unjudged {
+ c.info("stage labels: adopted PR advanced by the hive; diff too large to judge intent", "repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA)
+ }
+ store.MarkAdoptedHead(key, obs.HeadSHA)
+ return 0, nil
+ }
+ reason := stagelabels.LaneReasonAdoptionFixerReverts
+ store.MarkAdoptionEscalated(key, obs.HeadSHA, reason)
+ c.warn("stage labels: hive push undoes the adopted PR's own change; refusing it and escalating to needs-decision", "repo", obs.Display, "pr", obs.Number, "intent_base", base, "head", obs.HeadSHA, "own_lines", intent.Own, "kept_lines", intent.Kept)
+ n := 0
+ if rec, _ := store.Get(key); !rec.AdoptionEscalationNoted(obs.HeadSHA, reason) {
+ n, err = c.postAdoptionEscalationNote(ctx, store, key, obs, reason, AdoptionFixerRevertsComment(obs, base, intent), "stage labels: fixer-reverts note failed")
+ if err != nil {
+ return n, err
+ }
+ }
+ store.MarkAdoptedHead(key, obs.HeadSHA)
+ return n, nil
+}
+
+// AdoptionFixerRevertsComment is the one note on a head whose hive push
+// undid the PR's change.
+func AdoptionFixerRevertsComment(obs stageObservation, before string, intent adoptionIntent) string {
+ return fmt.Sprintf("**Fixing CI would undo this PR's change.** The hive adopted this PR (`%s`) and pushed `%s` to fix it, but that push leaves %d of the %d lines this PR changed at `%s`, so the PR would no longer do what it set out to do. The hive will not merge `%s`.\n\n"+
+ "**Ask (author or maintainer):** decide what this PR should do: wait for what it depends on, revert the hive's commit and fix the cause another way, or close it. A new push re-checks.\n\n"+
+ "Moving to `%s` so a person sees it.",
+ stagelabels.AdoptedByHive.Label(), shortSHA(obs.HeadSHA), intent.Kept, intent.Own, shortSHA(before), shortSHA(obs.HeadSHA), stagelabels.NeedsDecision.Label())
+}
+
+// adoptionIntentRefused reports whether the stage store refuses headSHA as
+// the hive's adopted head: its push undid the PR's change, or the hive
+// pushed it and the intent check has not passed yet.
+func adoptionIntentRefused(rec stagelabels.Record, headSHA string) string {
+ if headSHA == "" || !rec.AdoptedInPlace() {
+ return ""
+ }
+ if rec.AdoptionEscalated(headSHA) && rec.AdoptEscalatedReason == stagelabels.LaneReasonAdoptionFixerReverts {
+ return stagelabels.LaneReasonAdoptionFixerReverts
+ }
+ if rec.HivePushedHead(headSHA) && rec.AdoptedHead != headSHA {
+ return contributorLaneReasonAdoptionIntentPending
+ }
+ return ""
+}
+
+// contributorLaneReasonAdoptionIntentPending: the hive pushed this head on
+// an adopted PR and has not yet checked it keeps the PR's change.
+const contributorLaneReasonAdoptionIntentPending = "contributor-lane:adoption-intent-pending"
+
+// adoptionIntentGate is the contributor lane's refusal for such a head,
+// whatever else would admit it.
+func (c *Engine) adoptionIntentGate(owner, repo string, number int, headSHA string) string {
+ if c == nil {
+ return ""
+ }
+ rec, ok := c.stageStoreOrShared().Get(stageKey(owner, repo, number))
+ if !ok {
+ return ""
+ }
+ return adoptionIntentRefused(rec, headSHA)
+}
diff --git a/src/pkg/github/automerge/automerge_adoption_intent_test.go b/src/pkg/github/automerge/automerge_adoption_intent_test.go
new file mode 100644
index 0000000000..ba82ca1bf2
--- /dev/null
+++ b/src/pkg/github/automerge/automerge_adoption_intent_test.go
@@ -0,0 +1,333 @@
+package automerge
+
+import (
+ "strings"
+ "testing"
+ "time"
+
+ gh "github.com/google/go-github/v72/github"
+
+ "github.com/hivecommons/hive/pkg/stagelabels"
+)
+
+// projectbluefin/utah#1091: the PR removes 25 resolved entries from
+// baselines/triage.toml and adds 10 lines of docs. The adopted fixer's
+// a75a567 put all 25 entries back, leaving a docs-only diff.
+func utah1091Files(withTriage bool, extraFix string) []map[string]string {
+ docs := map[string]string{"filename": "docs/skills/package-contract/references/factory-consumption.md", "status": "modified",
+ "patch": "@@ -1,0 +1,10 @@\n" + strings.Repeat("+factory consumption note\n", 9) + "+see package-contract.md"}
+ files := []map[string]string{docs}
+ if withTriage {
+ var b strings.Builder
+ b.WriteString("@@ -1,25 +0,0 @@\n")
+ for i := 0; i < 25; i++ {
+ b.WriteString("-[[missing]] package = \"pkg-" + string(rune('a'+i)) + "\"\n")
+ }
+ files = append(files, map[string]string{"filename": "baselines/triage.toml", "status": "modified", "patch": strings.TrimSuffix(b.String(), "\n")})
+ }
+ if extraFix != "" {
+ files = append(files, map[string]string{"filename": "tests/triage_test.py", "status": "modified", "patch": "@@ -3,1 +3,1 @@\n-" + extraFix + "_old\n+" + extraFix})
+ }
+ return files
+}
+
+func commitFiles(in []map[string]string) []*gh.CommitFile {
+ out := make([]*gh.CommitFile, 0, len(in))
+ for _, f := range in {
+ out = append(out, &gh.CommitFile{Filename: gh.Ptr(f["filename"]), Status: gh.Ptr(f["status"]), Patch: gh.Ptr(f["patch"])})
+ }
+ return out
+}
+
+func TestJudgeAdoptionIntent(t *testing.T) {
+ before := commitFiles(utah1091Files(true, ""))
+ for _, tc := range []struct {
+ name string
+ after []*gh.CommitFile
+ reverts bool
+ kept int
+ }{
+ {name: "utah#1091: the 25 removals put back, docs only", after: commitFiles(utah1091Files(false, "")), reverts: true, kept: 10},
+ {name: "a normal fix keeps the PR's change and adds its own", after: commitFiles(utah1091Files(true, "assert_missing")), reverts: false, kept: 35},
+ {name: "unchanged diff (a base merge)", after: before, reverts: false, kept: 35},
+ {name: "everything reverted", after: nil, reverts: true, kept: 0},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ got := judgeAdoptionIntent(before, tc.after)
+ if got.Reverts() != tc.reverts || got.Own != 35 || got.Kept != tc.kept {
+ t.Fatalf("intent = %+v reverts=%v, want own 35 kept %d reverts %v", got, got.Reverts(), tc.kept, tc.reverts)
+ }
+ })
+ }
+ // Exactly half kept is not "most reverted".
+ half := judgeAdoptionIntent(commitFiles([]map[string]string{{"filename": "a", "patch": "+x\n+y"}}), commitFiles([]map[string]string{{"filename": "a", "patch": "+x"}}))
+ if half.Reverts() {
+ t.Fatalf("half kept read as reverted: %+v", half)
+ }
+ // A file without a patch (binary) is one unit for its status.
+ bin := judgeAdoptionIntent(commitFiles([]map[string]string{{"filename": "logo.png", "status": "added"}}), nil)
+ if bin.Own != 1 || !bin.Reverts() {
+ t.Fatalf("binary unit: %+v", bin)
+ }
+ // A diff that fills GitHub's compare page is not judged.
+ many := make([]*gh.CommitFile, adoptionIntentMaxFiles)
+ for i := range many {
+ many[i] = &gh.CommitFile{Filename: gh.Ptr("f" + string(rune(i))), Patch: gh.Ptr("+x")}
+ }
+ if got := judgeAdoptionIntent(many, nil); !got.Unjudged || got.Reverts() {
+ t.Fatalf("oversized diff judged: %+v", got)
+ }
+}
+
+// adoptUtah1091InPlace adopts PR #7 in place at head aaa (the utah#1091 diff).
+func adoptUtah1091InPlace(t *testing.T) (*adoptHarness, func(), []string) {
+ t.Helper()
+ p := adoptPolicy()
+ p.AdoptInPlace = true
+ h, done := newAdoptHarness(t, ContributorNudgeOutcome{Mode: "comment"}, p)
+ h.adopt.allowMaintainerEdits(7, true)
+ h.adopt.comparePatches = map[string][]map[string]string{"aaa": utah1091Files(true, "")}
+ h.observe(adoptPR(7, "aaa"), "check-failing:unit")
+ labels := []string{"stage/072-returned-to-contributor", "role/contributor", "author/clanker"}
+ h.clock.advance(30 * time.Minute)
+ h.observe(adoptPR(7, "aaa", labels...), "check-failing:unit")
+ h.observe(adoptPR(7, "aaa", labels...), "check-failing:unit")
+ rec, _ := h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7))
+ if !rec.AdoptedInPlace() || rec.AdoptedHead != "aaa" {
+ done()
+ t.Fatalf("not adopted in place: %+v", rec)
+ }
+ return h, done, []string{"stage/074-adopted-by-hive", "role/contributor", "author/clanker"}
+}
+
+func fixerRevertsComments(api *stageAPI) []string {
+ var out []string
+ for _, c := range api.comments {
+ if strings.Contains(c, "Fixing CI would undo this PR's change") {
+ out = append(out, c)
+ }
+ }
+ return out
+}
+
+// The fixer's push re-adds what the PR removed: refused, parked at 085 with
+// the reason, one note, and the contributor lane will not merge that head.
+func TestAdoptedFixerPushThatRevertsPRIsRefused(t *testing.T) {
+ h, done, adopted := adoptUtah1091InPlace(t)
+ defer done()
+ h.adopt.comparePatches["a75a567"] = utah1091Files(false, "")
+ h.adopt.setCommitAuthor("a75a567", testHiveAppBotLogin)
+ h.api.reset()
+ h.clock.advance(time.Minute)
+ h.observe(adoptPR(7, "a75a567", adopted...), "ci-pending")
+
+ notes := fixerRevertsComments(h.api)
+ if len(notes) != 1 || !strings.Contains(notes[0], "leaves 10 of the 35 lines") || !strings.Contains(notes[0], "will not merge `a75a567`") {
+ t.Fatalf("notes = %v (all comments %v)", notes, h.api.comments)
+ }
+ if added := strings.Join(h.addedLabels(), ","); !strings.Contains(added, "stage/085-needs-decision") {
+ t.Fatalf("added = %v, want the 085 park", h.api.added)
+ }
+ rec, _ := h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7))
+ if !rec.AdoptionEscalated("a75a567") || rec.AdoptEscalatedReason != stagelabels.LaneReasonAdoptionFixerReverts || rec.AdoptedHead != "a75a567" {
+ t.Fatalf("record = %+v", rec)
+ }
+ if got := h.e.adoptionIntentGate("acme", "widget", 7, "a75a567"); got != stagelabels.LaneReasonAdoptionFixerReverts {
+ t.Fatalf("merge gate = %q, want the refusal", got)
+ }
+
+ // Next sweep on the same head: the park holds, the note is not repeated.
+ h.api.reset()
+ h.clock.advance(time.Minute)
+ h.observe(adoptPR(7, "a75a567", "stage/085-needs-decision", "role/contributor", "author/clanker"), "ci-pending")
+ if len(fixerRevertsComments(h.api)) != 0 || strings.Contains(strings.Join(h.addedLabels(), ","), "stage/074") {
+ t.Fatalf("second sweep: comments=%v added=%v", h.api.comments, h.api.added)
+ }
+}
+
+// A fix that keeps the PR's change is the hive's head as before.
+func TestAdoptedFixerPushThatKeepsPRIsAllowed(t *testing.T) {
+ h, done, adopted := adoptUtah1091InPlace(t)
+ defer done()
+ h.adopt.comparePatches["bbb"] = utah1091Files(true, "assert_missing")
+ h.adopt.setCommitAuthor("bbb", testHiveAppBotLogin)
+ h.api.reset()
+ h.clock.advance(time.Minute)
+ h.observe(adoptPR(7, "bbb", adopted...), "check-failing:unit")
+ if h.api.writes() != 0 {
+ t.Fatalf("writes after a good fix: added=%v removed=%v comments=%v", h.api.added, h.api.removed, h.api.comments)
+ }
+ rec, _ := h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7))
+ if rec.AdoptedHead != "bbb" || rec.AdoptionEscalated("bbb") {
+ t.Fatalf("record = %+v", rec)
+ }
+ if got := h.e.adoptionIntentGate("acme", "widget", 7, "bbb"); got != "" {
+ t.Fatalf("merge gate = %q, want none", got)
+ }
+}
+
+// An unreadable diff leaves the hive's head unconfirmed: no merge until the
+// check passes on a later sweep.
+func TestAdoptedFixerPushIntentUnreadableHoldsMerge(t *testing.T) {
+ h, done, adopted := adoptUtah1091InPlace(t)
+ defer done()
+ h.adopt.setCommitAuthor("bbb", testHiveAppBotLogin)
+ h.adopt.failCompare = true
+ h.clock.advance(time.Minute)
+ h.observe(adoptPR(7, "bbb", adopted...), "check-failing:unit")
+ rec, _ := h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7))
+ if rec.AdoptedHead != "aaa" || !rec.HivePushedHead("bbb") {
+ t.Fatalf("record = %+v, want the head unconfirmed with push evidence kept", rec)
+ }
+ if got := h.e.adoptionIntentGate("acme", "widget", 7, "bbb"); got != contributorLaneReasonAdoptionIntentPending {
+ t.Fatalf("merge gate = %q, want pending", got)
+ }
+ h.adopt.failCompare = false
+ h.adopt.comparePatches["bbb"] = utah1091Files(true, "")
+ h.clock.advance(time.Minute)
+ h.observe(adoptPR(7, "bbb", adopted...), "check-failing:unit")
+ if rec, _ := h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7)); rec.AdoptedHead != "bbb" {
+ t.Fatalf("retry did not confirm the head: %+v", rec)
+ }
+ if got := h.e.adoptionIntentGate("acme", "widget", 7, "bbb"); got != "" {
+ t.Fatalf("merge gate after retry = %q", got)
+ }
+}
+
+// The push came through the hive's own push record (a fixer committing under
+// a bare git identity): the same check applies, and a note already posted on
+// the head is not posted again.
+func TestAdoptedRecordedHivePushThatRevertsPRIsRefusedOnce(t *testing.T) {
+ h, done, adopted := adoptUtah1091InPlace(t)
+ defer done()
+ key := stageKey("acme", "widget", 7)
+ h.adopt.comparePatches["a75a567"] = utah1091Files(false, "")
+ h.adopt.setCommitAuthor("a75a567", "git:scanner")
+ h.e.stageStoreOrShared().MarkHivePushedHead(key, "a75a567")
+ h.api.reset()
+ h.clock.advance(time.Minute)
+ h.observe(adoptPR(7, "a75a567", adopted...), "ci-pending")
+ if len(fixerRevertsComments(h.api)) != 1 {
+ t.Fatalf("comments = %v", h.api.comments)
+ }
+ rec, _ := h.e.stageStoreOrShared().Get(key)
+ if rec.AdoptEscalatedReason != stagelabels.LaneReasonAdoptionFixerReverts || rec.AdoptedHead != "a75a567" {
+ t.Fatalf("record = %+v", rec)
+ }
+
+ // The note posted but the head was not taken (a restart in between):
+ // the re-check does not post it again.
+ h.e.stageStoreOrShared().MarkAdoptedHead(key, "aaa")
+ h.api.reset()
+ h.clock.advance(time.Minute)
+ h.observe(adoptPR(7, "a75a567", "stage/085-needs-decision", "role/contributor", "author/clanker"), "ci-pending")
+ if n := len(fixerRevertsComments(h.api)); n != 0 {
+ t.Fatalf("note re-posted: %v", h.api.comments)
+ }
+ if rec, _ := h.e.stageStoreOrShared().Get(key); rec.AdoptedHead != "a75a567" || rec.AdoptEscalatedReason != stagelabels.LaneReasonAdoptionFixerReverts {
+ t.Fatalf("record after re-check = %+v", rec)
+ }
+}
+
+// utah1091Partial is the PR's diff with only the first n of its 25 triage
+// removals left (plus the 10 docs lines).
+func utah1091Partial(n int) []map[string]string {
+ full := utah1091Files(true, "")
+ triage := full[1]
+ lines := strings.Split(triage["patch"], "\n")
+ files := []map[string]string{full[0]}
+ if n > 0 {
+ files = append(files, map[string]string{"filename": triage["filename"], "status": "modified", "patch": strings.Join(lines[:n+1], "\n")})
+ }
+ return files
+}
+
+// A further hive push on top of a refused head is judged against the
+// contributor's head, not the refused one: it stays refused and the lane
+// still will not merge it.
+func TestAdoptedFixerPushOnTopOfRefusedHeadStaysRefused(t *testing.T) {
+ h, done, adopted := adoptUtah1091InPlace(t)
+ defer done()
+ key := stageKey("acme", "widget", 7)
+ h.adopt.comparePatches["a75a567"] = utah1091Files(false, "")
+ h.adopt.setCommitAuthor("a75a567", testHiveAppBotLogin)
+ h.clock.advance(time.Minute)
+ h.observe(adoptPR(7, "a75a567", adopted...), "ci-pending")
+
+ // A lint fix on top keeps the docs-only diff.
+ h.adopt.comparePatches["ccc"] = utah1091Files(false, "")
+ h.adopt.setCommitAuthor("ccc", testHiveAppBotLogin)
+ h.api.reset()
+ h.clock.advance(time.Minute)
+ h.observe(adoptPR(7, "ccc", "stage/085-needs-decision", "role/contributor", "author/clanker"), "ci-pending")
+ if notes := fixerRevertsComments(h.api); len(notes) != 1 || !strings.Contains(notes[0], "leaves 10 of the 35 lines this PR changed at `aaa`") {
+ t.Fatalf("notes = %v", h.api.comments)
+ }
+ if got := h.e.adoptionIntentGate("acme", "widget", 7, "ccc"); got != stagelabels.LaneReasonAdoptionFixerReverts {
+ t.Fatalf("merge gate on the push over the refused head = %q", got)
+ }
+ if rec, _ := h.e.stageStoreOrShared().Get(key); rec.IntentBaseHead() != "aaa" {
+ t.Fatalf("intent baseline moved: %+v", rec)
+ }
+}
+
+// Pushes that each keep 60% of the previous head cannot erode the PR's
+// change below half: the second one is judged against the contributor's
+// head (12/35 kept) and refused.
+func TestAdoptedFixerPushChainCannotErodeChange(t *testing.T) {
+ h, done, adopted := adoptUtah1091InPlace(t)
+ defer done()
+ h.adopt.comparePatches["b1"] = utah1091Partial(11) // 21 of 35 kept (60%)
+ h.adopt.setCommitAuthor("b1", testHiveAppBotLogin)
+ h.api.reset()
+ h.clock.advance(time.Minute)
+ h.observe(adoptPR(7, "b1", adopted...), "check-failing:unit")
+ if len(fixerRevertsComments(h.api)) != 0 || h.e.adoptionIntentGate("acme", "widget", 7, "b1") != "" {
+ t.Fatalf("60%% kept must pass: comments=%v", h.api.comments)
+ }
+
+ h.adopt.comparePatches["b2"] = utah1091Partial(2) // 12 of b1's 21 (57%), 12 of 35 overall
+ h.adopt.setCommitAuthor("b2", testHiveAppBotLogin)
+ h.api.reset()
+ h.clock.advance(time.Minute)
+ h.observe(adoptPR(7, "b2", adopted...), "check-failing:unit")
+ if notes := fixerRevertsComments(h.api); len(notes) != 1 || !strings.Contains(notes[0], "leaves 12 of the 35 lines") {
+ t.Fatalf("eroding chain not refused: %v", h.api.comments)
+ }
+ if got := h.e.adoptionIntentGate("acme", "widget", 7, "b2"); got != stagelabels.LaneReasonAdoptionFixerReverts {
+ t.Fatalf("merge gate = %q", got)
+ }
+}
+
+// Stale-red recovery re-runs CI on the refused head; it must not release the
+// refusal (a rerun cannot restore the PR's change).
+func TestAdoptedFixerRevertsSurvivesStaleRedRecovery(t *testing.T) {
+ h, done, adopted := adoptUtah1091InPlace(t)
+ defer done()
+ key := stageKey("acme", "widget", 7)
+ h.adopt.comparePatches["a75a567"] = utah1091Files(false, "")
+ h.adopt.setCommitAuthor("a75a567", testHiveAppBotLogin)
+ h.clock.advance(time.Minute)
+ h.observe(adoptPR(7, "a75a567", adopted...), recoveryReason)
+ if rec, _ := h.e.stageStoreOrShared().Get(key); rec.AdoptEscalatedReason != stagelabels.LaneReasonAdoptionFixerReverts {
+ t.Fatalf("not refused: %+v", rec)
+ }
+
+ // The head sits red past the dwell; base is green, so recovery re-runs.
+ h.adopt.compareBase = "base1"
+ h.adopt.baseChecks = map[string]string{"coverage": "success"}
+ h.adopt.failedRuns = []int64{41}
+ h.api.reset()
+ h.clock.advance(adoptedDwell + time.Minute)
+ h.observe(adoptPR(7, "a75a567", "stage/085-needs-decision", "role/contributor", "author/clanker"), recoveryReason)
+ if len(h.adopt.reruns) != 1 {
+ t.Fatalf("recovery did not run: reruns=%v", h.adopt.reruns)
+ }
+ rec, _ := h.e.stageStoreOrShared().Get(key)
+ if !rec.AdoptionEscalated("a75a567") || rec.AdoptEscalatedReason != stagelabels.LaneReasonAdoptionFixerReverts {
+ t.Fatalf("recovery released the refusal: %+v", rec)
+ }
+ if got := h.e.adoptionIntentGate("acme", "widget", 7, "a75a567"); got != stagelabels.LaneReasonAdoptionFixerReverts {
+ t.Fatalf("merge gate after recovery = %q", got)
+ }
+}
diff --git a/src/pkg/github/automerge/automerge_contributor_adopt.go b/src/pkg/github/automerge/automerge_contributor_adopt.go
index da9b2251bb..050309021b 100644
--- a/src/pkg/github/automerge/automerge_contributor_adopt.go
+++ b/src/pkg/github/automerge/automerge_contributor_adopt.go
@@ -1978,9 +1978,8 @@ func (c *Engine) settleInPlaceAdoption(ctx context.Context, store *stagelabels.S
return 0, nil
}
if prev.HivePushedHead(obs.HeadSHA) {
- store.MarkAdoptedHead(key, obs.HeadSHA)
c.info("stage labels: adopted PR advanced by recorded hive push", "repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA)
- return 0, nil
+ return c.checkAdoptedPushIntent(ctx, store, key, obs, prev)
}
app := c.transport.AppBotLogin()
commit, _, err := c.gh.Repositories.GetCommit(ctx, obs.Owner, obs.Repo, obs.HeadSHA, nil)
@@ -1992,9 +1991,8 @@ func (c *Engine) settleInPlaceAdoption(ctx context.Context, store *stagelabels.S
}
if commitByApp(commit, app) {
store.MarkHivePushedHead(key, obs.HeadSHA)
- store.MarkAdoptedHead(key, obs.HeadSHA)
c.info("stage labels: adopted PR advanced by the hive", "repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA, "pusher", commitIdentity(commit))
- return 0, nil
+ return c.checkAdoptedPushIntent(ctx, store, key, obs, prev)
}
if !policy.AdoptionStrictAttribution {
store.MarkAdoptedHead(key, obs.HeadSHA)
@@ -2318,7 +2316,10 @@ func (c *Engine) escapeStalledAdoption(ctx context.Context, store *stagelabels.S
// an attempt holds the escalation while CI runs on the result, and
// releases one already in place for the same head.
if recovery := c.recoverStaleRedAdoption(ctx, store, key, obs, rec); recovery.Attempted {
- if rec.AdoptionEscalated(obs.HeadSHA) {
+ // A fixer-reverts refusal is a verdict on the head's content, not
+ // its CI: a rerun or rebase cannot restore the PR's change, so the
+ // recovery never releases it.
+ if rec.AdoptionEscalated(obs.HeadSHA) && rec.AdoptEscalatedReason != stagelabels.LaneReasonAdoptionFixerReverts {
store.ClearAdoptionEscalation(key)
c.info("stage labels: adoption escalation released for recovery; back to adopted-by-hive", "repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA, "was", rec.AdoptEscalatedReason, "recovery", recovery.Reason)
}
@@ -2338,7 +2339,7 @@ func (c *Engine) escapeStalledAdoption(ctx context.Context, store *stagelabels.S
n, err := c.postAdoptionEscalationNote(ctx, store, key, obs, adoptionWorkflowRefusedReason, AdoptionWorkflowRefusedComment(obs), "stage labels: workflow-path refusal note failed")
return esc, n, err
}
- if rec.AdoptEscalatedReason == stagelabels.LaneReasonAdoptionChildClosedTwice || rec.AdoptEscalatedReason == stagelabels.LaneReasonAdoptionCopyFailed {
+ if rec.AdoptEscalatedReason == stagelabels.LaneReasonAdoptionChildClosedTwice || rec.AdoptEscalatedReason == stagelabels.LaneReasonAdoptionCopyFailed || rec.AdoptEscalatedReason == stagelabels.LaneReasonAdoptionFixerReverts {
return adoptionEscape{Escalated: true, Reason: rec.AdoptEscalatedReason}, 0, nil
}
stalled := ""
diff --git a/src/pkg/github/automerge/automerge_contributor_adopt_test.go b/src/pkg/github/automerge/automerge_contributor_adopt_test.go
index 46afc6500f..309d6a87a5 100644
--- a/src/pkg/github/automerge/automerge_contributor_adopt_test.go
+++ b/src/pkg/github/automerge/automerge_contributor_adopt_test.go
@@ -36,7 +36,11 @@ type adoptAPI struct {
// compareFiles are the base-side files served by GET compare/head...base;
// failCompare makes it answer 500.
compareFiles []string
- failCompare bool
+ // comparePatches, when set for a head, are the files (filename, status,
+ // patch) GET compare/