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-already-landed-autoclose.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- The hive closes its own already-landed PRs as superseded instead of asking a person: a hive-authored PR (an adoption included) whose reviewer verdict says the change is already on the base — by the `hive-redundant-of` marker or by naming the landing PR in words — is closed with the superseded comment, its adopted originals are settled and closed too, and the parent issue gets the usual closing; the empty-diff freshness guard no longer reads the hive's own label bumps as a fresh head. Logged as `self-authored:already-landed` ([#15647](https://github.com/hivecommons/hive/pull/15647)).
27 changes: 27 additions & 0 deletions src/docs/hive-merge.md
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,33 @@ Three things made #15499 sit a day at 085 instead:
decision now quotes the newest decision-requested verdict on any head,
shows a *redundant of #N* chip, and leads with **Close as superseded**.

**Already landed, by the reviewer's word.** #15647 and #15648 (adoptions of
#15324 / #15343) still parked at 085 with the question "should decide
whether to close this adoption … as already landed, since v5 already
contains the identical change via #15506": the empty-diff closer ran, but
its 15-minute freshness guard read the PR's `updated_at`, which the hive's
own labels and comments bump every pass, so the head was "too fresh"
forever. "Already landed / redundant / no-op" is the hive's own decision
about its own PR, not a person's. The 085 route now takes the
**already-landed** entry when the verdict for the head says so — the
`hive-redundant-of` marker, or wording that names the landing PR
explicitly ("already landed … via #N", "identical change merged in #N";
a question that merely mentions a PR is not a claim, and two candidate
numbers are ambiguous and name none). On that entry there is no
freshness guard (a reviewer confirmed this head), a head whose files
are each byte-identical on the base tip counts as empty when GitHub
still reports a diff, and the landing PR is taken from the marker or
the words; with no number the compare alone must prove redundancy. The
close is logged once with reason `self-authored:already-landed`
(tracker caption "already landed via #N — closing as superseded"),
and settles the adoption's lineage: each original the copy declared
`Supersedes #M` whose record names this copy is told, marked superseded
on the stage store (so the orphan recovery does not re-adopt a head the
base already has) and closed, and issues the copy would have closed are
closed with the redundant-of note. Only when the verdict names no PR
*and* the compare is not empty does the PR fall through to 085 as before
([#15647](https://github.com/hivecommons/hive/pull/15647)).

## Usage

```sh
Expand Down
3 changes: 3 additions & 0 deletions src/pkg/dashboard/stage_tracker_caption.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package dashboard

import (
Expand Down Expand Up @@ -324,6 +324,9 @@
if clause, ok := humanizeCoverReason(r); ok {
return clause
}
if clause, ok := stagelabels.AlreadyLandedCaption(r); ok {
return clause
}
switch lower {
case stagelabels.LaneReasonAdoptionChildClosedTwice:
return "the hive's copies of this PR were closed without merging twice within an hour"
Expand Down
149 changes: 141 additions & 8 deletions src/pkg/github/automerge/automerge_stage_human.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package automerge

// Human-stage routing (hivecommons/hive#12528). When a lane's opinion is
Expand All @@ -11,13 +11,15 @@

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

gh "github.com/google/go-github/v72/github"
"github.com/hivecommons/hive/pkg/authorkind"
hgithub "github.com/hivecommons/hive/pkg/github"
"github.com/hivecommons/hive/pkg/oktotest"
"github.com/hivecommons/hive/pkg/review"
"github.com/hivecommons/hive/pkg/stageengine"
"github.com/hivecommons/hive/pkg/stageengine/prstage"
"github.com/hivecommons/hive/pkg/stagelabels"
Expand Down Expand Up @@ -117,8 +119,10 @@
if v.DecisionRequested {
// A decision question that names the PR already carrying this
// change ("close as a no-op?") is the superseded question itself
// (hivecommons/hive#15499); any other decision stays a person's.
return v.RedundantOf > 0
// (hivecommons/hive#15499) — by marker or in words
// (hivecommons/hive#15647); any other decision stays a person's.
_, claimed := alreadyLandedVerdict(v)
return claimed
}
return strings.EqualFold(strings.TrimSpace(v.Verdict), "requires_human")
}
Expand Down Expand Up @@ -162,7 +166,7 @@
return false
}
if !res.Closed {
if c.closeEmptyDiffBeforeHumanRoute(ctx, obs, pr) {
if c.closeEmptyDiffBeforeHumanRoute(ctx, obs, pr, now) {
return true
}
c.debug("stage labels: hive PR kept", "repo", obs.Display, "pr", obs.Number, "verdict", res.Verdict, "reason", res.Reason)
Expand All @@ -174,6 +178,21 @@
return true
}

// alreadyLandedVerdict reads a reviewer verdict for "this change is already
// on the base": landed is the PR it named (the `hive-redundant-of` marker,
// else "already landed via #N" in words), claimed is true when it says so
// at all, number or not (hivecommons/hive#15647).
func alreadyLandedVerdict(v ContributorLaneVerdict) (landed int, claimed bool) {
return review.AlreadyLandedFromVerdict(v.RedundantOf, v.DecisionQuestion, v.HumanSummaries)
}

// alreadyLandedCloserTransport is the optional transport hook that closes
// a hive-authored PR on the reviewer's already-landed verdict
// (hgithub.Client.CloseAlreadyLandedHivePRIfEligible; hivecommons/hive#15647).
type alreadyLandedCloserTransport interface {
CloseAlreadyLandedHivePRIfEligible(ctx context.Context, repo string, pr *gh.PullRequest, redundantOf int) (hgithub.HiveEmptyDiffClose, error)
}

// closeEmptyDiffBeforeHumanRoute runs the hive-authored empty-diff rule
// (hgithub.CloseEmptyDiffHivePRIfEligible) on a hive-authored PR about to
// be routed to a person: a head whose compare against the base lists no
Expand All @@ -182,15 +201,27 @@
// of asking the owner to say so on every head (hivecommons/hive#15499). The
// diff is measured by the closer; the reviewer's text only names the
// superseding PR.
func (c *Engine) closeEmptyDiffBeforeHumanRoute(ctx context.Context, obs stageObservation, pr *gh.PullRequest) bool {
//
// When the verdict for this head says the change is already landed, the
// already-landed entry is taken instead (hivecommons/hive#15647): no
// freshness guard, files identical on the base tip count as no diff, and
// the close settles the adoption's lineage — the original the copy
// superseded is closed as superseded by the same landed PR, so the orphan
// recovery does not re-adopt a head the base already has.
func (c *Engine) closeEmptyDiffBeforeHumanRoute(ctx context.Context, obs stageObservation, pr *gh.PullRequest, now time.Time) bool {
redundantOf, claimed := 0, false
if obs.Detail.VerdictKnown {
redundantOf, claimed = alreadyLandedVerdict(obs.Detail.Verdict)
}
if claimed {
if landedCloser, ok := c.transport.(alreadyLandedCloserTransport); ok {
return c.closeAlreadyLandedBeforeHumanRoute(ctx, obs, pr, landedCloser, redundantOf, now)
}
}
closer, ok := c.transport.(emptyDiffCloserTransport)
if !ok {
return false
}
redundantOf := 0
if obs.Detail.VerdictKnown {
redundantOf = obs.Detail.Verdict.RedundantOf
}
res, err := closer.CloseEmptyDiffHivePRIfEligible(ctx, obs.Owner+"/"+obs.Repo, pr, redundantOf)
if err != nil {
c.warn("stage labels: empty-diff check failed", "repo", obs.Display, "pr", obs.Number, "error", err)
Expand All @@ -208,6 +239,108 @@
return true
}

// closeAlreadyLandedBeforeHumanRoute is the already-landed entry of the
// empty-diff rule (see closeEmptyDiffBeforeHumanRoute). It reports whether
// the PR was closed; a PR the closer kept (a real diff, a guard) falls
// through to the ordinary route.
func (c *Engine) closeAlreadyLandedBeforeHumanRoute(ctx context.Context, obs stageObservation, pr *gh.PullRequest, closer alreadyLandedCloserTransport, redundantOf int, now time.Time) bool {
res, err := closer.CloseAlreadyLandedHivePRIfEligible(ctx, obs.Owner+"/"+obs.Repo, pr, redundantOf)
if err != nil {
c.warn("stage labels: already-landed check failed", "repo", obs.Display, "pr", obs.Number, "redundant_of", redundantOf, "error", err)
return false
}
if !res.Closed {
c.info("stage labels: already-landed verdict but the PR was kept", "repo", obs.Display, "pr", obs.Number, "redundant_of", redundantOf, "empty", res.Empty, "verdict", res.Verdict, "reason", res.Reason)
return false
}
caption, _ := stagelabels.AlreadyLandedCaption(stagelabels.AlreadyLandedReason(res.RedundantOf))
c.info("stage labels: hive PR closed as already landed instead of a human decision",
"repo", obs.Display, "pr", obs.Number, "reason", stagelabels.LaneReasonAlreadyLanded, "action", res.Reason,
"redundant_of", res.RedundantOf, "issues_closed", res.IssuesClosed, "supersedes", res.Supersedes, "caption", caption, "lane_reason", obs.LaneReason)
c.settleAlreadyLandedLineage(ctx, obs, pr.GetNumber(), res.RedundantOf, res.Supersedes, now)
return true
}

// settleAlreadyLandedLineage closes the originals an already-landed copy
// declared it supersedes (hivecommons/hive#15647): the adoption promised
// "this PR closes when that copy merges"; the copy did not merge because
// the base already had the change, which is the same outcome for the
// original. Each original whose record names this copy as its adoption is
// told, marked superseded on the store (so settleSuperseder does not read
// the closed copy as an orphan and re-adopt), and closed. A refused close
// (403/404 on a fork PR) leaves the note and the mark, as the merged-copy
// path does. The original's record is required: without it the hive does
// not know which head it adopted, and a PR that moved on since is not
// closed on the copy's word.
func (c *Engine) settleAlreadyLandedLineage(ctx context.Context, obs stageObservation, copy, landed int, originals []int, now time.Time) {
if c.gh == nil || len(originals) == 0 {
return
}
store := c.stageStoreOrShared()
for _, number := range originals {
if number <= 0 || number == copy {
continue
}
key := stageKey(obs.Owner, obs.Repo, number)
rec, ok := store.Get(key)
if !ok || rec.AdoptedBy != copy {
c.info("stage labels: already-landed copy names an original the store does not tie to it; left alone", "repo", obs.Display, "pr", number, "copy", copy, "recorded_adopted_by", rec.AdoptedBy)
continue
}
if rec.Superseded() {
continue
}
orig, _, err := c.gh.PullRequests.Get(ctx, obs.Owner, obs.Repo, number)
if err != nil {
c.warn("stage labels: already-landed lineage could not read the original", "repo", obs.Display, "pr", number, "copy", copy, "error", err)
continue
}
if orig.GetMerged() || strings.EqualFold(orig.GetState(), "closed") {
store.MarkSuperseded(key, copy, false, now)
continue
}
if head := strings.TrimSpace(orig.GetHead().GetSHA()); head != "" && rec.AdoptedHead != "" && !strings.EqualFold(head, rec.AdoptedHead) {
c.warn("stage labels: already-landed lineage: the original moved on since the adoption; left open", "repo", obs.Display, "pr", number, "copy", copy, "adopted_head", rec.AdoptedHead, "head", head)
continue
}
body := alreadyLandedOriginalNote(copy, landed, hgithub.SafeGetLogin(orig.GetUser()), nonEmpty(orig.GetBase().GetRef(), obs.BaseRef))
item := writeback.Item{Number: number, IsPR: true, Author: hgithub.SafeGetLogin(orig.GetUser()), Labels: labelNames(orig.Labels), CreatedAt: orig.GetCreatedAt().Time, SupersededBy: copy}
if d := c.writebackAllowed(obs.Display, item, writeback.KindSupersessionClose, body); !d.Allowed {
c.info("stage labels: already-landed lineage close gated", "repo", obs.Display, "pr", number, "copy", copy, "reason", d.Reason)
continue
}
if _, _, err := c.gh.Issues.CreateComment(ctx, obs.Owner, obs.Repo, number, &gh.IssueComment{Body: gh.Ptr(body)}); err != nil { // writeback:gated
c.warn("stage labels: already-landed lineage note failed", "repo", obs.Display, "pr", number, "copy", copy, "error", err)
continue
}
// The note is out: whatever the close below does, never post it
// again, and never read the closed copy as an orphan.
store.MarkSuperseded(key, copy, true, now)
if _, _, err := c.gh.PullRequests.Edit(ctx, obs.Owner, obs.Repo, number, &gh.PullRequest{State: gh.Ptr("closed")}); err != nil { // writeback:gated
c.warn("stage labels: already-landed lineage: closing the original failed; left open with the note", "repo", obs.Display, "pr", number, "copy", copy, "error", err)
continue
}
store.MarkSuperseded(key, copy, false, now)
c.info("stage labels: adopted original closed; its copy was already landed", "repo", obs.Display, "pr", number, "copy", copy, "landed", landed, "reason", stagelabels.LaneReasonAlreadyLanded)
}
}

// alreadyLandedOriginalNote is the close note on an adopted original whose
// hive copy was closed as already landed.
func alreadyLandedOriginalNote(copy, landed int, author, base string) string {
var b strings.Builder
b.WriteString(adoptionCloseMarker)
if landed > 0 {
b.WriteString("\n" + writeback.RedundantOfMarker + " #" + strconv.Itoa(landed) + " -->")
}
b.WriteString("\nClosing: #" + strconv.Itoa(copy) + " (the hive's adoption of this PR, with @" + author + "'s commits) was closed because the change is already on `" + base + "`")
if landed > 0 {
b.WriteString(" via #" + strconv.Itoa(landed))
}
b.WriteString(", so there is nothing left to merge. Thank you — reopen if that's wrong.")
return b.String()
}

// humanEvidence gathers the Evidence for one human-routed observation.
func (c *Engine) humanEvidence(ctx context.Context, tracker *StageTracker, key string, obs stageObservation, cause stagelabels.HumanCause, now time.Time) stagelabels.Evidence {
contributorVerdict := normalizeContributorLaneVerdict(c.contributorLaneVerdictFor(obs.Owner+"/"+obs.Repo, obs.Number, obs.HeadSHA))
Expand Down
Loading
Loading