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-adopted-fixer-reverts-pr.md
Original file line number Diff line number Diff line change
@@ -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.
21 changes: 20 additions & 1 deletion src/docs/labels-and-control-signals/stage-labels.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions src/pkg/dashboard/stage_tracker_adoption_fixer_reverts_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 2 additions & 0 deletions src/pkg/dashboard/stage_tracker_caption.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
2 changes: 2 additions & 0 deletions src/pkg/dashboard/stage_tracker_decision_pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
182 changes: 182 additions & 0 deletions src/pkg/github/automerge/automerge_adoption_intent.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading
Loading