From 27dde8731ccb0fe0a1e193177b5f8468fc302528 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Fri, 30 Oct 2026 00:02:23 -0400 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=90=9B=20review:=20the=20hive=20close?= =?UTF-8?q?s=20its=20own=20already-landed=20PRs=20instead=20of=20asking=20?= =?UTF-8?q?a=20person?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A hive-authored PR (an adoption included) whose reviewer verdict says the change is already on the base — the hive-redundant-of marker, or wording that names the landing PR explicitly — is closed as superseded on the 085 route instead of parked for a person. The empty-diff closer's freshness guard read updated_at, which the hive's own labels bump every pass, so the head was always "too fresh"; the already-landed entry waives it (a reviewer confirmed this head), measures head age from the compare's commits, counts files byte-identical on the base tip as empty, settles the adopted originals' lineage and closes the parent issue. Logged once as self-authored:already-landed. Fixes #15670 Signed-off-by: Andrew Anderson --- changelog.d/fixed-already-landed-autoclose.md | 1 + src/docs/hive-merge.md | 27 ++ src/pkg/dashboard/stage_tracker_caption.go | 3 + .../github/automerge/automerge_stage_human.go | 149 +++++++++- .../automerge/automerge_stage_labels_test.go | 114 ++++++++ .../github/pr_empty_diff_hive_autoclose.go | 256 +++++++++++++++--- .../pr_empty_diff_hive_autoclose_test.go | 16 +- src/pkg/github/pr_superseded_declared.go | 31 ++- src/pkg/github/review_verdict_relay.go | 6 + src/pkg/review/already_landed.go | 108 ++++++++ src/pkg/review/already_landed_test.go | 52 ++++ src/pkg/stagelabels/human.go | 29 ++ src/pkg/stagelabels/human_test.go | 15 + 13 files changed, 758 insertions(+), 49 deletions(-) create mode 100644 changelog.d/fixed-already-landed-autoclose.md create mode 100644 src/pkg/review/already_landed.go create mode 100644 src/pkg/review/already_landed_test.go diff --git a/changelog.d/fixed-already-landed-autoclose.md b/changelog.d/fixed-already-landed-autoclose.md new file mode 100644 index 0000000000..97dd06cfe2 --- /dev/null +++ b/changelog.d/fixed-already-landed-autoclose.md @@ -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)). diff --git a/src/docs/hive-merge.md b/src/docs/hive-merge.md index e914f28dab..c7c5945274 100644 --- a/src/docs/hive-merge.md +++ b/src/docs/hive-merge.md @@ -888,6 +888,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 diff --git a/src/pkg/dashboard/stage_tracker_caption.go b/src/pkg/dashboard/stage_tracker_caption.go index 5407a9d673..29dbdd204c 100644 --- a/src/pkg/dashboard/stage_tracker_caption.go +++ b/src/pkg/dashboard/stage_tracker_caption.go @@ -324,6 +324,9 @@ func humanizeStageReason(reason string) string { 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" diff --git a/src/pkg/github/automerge/automerge_stage_human.go b/src/pkg/github/automerge/automerge_stage_human.go index 0568c9f483..b99af69213 100644 --- a/src/pkg/github/automerge/automerge_stage_human.go +++ b/src/pkg/github/automerge/automerge_stage_human.go @@ -11,6 +11,7 @@ package automerge import ( "context" + "strconv" "strings" "time" @@ -18,6 +19,7 @@ import ( "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" @@ -117,8 +119,10 @@ func supersededCheckWarranted(verdict stageVerdict, obs stageObservation) bool { 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") } @@ -162,7 +166,7 @@ func (c *Engine) closeSupersededBeforeHumanRoute(ctx context.Context, tracker *S 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) @@ -174,6 +178,21 @@ func (c *Engine) closeSupersededBeforeHumanRoute(ctx context.Context, tracker *S 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 @@ -182,15 +201,27 @@ func (c *Engine) closeSupersededBeforeHumanRoute(ctx context.Context, tracker *S // 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) @@ -208,6 +239,108 @@ func (c *Engine) closeEmptyDiffBeforeHumanRoute(ctx context.Context, obs stageOb 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 { + 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 { + 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)) diff --git a/src/pkg/github/automerge/automerge_stage_labels_test.go b/src/pkg/github/automerge/automerge_stage_labels_test.go index b97f2aec3e..845f28f4e5 100644 --- a/src/pkg/github/automerge/automerge_stage_labels_test.go +++ b/src/pkg/github/automerge/automerge_stage_labels_test.go @@ -2584,4 +2584,118 @@ func TestSupersededCheckWarrantedForRedundantOfDecision(t *testing.T) { if supersededCheckWarranted(human, other) { t.Fatal("an ordinary decision question is not a supersession check") } + // hivecommons/hive#15647: the reviewer said it in words, no marker. + worded := stageObservation{Detail: stageDetail{VerdictKnown: true, Verdict: ContributorLaneVerdict{Configured: true, Verdict: "requires_human", DecisionRequested: true, DecisionQuestion: "@clubanderson should decide whether to close this adoption of #15324 as already landed, since v5 already contains the identical two-line test change via #15506"}}} + if !supersededCheckWarranted(human, worded) { + t.Fatal("an already-landed decision in words must run the closer") + } + lineage := stageObservation{Detail: stageDetail{VerdictKnown: true, Verdict: ContributorLaneVerdict{Configured: true, Verdict: "requires_human", DecisionRequested: true, DecisionQuestion: "@clubanderson should decide whether this adoption of #15324 should keep the contributor's flag rename"}}} + if supersededCheckWarranted(human, lineage) { + t.Fatal("a PR mention that is not about redundancy is a person's decision") + } +} + +// alreadyLandedCloserSpy stands in for the hgithub closers on the stage +// engine's transport: the superseded rule keeps the PR, the already-landed +// rule closes it. +type alreadyLandedCloserSpy struct { + Transport + supersededCalls []int + landedCalls []int + redundantOf []int + result hgithub.HiveEmptyDiffClose +} + +func (s *alreadyLandedCloserSpy) CloseSupersededHivePRIfEligible(_ context.Context, _ string, pr *gh.PullRequest) (hgithub.HiveSupersededClose, error) { + s.supersededCalls = append(s.supersededCalls, pr.GetNumber()) + return hgithub.HiveSupersededClose{Reason: "issue-open"}, nil +} + +func (s *alreadyLandedCloserSpy) CloseAlreadyLandedHivePRIfEligible(_ context.Context, _ string, pr *gh.PullRequest, redundantOf int) (hgithub.HiveEmptyDiffClose, error) { + s.landedCalls = append(s.landedCalls, pr.GetNumber()) + s.redundantOf = append(s.redundantOf, redundantOf) + return s.result, nil +} + +// TestApplyStageLabelsHiveAlreadyLandedClosesInsteadOfDecision is +// hivecommons/hive#15647: a hive-authored adoption whose reviewer verdict +// says the change already landed via another PR is closed as superseded +// (and its adopted original settled) — never parked at 085. +func TestApplyStageLabelsHiveAlreadyLandedClosesInsteadOfDecision(t *testing.T) { + const copy, original, landed = 15647, 15324, 15506 + var closedOriginal bool + var originalComments []string + api := &stageAPI{} + api.extra = func(w http.ResponseWriter, r *http.Request) bool { + switch { + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/pulls/15647": + _ = json.NewEncoder(w).Encode(map[string]any{"number": copy, "state": "open", "user": map[string]any{"login": testHiveAppBotLogin}, + "body": "Supersedes #15324 (adopted after CI went red; original commits by @onkar717 preserved).", + "head": map[string]any{"sha": "h1", "ref": "hive/adopt-15324"}, "base": map[string]any{"ref": "v5"}}) + return true + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/pulls/15324": + _ = json.NewEncoder(w).Encode(map[string]any{"number": original, "state": "open", "user": map[string]any{"login": "onkar717"}, + "head": map[string]any{"sha": "o1", "ref": "fix"}, "base": map[string]any{"ref": "v5"}}) + return true + case r.Method == http.MethodPatch && r.URL.Path == "/repos/acme/widget/pulls/15324": + closedOriginal = true + _ = json.NewEncoder(w).Encode(map[string]any{"number": original, "state": "closed"}) + return true + case r.Method == http.MethodPost && r.URL.Path == "/repos/acme/widget/issues/15324/comments": + var c struct{ Body string } + _ = json.NewDecoder(r.Body).Decode(&c) + originalComments = append(originalComments, c.Body) + _ = json.NewEncoder(w).Encode(map[string]any{"id": 1}) + return true + } + return false + } + srv := newStageAPI(t, api) + defer srv.Close() + clock := &contributorLaneClock{at: time.Date(2026, 10, 20, 3, 41, 0, 0, time.UTC)} + e, _, store := newStageEngine(t, srv.URL, clock) + e.trustedAuthorPolicy = func() TrustedAuthorPolicy { return TrustedAuthorPolicy{Enabled: true} } + e.contributorLanePolicy = func() ContributorLanePolicy { return ContributorLanePolicy{} } + spy := &alreadyLandedCloserSpy{Transport: e.transport, result: hgithub.HiveEmptyDiffClose{Closed: true, Empty: true, RedundantOf: landed, Reason: hgithub.SupersessionActionClosedAlreadyLanded, Supersedes: []int{original}}} + e.transport = spy + ctx := context.Background() + + // The original is on record as adopted (supersede mode) by the copy. + origKey := stageKey("acme", "widget", original) + store.Observe(origKey, stagelabels.AdoptedByHive, "adopted") + store.MarkAdopted(origKey, "o1", copy, clock.at) + + pr := stagePR(copy) + pr.User = &gh.User{Login: gh.Ptr(testHiveAppBotLogin)} + e.recordStage(stageLaneSelf, "acme/widget", "acme", "widget", pr, contributorLaneReasonNoVerdict, false, nil) + e.noteStageRole("acme", "widget", copy, stagelabels.RoleTrusted) + e.noteStageVerdict("acme", "widget", copy, ContributorLaneVerdict{Configured: true, Verdict: "requires_human", DecisionRequested: true, + DecisionQuestion: "@clubanderson should decide whether to close this adoption of #15324 as already landed, since v5 already contains the identical two-line test change via #15506"}) + res, err := e.ApplyStageLabels(ctx) + if err != nil { + t.Fatal(err) + } + if res.ClosedSuperseded != 1 { + t.Fatalf("ClosedSuperseded=%d want 1", res.ClosedSuperseded) + } + for _, batch := range api.added { + for _, l := range batch { + if l == "stage/085-needs-decision" { + t.Fatalf("already-landed hive PR routed to 085: added=%v", api.added) + } + } + } + if !reflect.DeepEqual(spy.landedCalls, []int{copy}) || !reflect.DeepEqual(spy.redundantOf, []int{landed}) { + t.Fatalf("already-landed closer calls=%v redundant_of=%v", spy.landedCalls, spy.redundantOf) + } + if !closedOriginal || len(originalComments) != 1 || !strings.Contains(originalComments[0], "via #15506") { + t.Fatalf("original lineage: closed=%v comments=%q", closedOriginal, originalComments) + } + rec, _ := store.Get(origKey) + if rec.SupersededBy != copy || rec.SupersededOpen { + t.Fatalf("original record not marked superseded: %+v", rec) + } + if _, ok := store.Get(stageKey("acme", "widget", copy)); ok { + t.Fatal("closed copy still on the store") + } } diff --git a/src/pkg/github/pr_empty_diff_hive_autoclose.go b/src/pkg/github/pr_empty_diff_hive_autoclose.go index baeee60072..aa5d10ab23 100644 --- a/src/pkg/github/pr_empty_diff_hive_autoclose.go +++ b/src/pkg/github/pr_empty_diff_hive_autoclose.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "log/slog" + "net/http" "strings" "time" @@ -30,6 +31,22 @@ import ( // compare API base...head is the proof. A head younger than // EmptyDiffMinHeadAge is left alone: a branch the author is still // pushing to may be empty for a moment. +// +// Already-landed (hivecommons/hive#15647, #15648). The same rule, entered +// from the reviewer's verdict rather than from changed_files: when the +// review of THIS head says the change is already on the base (the +// `hive-redundant-of` marker, or "already landed via #N" in words), the +// hive closes its own PR as superseded by #N instead of parking it at 085 +// for a person to say so. Two things differ from the plain empty-diff +// entry. The freshness guard is waived — the reviewer has already read the +// head, so it is not mid-push (on #15647 the guard measured from +// updated_at, which the hive's own labels and comments bump every pass, +// and the PR sat at 085). And an adoption whose contributor head was never +// merged from the base still lists files in the compare even though every +// one of them is byte-identical on the base tip; the closer proves that +// file by file (blob SHA at head == blob SHA on base) before closing. +// Redundancy is always measured: the reviewer's text only names the +// superseding PR, and when it names none the close says "the base itself". const ( // SupersessionActionClosedEmptyDiff: a hive-authored PR was closed @@ -37,11 +54,26 @@ const ( // files); the change it carried is already on the base. SupersessionActionClosedEmptyDiff = "closed-empty-diff" + // SupersessionActionClosedAlreadyLanded: a hive-authored PR was closed + // because every file its compare lists is already identical on the + // base tip — the change landed through another PR and the head was + // simply never merged from the base. + SupersessionActionClosedAlreadyLanded = "closed-already-landed" + // EmptyDiffMinHeadAge is how old a PR must be before an empty diff // closes it: a just-opened PR whose commits are still arriving is not // yet empty by intent. EmptyDiffMinHeadAge = 15 * time.Minute + // alreadyLandedMaxFiles bounds the file-by-file base comparison: one + // contents GET per file, so a wide PR is not proved this way (it + // stays with the reviewer's question). + alreadyLandedMaxFiles = 25 + + // alreadyLandedReason is the stage-label log reason of a close taken + // on the reviewer's already-landed verdict. + alreadyLandedReason = "self-authored:already-landed" + hiveEmptyDiffReasonHasDiff = "has-diff" hiveEmptyDiffReasonUnknown = "diff-unknown" hiveEmptyDiffReasonTooFresh = "head-too-fresh" @@ -76,9 +108,17 @@ type HiveEmptyDiffAutoCloseFacts struct { // complete file list; DiffFiles is its length. DiffKnown bool DiffFiles int - // HeadAge is now minus the PR's last update (or creation when GitHub - // reports no update). + // HeadAge is now minus the head commit's date when the compare + // reported one, else minus the PR's last update (or creation). HeadAge time.Duration + // ReviewerConfirmed: the reviewer verdict for this very head says the + // change is already on the base. The head has been read, so the + // freshness guard (a branch mid-push) does not apply. + ReviewerConfirmed bool + // TreeMatchesBase is true when the compare lists files but every one + // of them is byte-identical on the base tip (prFilesIdenticalOnBase): + // the diff GitHub shows is history, not content. + TreeMatchesBase bool // Level is the repo's ACMM level; own-PR closes need L6 like the // superseded rule. Level int @@ -98,7 +138,7 @@ func DecideHiveEmptyDiffAutoClose(f HiveEmptyDiffAutoCloseFacts) (verdict, reaso return hiveEmptyDiffVerdictKeep, hiveEmptyDiffReasonDraft case !f.DiffKnown: return hiveEmptyDiffVerdictKeep, hiveEmptyDiffReasonUnknown - case f.DiffFiles > 0: + case f.DiffFiles > 0 && !f.TreeMatchesBase: return hiveEmptyDiffVerdictKeep, hiveEmptyDiffReasonHasDiff case !f.HiveAuthored: return hiveEmptyDiffVerdictHumanNote, hiveEmptyDiffReasonNotHive @@ -110,9 +150,12 @@ func DecideHiveEmptyDiffAutoClose(f HiveEmptyDiffAutoCloseFacts) (verdict, reaso return hiveEmptyDiffVerdictKeep, hiveEmptyDiffReasonPerson case f.Level < acmmLevelAutonomous: return hiveEmptyDiffVerdictKeep, hiveEmptyDiffReasonLevel - case f.HeadAge < EmptyDiffMinHeadAge: + case f.HeadAge < EmptyDiffMinHeadAge && !f.ReviewerConfirmed: return hiveEmptyDiffVerdictKeep, hiveEmptyDiffReasonTooFresh } + if f.DiffFiles > 0 { + return hiveEmptyDiffVerdictClose, SupersessionActionClosedAlreadyLanded + } return hiveEmptyDiffVerdictClose, SupersessionActionClosedEmptyDiff } @@ -128,6 +171,12 @@ type HiveEmptyDiffClose struct { // RedundantOf is the PR the close comment names as carrying the // change, when the caller knew one. RedundantOf int + // Supersedes are the PRs the closed PR's body declared it supersedes + // (an adoption's original); the caller settles their lineage. + Supersedes []int + // IssuesClosed are the issues the PR's closing references named that + // the close also closed (through CloseIssue's usual gates). + IssuesClosed []int } // PRChangedFilesClaimsEmpty reports whether a single-PR GET payload says the @@ -139,21 +188,74 @@ func PRChangedFilesClaimsEmpty(pr *gh.PullRequest) bool { // prDiffAgainstBase measures the PR's diff as GitHub shows it: the compare // base...head. known is false when the compare failed or the file list -// was cut short. -func (c *Client) prDiffAgainstBase(ctx context.Context, owner, name string, pr *gh.PullRequest) (files int, known bool) { +// was cut short. headAt is the newest commit's committer date when the +// compare listed commits (zero otherwise). +func (c *Client) prDiffAgainstBase(ctx context.Context, owner, name string, pr *gh.PullRequest) (files []*gh.CommitFile, headAt time.Time, known bool) { base := strings.TrimSpace(pr.GetBase().GetRef()) head := strings.TrimSpace(pr.GetHead().GetSHA()) if base == "" || head == "" { - return 0, false + return nil, time.Time{}, false } comparison, resp, err := c.client.Repositories.CompareCommits(WithRESTCaller(ctx, "hive:empty_diff_autoclose"), owner, name, base, head, &gh.ListOptions{PerPage: githubCompareFileLimit}) if err != nil || comparison == nil { - return 0, false + return nil, time.Time{}, false + } + for _, commit := range comparison.Commits { + if at := commit.GetCommit().GetCommitter().GetDate().Time; at.After(headAt) { + headAt = at + } } if (resp != nil && resp.NextPage != 0) || len(comparison.Files) >= githubCompareFileLimit { - return len(comparison.Files), false + return comparison.Files, headAt, false } - return len(comparison.Files), true + return comparison.Files, headAt, true +} + +// prFilesIdenticalOnBase proves an already-landed head: every file the +// compare lists is, on the base tip, exactly what the head has (added or +// modified → same blob SHA; removed → absent). known is false when a +// contents read failed for a reason other than 404, or the PR is wider +// than alreadyLandedMaxFiles; a rename or copy is not proved (match +// false, known true). +func (c *Client) prFilesIdenticalOnBase(ctx context.Context, owner, name, base string, files []*gh.CommitFile) (match, known bool) { + if len(files) == 0 { + return true, true + } + if len(files) > alreadyLandedMaxFiles || strings.TrimSpace(base) == "" { + return false, false + } + ctx = WithRESTCaller(ctx, "hive:already_landed_autoclose") + for _, f := range files { + path := strings.TrimSpace(f.GetFilename()) + if path == "" { + return false, false + } + content, _, resp, err := c.client.Repositories.GetContents(ctx, owner, name, path, &gh.RepositoryContentGetOptions{Ref: base}) + notFound := err != nil && resp != nil && resp.StatusCode == http.StatusNotFound + switch strings.ToLower(strings.TrimSpace(f.GetStatus())) { + case "added", "modified", "changed": + if notFound { + return false, true + } + if err != nil || content == nil { + return false, false + } + if !strings.EqualFold(strings.TrimSpace(content.GetSHA()), strings.TrimSpace(f.GetSHA())) { + return false, true + } + case "removed": + if notFound { + continue + } + if err != nil { + return false, false + } + return false, true + default: + return false, true + } + } + return true, true } // CloseEmptyDiffHivePRIfEligible closes pr when the hive opened it and the @@ -164,6 +266,22 @@ func (c *Client) prDiffAgainstBase(ctx context.Context, owner, name string, pr * // close. Idempotent: a closed PR, a PR with a diff, or a person's PR is a // no-op. repo is "owner/name". func (c *Client) CloseEmptyDiffHivePRIfEligible(ctx context.Context, repo string, pr *gh.PullRequest, redundantOf int) (HiveEmptyDiffClose, error) { + return c.closeHiveRedundantPR(ctx, repo, pr, redundantOf, false) +} + +// CloseAlreadyLandedHivePRIfEligible is CloseEmptyDiffHivePRIfEligible +// entered from the reviewer's verdict on this head (the change is already +// on the base; redundantOf is the PR it named, 0 when it named none). The +// freshness guard is waived and a head whose compare lists files is still +// closed when every one of them is identical on the base tip. The PR's +// closing references are closed through CloseIssue's usual gates, and the +// originals its body declares it supersedes are reported for the caller's +// lineage settlement. Redundancy is measured, never taken from the text. +func (c *Client) CloseAlreadyLandedHivePRIfEligible(ctx context.Context, repo string, pr *gh.PullRequest, redundantOf int) (HiveEmptyDiffClose, error) { + return c.closeHiveRedundantPR(ctx, repo, pr, redundantOf, true) +} + +func (c *Client) closeHiveRedundantPR(ctx context.Context, repo string, pr *gh.PullRequest, redundantOf int, reviewed bool) (HiveEmptyDiffClose, error) { out := HiveEmptyDiffClose{RedundantOf: redundantOf} if c == nil || c.client == nil { return out, ErrNoGitHubClient @@ -179,29 +297,47 @@ func (c *Client) CloseEmptyDiffHivePRIfEligible(ctx context.Context, repo string identity.AppLogin = c.appBotLogin } labels := labelNames(pr.Labels) + now := time.Now() facts := HiveEmptyDiffAutoCloseFacts{ - Enabled: enabled, - Level: level, - Merged: pr.GetMerged() || !pr.GetMergedAt().IsZero() || strings.EqualFold(pr.GetState(), "closed"), - Draft: pr.GetDraft(), - HiveAuthored: identity.Matches(safeGetLogin(pr.GetUser())), - Held: c.isHeld(labels) || c.isExempt(labels), - KeptOpen: supersessionHasLabel(pr, SupersessionKeepOpenLabel), - HeadAge: emptyDiffHeadAge(pr, time.Now()), - } - if pr.ChangedFiles != nil && *pr.ChangedFiles > 0 { - // The GET payload already shows a diff: no compare needed. + Enabled: enabled, + Level: level, + Merged: pr.GetMerged() || !pr.GetMergedAt().IsZero() || strings.EqualFold(pr.GetState(), "closed"), + Draft: pr.GetDraft(), + HiveAuthored: identity.Matches(safeGetLogin(pr.GetUser())), + Held: c.isHeld(labels) || c.isExempt(labels), + KeptOpen: supersessionHasLabel(pr, SupersessionKeepOpenLabel), + HeadAge: emptyDiffHeadAge(pr, now), + ReviewerConfirmed: reviewed, + } + if pr.ChangedFiles != nil && *pr.ChangedFiles > 0 && !reviewed { + // The GET payload already shows a diff: no compare needed. On the + // reviewer's word the compare still runs — the files may all be + // identical on the base. facts.DiffKnown, facts.DiffFiles = true, *pr.ChangedFiles } - if !enabled || facts.Merged || facts.Draft || facts.Held || facts.KeptOpen || facts.DiffFiles > 0 { + if !enabled || facts.Merged || facts.Draft || facts.Held || facts.KeptOpen || facts.DiffFiles > 0 || (!reviewed && !facts.HiveAuthored && facts.DiffKnown) { // Cheap guards first: no compare for a PR the rule cannot touch. out.Verdict, out.Reason = DecideHiveEmptyDiffAutoClose(facts) return out, nil } + if reviewed && !facts.HiveAuthored { + // A person's PR is never closed on the hive's reading of a + // verdict; no compare is spent on it. + out.Verdict, out.Reason = hiveEmptyDiffVerdictHumanNote, hiveEmptyDiffReasonNotHive + return out, nil + } if !facts.DiffKnown { - facts.DiffFiles, facts.DiffKnown = c.prDiffAgainstBase(ctx, owner, name, pr) + files, headAt, known := c.prDiffAgainstBase(ctx, owner, name, pr) + facts.DiffFiles, facts.DiffKnown = len(files), known + if !headAt.IsZero() { + facts.HeadAge = now.Sub(headAt) + } + if known && len(files) > 0 && reviewed { + match, matchKnown := c.prFilesIdenticalOnBase(ctx, owner, name, strings.TrimSpace(pr.GetBase().GetRef()), files) + facts.TreeMatchesBase = match && matchKnown + } } - out.Empty = facts.DiffKnown && facts.DiffFiles == 0 + out.Empty = facts.DiffKnown && (facts.DiffFiles == 0 || facts.TreeMatchesBase) if out.Empty && facts.HiveAuthored { facts.ParkedByPerson = c.supersessionNeedsHumanByPerson(ctx, owner, name, pr) } @@ -215,11 +351,11 @@ func (c *Client) CloseEmptyDiffHivePRIfEligible(ctx context.Context, repo string } event := SupersessionSweepEvent{ Repo: repo, Number: pr.GetNumber(), Author: safeGetLogin(pr.GetUser()), - IssueRepo: repo, CloserPR: redundantOf, Action: SupersessionActionClosedEmptyDiff, + IssueRepo: repo, CloserPR: redundantOf, Action: out.Reason, } closer := supersessionCloser{Repo: repo, Number: redundantOf} - why := hiveEmptyDiffCloseReason(redundantOf, shortHeadSHA(pr), strings.TrimSpace(pr.GetBase().GetRef())) - if _, err := c.ensureSupersessionComment(ctx, owner, name, pr.GetNumber(), renderEmptyDiffCloseComment(closer, why)); err != nil { + why := hiveEmptyDiffCloseReason(redundantOf, shortHeadSHA(pr), strings.TrimSpace(pr.GetBase().GetRef()), facts.TreeMatchesBase) + if _, err := c.ensureSupersessionComment(ctx, owner, name, pr.GetNumber(), renderEmptyDiffCloseComment(closer, why, facts.TreeMatchesBase)); err != nil { return out, err } if err := c.labelAndCloseSupersededPR(ctx, owner, name, pr.GetNumber()); err != nil { @@ -235,9 +371,57 @@ func (c *Client) CloseEmptyDiffHivePRIfEligible(ctx context.Context, repo string slog.String("repo", repo), slog.Int("pr", pr.GetNumber()), slog.Int("redundant_of", redundantOf), slog.String("action", event.Action)) } out.Closed = true + if reviewed { + out.Supersedes = SupersedesDeclared(pr.GetBody()) + out.IssuesClosed = c.closeIssuesAlreadyLanded(ctx, owner, name, pr, redundantOf) + } return out, nil } +// closeIssuesAlreadyLanded closes the issues pr's closing references name +// once the PR itself is closed as already landed: the work is on the base, +// so the issue gets the usual close (CloseIssue's tracking-issue and +// reporter-confirmation gates included) with a note naming the PR that +// landed it. Failures are logged, never fatal: the PR close stands. +func (c *Client) closeIssuesAlreadyLanded(ctx context.Context, owner, name string, pr *gh.PullRequest, redundantOf int) []int { + repo := owner + "/" + name + var closed []int + for _, number := range ClosingIssueRefs(pr.GetBody(), repo) { + issue, _, err := c.client.Issues.Get(ctx, owner, name, number) + if err != nil || issue == nil || issue.IsPullRequest() || !strings.EqualFold(issue.GetState(), "open") { + continue + } + item := writeback.Item{Number: number, Author: safeGetLogin(issue.GetUser()), Labels: labelNames(issue.Labels), CreatedAt: issue.GetCreatedAt().Time, HiveAuthored: c.getHiveIdentity().Matches(safeGetLogin(issue.GetUser()))} + if d := c.WritebackAllowed(repo, item, writeback.KindIssueClose, "close issue already landed"); !d.Allowed { + continue + } + note := alreadyLandedIssueNote(pr.GetNumber(), redundantOf, strings.TrimSpace(pr.GetBase().GetRef())) + if _, _, err := c.client.Issues.CreateComment(ctx, owner, name, number, &gh.IssueComment{Body: gh.Ptr(note)}); err != nil { + if c.logger != nil { + c.logger.Warn("already-landed auto-close: issue note failed", slog.String("repo", repo), slog.Int("issue", number), slog.String("error", err.Error())) + } + continue + } + if err := c.CloseIssue(ctx, repo, number, IssueCloseOptions{StateReason: IssueStateReasonCompleted}); err != nil { + if c.logger != nil { + c.logger.Warn("already-landed auto-close: issue left open", slog.String("repo", repo), slog.Int("issue", number), slog.String("error", err.Error())) + } + continue + } + closed = append(closed, number) + } + return closed +} + +// alreadyLandedIssueNote is the one-line note on an issue closed because +// the PR that claimed it was already landed through another PR. +func alreadyLandedIssueNote(pr, redundantOf int, base string) string { + if redundantOf > 0 { + return fmt.Sprintf("%s #%d -->\nThe hive closed its own #%d as superseded: the change it carried is already on `%s` via #%d, so this issue is done. Reopen if that's wrong.", reviewRedundantOfMarker, redundantOf, pr, base, redundantOf) + } + return fmt.Sprintf("The hive closed its own #%d as superseded: the change it carried is already on `%s`, so this issue is done. Reopen if that's wrong.", pr, base) +} + func emptyDiffHeadAge(pr *gh.PullRequest, now time.Time) time.Duration { at := pr.GetUpdatedAt().Time if at.IsZero() { @@ -258,24 +442,32 @@ func shortHeadSHA(pr *gh.PullRequest) string { } // hiveEmptyDiffCloseReason is the one-line why of the close comment. -func hiveEmptyDiffCloseReason(redundantOf int, head, base string) string { +func hiveEmptyDiffCloseReason(redundantOf int, head, base string, treeMatches bool) string { + proof := "the compare lists no files" + if treeMatches { + proof = "every file it touches is already byte-identical on the base tip" + } if redundantOf > 0 { - return fmt.Sprintf("Closing this hive-authored PR as superseded by #%d: head %s is identical to `%s` (the compare lists no files), so the change it carried is already on the base. Reopen if that's wrong.", redundantOf, head, base) + return fmt.Sprintf("Closing this hive-authored PR as superseded by #%d: head %s is identical to `%s` (%s), so the change it carried is already on the base. Reopen if that's wrong.", redundantOf, head, base, proof) } - return fmt.Sprintf("Closing this hive-authored PR: head %s is identical to `%s` (the compare lists no files), so there is nothing left to merge. Reopen if that's wrong.", head, base) + return fmt.Sprintf("Closing this hive-authored PR: head %s is identical to `%s` (%s), so there is nothing left to merge. Reopen if that's wrong.", head, base, proof) } // renderEmptyDiffCloseComment is the close notice, under the supersession // marker so ensureSupersessionComment edits it in place and the // `hive-redundant-of` marker so the dashboard and the dedupe key read the // superseding PR. -func renderEmptyDiffCloseComment(closer supersessionCloser, reason string) string { +func renderEmptyDiffCloseComment(closer supersessionCloser, reason string, treeMatches bool) string { var b strings.Builder fmt.Fprintln(&b, supersessionSweepMarker) if closer.Number > 0 { fmt.Fprintf(&b, "%s #%d -->\n", reviewRedundantOfMarker, closer.Number) } - fmt.Fprintln(&b, "The hive found that this PR's diff against its base is empty.") + if treeMatches { + fmt.Fprintln(&b, "The hive found that this PR's change is already on its base.") + } else { + fmt.Fprintln(&b, "The hive found that this PR's diff against its base is empty.") + } fmt.Fprintln(&b) switch { case closer.Number <= 0: diff --git a/src/pkg/github/pr_empty_diff_hive_autoclose_test.go b/src/pkg/github/pr_empty_diff_hive_autoclose_test.go index 1042a87044..de30f2c02a 100644 --- a/src/pkg/github/pr_empty_diff_hive_autoclose_test.go +++ b/src/pkg/github/pr_empty_diff_hive_autoclose_test.go @@ -37,6 +37,20 @@ func TestDecideHiveEmptyDiffAutoClose(t *testing.T) { {"parked by a person keeps", with(func(f *HiveEmptyDiffAutoCloseFacts) { f.ParkedByPerson = true }), hiveEmptyDiffVerdictKeep, hiveEmptyDiffReasonPerson}, {"below L6 keeps", with(func(f *HiveEmptyDiffAutoCloseFacts) { f.Level = acmmLevelAutonomous - 1 }), hiveEmptyDiffVerdictKeep, hiveEmptyDiffReasonLevel}, {"fresh head keeps", with(func(f *HiveEmptyDiffAutoCloseFacts) { f.HeadAge = EmptyDiffMinHeadAge / 2 }), hiveEmptyDiffVerdictKeep, hiveEmptyDiffReasonTooFresh}, + // hivecommons/hive#15647: the reviewer read this head and said it + // is already landed; a fresh head is not mid-push, and files that + // are all identical on the base tip are no diff. + {"reviewer-confirmed fresh head closes", with(func(f *HiveEmptyDiffAutoCloseFacts) { f.HeadAge = 0; f.ReviewerConfirmed = true }), hiveEmptyDiffVerdictClose, SupersessionActionClosedEmptyDiff}, + {"files identical on base close as already landed", with(func(f *HiveEmptyDiffAutoCloseFacts) { + f.DiffFiles = 2 + f.TreeMatchesBase = true + f.ReviewerConfirmed = true + }), hiveEmptyDiffVerdictClose, SupersessionActionClosedAlreadyLanded}, + {"files identical on base but a person's PR is a prompt", with(func(f *HiveEmptyDiffAutoCloseFacts) { + f.DiffFiles = 2 + f.TreeMatchesBase = true + f.HiveAuthored = false + }), hiveEmptyDiffVerdictHumanNote, hiveEmptyDiffReasonNotHive}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { @@ -61,7 +75,7 @@ func TestPRChangedFilesClaimsEmpty(t *testing.T) { } func TestRenderEmptyDiffCloseCommentCarriesMarkers(t *testing.T) { - body := renderEmptyDiffCloseComment(supersessionCloser{Repo: "hivecommons/hive", Number: 15427, URL: "https://github.com/hivecommons/hive/pull/15427"}, "closed: empty diff") + body := renderEmptyDiffCloseComment(supersessionCloser{Repo: "hivecommons/hive", Number: 15427, URL: "https://github.com/hivecommons/hive/pull/15427"}, "closed: empty diff", false) for _, want := range []string{supersessionSweepMarker, reviewRedundantOfMarker + " #15427 -->", "[#15427](https://github.com/hivecommons/hive/pull/15427)", "edited in place"} { if !strings.Contains(body, want) { t.Fatalf("comment lacks %q:\n%s", want, body) diff --git a/src/pkg/github/pr_superseded_declared.go b/src/pkg/github/pr_superseded_declared.go index e4df2afba7..7481b857e7 100644 --- a/src/pkg/github/pr_superseded_declared.go +++ b/src/pkg/github/pr_superseded_declared.go @@ -29,23 +29,38 @@ var supersedesLinePattern = regexp.MustCompile(`(?i)\bsupersedes\b:?([^\n]*)`) var supersedesNumberPattern = regexp.MustCompile(`#(\d+)`) -// bodyDeclaresSupersedes reports whether body states that it supersedes -// PR number. -func bodyDeclaresSupersedes(body string, number int) bool { - if number <= 0 { - return false - } +// SupersedesDeclared returns the PR numbers body states it supersedes +// (`Supersedes #N`), in order of first appearance, de-duplicated. An +// adoption copy's body names its original this way. +func SupersedesDeclared(body string) []int { + seen := map[int]bool{} + var out []int for _, m := range supersedesLinePattern.FindAllStringSubmatch(body, -1) { rest := m[1] if i := strings.IndexAny(rest, "·;("); i >= 0 { rest = rest[:i] } for _, n := range supersedesNumberPattern.FindAllStringSubmatch(rest, -1) { - if v, err := strconv.Atoi(n[1]); err == nil && v == number { - return true + if v, err := strconv.Atoi(n[1]); err == nil && v > 0 && !seen[v] { + seen[v] = true + out = append(out, v) } } } + return out +} + +// bodyDeclaresSupersedes reports whether body states that it supersedes +// PR number. +func bodyDeclaresSupersedes(body string, number int) bool { + if number <= 0 { + return false + } + for _, v := range SupersedesDeclared(body) { + if v == number { + return true + } + } return false } diff --git a/src/pkg/github/review_verdict_relay.go b/src/pkg/github/review_verdict_relay.go index 0062c2b083..9ba43fd260 100644 --- a/src/pkg/github/review_verdict_relay.go +++ b/src/pkg/github/review_verdict_relay.go @@ -79,6 +79,12 @@ func (c *Client) recordReviewVerdict(ctx context.Context, req ReviewRequest, dir decisionRequested, decisionQuestion := reviewDecisionRequestFromBody(req.Body) decisionRiskHigh := reviewDecisionRiskHighFromBody(req.Body) redundantOf := reviewRedundantOfNumber(req.Body) + if redundantOf == 0 && decisionRequested { + // A reviewer that wrote "already landed via #N" but forgot the + // marker still named the PR; carry it so the hive's own + // already-landed close can act (hivecommons/hive#15647). + redundantOf = review.AlreadyLandedPR(0, decisionQuestion) + } if err := os.MkdirAll(dir, 0o755); err != nil { c.logger.Warn("review-request watcher: could not create report dir", slog.String("dir", dir), slog.String("error", err.Error())) diff --git a/src/pkg/review/already_landed.go b/src/pkg/review/already_landed.go new file mode 100644 index 0000000000..d2c6c8f27d --- /dev/null +++ b/src/pkg/review/already_landed.go @@ -0,0 +1,108 @@ +package review + +import ( + "regexp" + "strconv" + "strings" +) + +// Already-landed detection (hivecommons/hive#15647, #15648). +// +// A reviewer that finds a PR's change already on the base branch says so +// in words — "close this adoption of #15324 as already landed, since v5 +// already contains the identical two-line test change via #15506" — and +// usually also sets ``. The marker is +// the contract; the wording is the fallback for a review that forgot it. +// The fallback is conservative on purpose: it needs an already-landed +// phrase AND exactly one merged-PR reference introduced by a landing +// preposition (via / through / in / by / since). A question that merely +// mentions a PR ("should #15506's flag default on?") names nothing, and a +// question whose only reference is the adoption's own lineage ("close this +// adoption of #15324 as already landed") names nothing either: the number +// after "adoption of" is the superseded original, not the PR that landed +// the change. + +var ( + alreadyLandedPhraseRE = regexp.MustCompile(`(?i)\b(already (landed|merged|on (the )?base|on [a-z0-9._/-]+|contains?|in place|there|done)|no-op|redundant|landed (this|the|it)|superseded by|identical (change|fix|section|lines?|diff|two-line)|same (change|fix|lines?) (already|landed|merged))\b`) + // alreadyLandedRefRE captures " #N" (and " + // owner/repo#N" / ".../pull/N"); the preposition is what tells the + // landing PR from a lineage mention. + alreadyLandedRefRE = regexp.MustCompile(`(?i)\b(via|through|in|by|since|landed in|merged (?:in|as|via|through))\s+(?:[\w.-]+/[\w.-]+)?#(\d+)\b`) + alreadyLandedPullURLRE = regexp.MustCompile(`(?i)\b(via|through|in|by|since)\s+https?://[^\s/]+/[\w.-]+/[\w.-]+/pull/(\d+)\b`) +) + +// AlreadyLandedClaim reports whether any of texts says the change is +// already on the base (an "already landed" / "no-op" / "redundant" +// phrase), whether or not it names the PR that landed it. +func AlreadyLandedClaim(texts ...string) bool { + for _, t := range texts { + if alreadyLandedPhraseRE.MatchString(t) { + return true + } + } + return false +} + +// AlreadyLandedPR returns the PR the review found already carries this +// change: redundantOf (the `hive-redundant-of` marker) when set, else the +// one PR the texts name as having landed it, else 0. A text with an +// already-landed phrase but no landing reference, or with two different +// landing references, yields 0: the hive then proves redundancy +// mechanically or leaves the question to a person. +func AlreadyLandedPR(redundantOf int, texts ...string) int { + if redundantOf > 0 { + return redundantOf + } + found := 0 + for _, t := range texts { + if !alreadyLandedPhraseRE.MatchString(t) { + continue + } + for _, m := range alreadyLandedRefRE.FindAllStringSubmatch(t, -1) { + n, err := strconv.Atoi(m[2]) + if err != nil || n <= 0 { + continue + } + if found != 0 && found != n { + return 0 + } + found = n + } + for _, m := range alreadyLandedPullURLRE.FindAllStringSubmatch(t, -1) { + n, err := strconv.Atoi(m[2]) + if err != nil || n <= 0 { + continue + } + if found != 0 && found != n { + return 0 + } + found = n + } + } + return found +} + +// alreadyLandedTexts is the verdict text the stage engine and the relay +// read for the claim: the decision question first, then the per-perspective +// human summaries. +func alreadyLandedTexts(question string, summaries []string) []string { + out := make([]string, 0, 1+len(summaries)) + if q := strings.TrimSpace(question); q != "" { + out = append(out, q) + } + for _, s := range summaries { + if s = strings.TrimSpace(s); s != "" { + out = append(out, s) + } + } + return out +} + +// AlreadyLandedFromVerdict is AlreadyLandedPR over a verdict's decision +// question and human summaries; claimed is AlreadyLandedClaim over the +// same texts (true whenever landed > 0). +func AlreadyLandedFromVerdict(redundantOf int, question string, summaries []string) (landed int, claimed bool) { + texts := alreadyLandedTexts(question, summaries) + landed = AlreadyLandedPR(redundantOf, texts...) + return landed, landed > 0 || AlreadyLandedClaim(texts...) +} diff --git a/src/pkg/review/already_landed_test.go b/src/pkg/review/already_landed_test.go new file mode 100644 index 0000000000..4817af9753 --- /dev/null +++ b/src/pkg/review/already_landed_test.go @@ -0,0 +1,52 @@ +package review + +import "testing" + +// The two reviewer questions that parked hivecommons/hive#15647 and #15648 +// at 085 on 2026-10-29: both name the PR that landed the change, neither +// carried its number anywhere the stage engine read it. +const ( + alreadyLandedQuestion15647 = "@clubanderson should decide whether to close this adoption of #15324 as already landed, since v5 already contains the identical two-line test change via #15506." + alreadyLandedQuestion15648 = "@clubanderson should decide whether to close this adoption of #15343 as already landed, since #15341 (same author) removed the identical \"Ways to run Hive\" section from src/docs/README.md on v5." +) + +func TestAlreadyLandedPRFromReviewerWording(t *testing.T) { + for _, tc := range []struct { + name string + text string + want int + }{ + {name: "15647 via", text: alreadyLandedQuestion15647, want: 15506}, + {name: "15648 since", text: alreadyLandedQuestion15648, want: 15341}, + {name: "owner/repo ref", text: "This is a no-op: the identical fix merged via hivecommons/hive#15301.", want: 15301}, + {name: "pull url", text: "Already landed through https://github.com/hivecommons/hive/pull/15301 — close?", want: 15301}, + {name: "question mentions a PR but not redundancy", text: "@clubanderson should decide whether the flag introduced in #15506 should default on before this PR merges.", want: 0}, + {name: "lineage mention only", text: "@clubanderson should decide whether to close this adoption of #15324 as already landed.", want: 0}, + {name: "two different landing refs are ambiguous", text: "Already landed via #15506 and via #15507.", want: 0}, + {name: "no text", text: "", want: 0}, + } { + t.Run(tc.name, func(t *testing.T) { + if got := AlreadyLandedPR(0, tc.text); got != tc.want { + t.Fatalf("AlreadyLandedPR(%q) = %d, want %d", tc.text, got, tc.want) + } + }) + } + if got := AlreadyLandedPR(15506, "rename the flag?"); got != 15506 { + t.Fatalf("marker must win: got %d", got) + } +} + +func TestAlreadyLandedFromVerdict(t *testing.T) { + landed, claimed := AlreadyLandedFromVerdict(0, alreadyLandedQuestion15647, nil) + if landed != 15506 || !claimed { + t.Fatalf("15647: landed=%d claimed=%v", landed, claimed) + } + landed, claimed = AlreadyLandedFromVerdict(0, "@clubanderson should confirm this can be closed as a no-op.", []string{"correctness: the base already contains this change."}) + if landed != 0 || !claimed { + t.Fatalf("claim without a number: landed=%d claimed=%v", landed, claimed) + } + landed, claimed = AlreadyLandedFromVerdict(0, "rename the flag to --dry-run?", nil) + if landed != 0 || claimed { + t.Fatalf("ordinary question: landed=%d claimed=%v", landed, claimed) + } +} diff --git a/src/pkg/stagelabels/human.go b/src/pkg/stagelabels/human.go index 588c03bfdb..0ebe26ff0c 100644 --- a/src/pkg/stagelabels/human.go +++ b/src/pkg/stagelabels/human.go @@ -28,6 +28,7 @@ import ( "regexp" "slices" "sort" + "strconv" "strings" "time" @@ -258,6 +259,11 @@ const ( // closed, or the merged PR declares it) instead of parking it at 085 on // a reviewer's requires_human (hivecommons/hive#15208). LaneReasonSupersededByMerge = "superseded-by-merge" + // LaneReasonAlreadyLanded is the ledger reason when the hive closes its + // own PR because the reviewer's verdict on the head says the change is + // already on the base and the closer proved it (hivecommons/hive#15647); + // AlreadyLandedReason appends the landing PR (":#N") when known. + LaneReasonAlreadyLanded = "self-authored:already-landed" // LaneReasonAdoptionStalledPrefix opens the 085 reason for an adopted // PR (074) whose head sat past the stage dwell with nothing the hive // could push; the suffix is the lane reason it was stuck on @@ -649,3 +655,26 @@ func HumanAuthorOwesAnswer(e Evidence) bool { func IsDecisionRequestedReason(reason string) bool { return strings.EqualFold(strings.TrimSpace(reason), "contributor-lane:decision-requested") } + +// AlreadyLandedReason is LaneReasonAlreadyLanded with the PR that landed +// the change (":#N"), or bare when none was named. +func AlreadyLandedReason(landed int) string { + if landed <= 0 { + return LaneReasonAlreadyLanded + } + return LaneReasonAlreadyLanded + ":#" + strconv.Itoa(landed) +} + +// AlreadyLandedCaption is the tracker caption for an AlreadyLandedReason +// ("already landed via #N — closing as superseded"); ok is false for any +// other reason. +func AlreadyLandedCaption(reason string) (caption string, ok bool) { + r := strings.TrimSpace(reason) + if r != LaneReasonAlreadyLanded && !strings.HasPrefix(r, LaneReasonAlreadyLanded+":#") { + return "", false + } + if n, err := strconv.Atoi(strings.TrimPrefix(r, LaneReasonAlreadyLanded+":#")); err == nil && n > 0 { + return "already landed via #" + strconv.Itoa(n) + " — closing as superseded", true + } + return "already landed on the base — closing as superseded", true +} diff --git a/src/pkg/stagelabels/human_test.go b/src/pkg/stagelabels/human_test.go index 39ca1399e6..575813d3ed 100644 --- a/src/pkg/stagelabels/human_test.go +++ b/src/pkg/stagelabels/human_test.go @@ -703,3 +703,18 @@ func TestEntryCommentRepeatsDecisionAcrossHeads(t *testing.T) { t.Fatalf("stage flap lost the decision identity: %+v", rec) } } + +func TestAlreadyLandedReasonCaption(t *testing.T) { + if got := AlreadyLandedReason(15506); got != "self-authored:already-landed:#15506" { + t.Fatalf("reason=%q", got) + } + if c, ok := AlreadyLandedCaption(AlreadyLandedReason(15506)); !ok || c != "already landed via #15506 — closing as superseded" { + t.Fatalf("caption=%q ok=%v", c, ok) + } + if c, ok := AlreadyLandedCaption(AlreadyLandedReason(0)); !ok || c != "already landed on the base — closing as superseded" { + t.Fatalf("bare caption=%q ok=%v", c, ok) + } + if _, ok := AlreadyLandedCaption("superseded-by-merge"); ok { + t.Fatal("other reasons are not already-landed") + } +} From 7d4537c695da935d1d8c28afc78f86c24b3b35d2 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Fri, 30 Oct 2026 00:35:17 -0400 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=90=9B=20test:=20stage=20spy=20forwar?= =?UTF-8?q?ds=20the=20stage=20reconciler;=20drop=20unused=20reason=20const?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andrew Anderson --- .../github/automerge/automerge_stage_labels_test.go | 12 +++++++++++- src/pkg/github/pr_empty_diff_hive_autoclose.go | 4 ---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/pkg/github/automerge/automerge_stage_labels_test.go b/src/pkg/github/automerge/automerge_stage_labels_test.go index 845f28f4e5..c81e4a006e 100644 --- a/src/pkg/github/automerge/automerge_stage_labels_test.go +++ b/src/pkg/github/automerge/automerge_stage_labels_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "log/slog" "net/http" "net/http/httptest" "path/filepath" @@ -2600,12 +2601,17 @@ func TestSupersededCheckWarrantedForRedundantOfDecision(t *testing.T) { // rule closes it. type alreadyLandedCloserSpy struct { Transport + stage stageReconcilerTransport supersededCalls []int landedCalls []int redundantOf []int result hgithub.HiveEmptyDiffClose } +func (s *alreadyLandedCloserSpy) StageReconciler(store *stagelabels.Store, opts *stageengine.Options, logger *slog.Logger) stageengine.Reconciler { + return s.stage.StageReconciler(store, opts, logger) +} + func (s *alreadyLandedCloserSpy) CloseSupersededHivePRIfEligible(_ context.Context, _ string, pr *gh.PullRequest) (hgithub.HiveSupersededClose, error) { s.supersededCalls = append(s.supersededCalls, pr.GetNumber()) return hgithub.HiveSupersededClose{Reason: "issue-open"}, nil @@ -2656,7 +2662,11 @@ func TestApplyStageLabelsHiveAlreadyLandedClosesInsteadOfDecision(t *testing.T) e, _, store := newStageEngine(t, srv.URL, clock) e.trustedAuthorPolicy = func() TrustedAuthorPolicy { return TrustedAuthorPolicy{Enabled: true} } e.contributorLanePolicy = func() ContributorLanePolicy { return ContributorLanePolicy{} } - spy := &alreadyLandedCloserSpy{Transport: e.transport, result: hgithub.HiveEmptyDiffClose{Closed: true, Empty: true, RedundantOf: landed, Reason: hgithub.SupersessionActionClosedAlreadyLanded, Supersedes: []int{original}}} + stage, ok := e.transport.(stageReconcilerTransport) + if !ok { + t.Fatal("engine transport provides no stage writer") + } + spy := &alreadyLandedCloserSpy{Transport: e.transport, stage: stage, result: hgithub.HiveEmptyDiffClose{Closed: true, Empty: true, RedundantOf: landed, Reason: hgithub.SupersessionActionClosedAlreadyLanded, Supersedes: []int{original}}} e.transport = spy ctx := context.Background() diff --git a/src/pkg/github/pr_empty_diff_hive_autoclose.go b/src/pkg/github/pr_empty_diff_hive_autoclose.go index aa5d10ab23..275e778da5 100644 --- a/src/pkg/github/pr_empty_diff_hive_autoclose.go +++ b/src/pkg/github/pr_empty_diff_hive_autoclose.go @@ -70,10 +70,6 @@ const ( // stays with the reviewer's question). alreadyLandedMaxFiles = 25 - // alreadyLandedReason is the stage-label log reason of a close taken - // on the reviewer's already-landed verdict. - alreadyLandedReason = "self-authored:already-landed" - hiveEmptyDiffReasonHasDiff = "has-diff" hiveEmptyDiffReasonUnknown = "diff-unknown" hiveEmptyDiffReasonTooFresh = "head-too-fresh" From 40c055abc27182f7902119d300ebcb3cf7bc2218 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Fri, 30 Oct 2026 01:05:22 -0400 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=90=9B=20writeback:=20annotate=20the?= =?UTF-8?q?=20gated=20already-landed=20writes=20for=20the=20direct-write?= =?UTF-8?q?=20ratchet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andrew Anderson --- src/pkg/github/automerge/automerge_stage_human.go | 4 ++-- src/pkg/github/pr_empty_diff_hive_autoclose.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/pkg/github/automerge/automerge_stage_human.go b/src/pkg/github/automerge/automerge_stage_human.go index b99af69213..2b62415978 100644 --- a/src/pkg/github/automerge/automerge_stage_human.go +++ b/src/pkg/github/automerge/automerge_stage_human.go @@ -309,14 +309,14 @@ func (c *Engine) settleAlreadyLandedLineage(ctx context.Context, obs stageObserv 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 { + 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 { + 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 } diff --git a/src/pkg/github/pr_empty_diff_hive_autoclose.go b/src/pkg/github/pr_empty_diff_hive_autoclose.go index 275e778da5..0b0db89aff 100644 --- a/src/pkg/github/pr_empty_diff_hive_autoclose.go +++ b/src/pkg/github/pr_empty_diff_hive_autoclose.go @@ -392,13 +392,13 @@ func (c *Client) closeIssuesAlreadyLanded(ctx context.Context, owner, name strin continue } note := alreadyLandedIssueNote(pr.GetNumber(), redundantOf, strings.TrimSpace(pr.GetBase().GetRef())) - if _, _, err := c.client.Issues.CreateComment(ctx, owner, name, number, &gh.IssueComment{Body: gh.Ptr(note)}); err != nil { + if _, _, err := c.client.Issues.CreateComment(ctx, owner, name, number, &gh.IssueComment{Body: gh.Ptr(note)}); err != nil { // writeback:gated if c.logger != nil { c.logger.Warn("already-landed auto-close: issue note failed", slog.String("repo", repo), slog.Int("issue", number), slog.String("error", err.Error())) } continue } - if err := c.CloseIssue(ctx, repo, number, IssueCloseOptions{StateReason: IssueStateReasonCompleted}); err != nil { + if err := c.CloseIssue(ctx, repo, number, IssueCloseOptions{StateReason: IssueStateReasonCompleted}); err != nil { // writeback:gated if c.logger != nil { c.logger.Warn("already-landed auto-close: issue left open", slog.String("repo", repo), slog.Int("issue", number), slog.String("error", err.Error())) }