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-overlap-guard-hive-agent-prs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix the create-PR duplicate guard refusing an agent's PR because a dependency-bot PR (Renovate, Dependabot, or a custom-named App such as `mergeraptor[bot]`) edits one of the same files: the overlapping-files check now counts only PRs this hive's agents opened (the App bot plus `github.additional_bot_logins`). When it does refuse a request, the issues the request was for get a note naming the branch and the blocking PR, are no longer staged under a PR that does not reference them, and the refusal is logged at WARN.
1 change: 1 addition & 0 deletions src/docs/notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ without a class. Review submissions and review requests are always counted as
| `github.(*Client).commentOnHumanFiledTwin` | `attention` | |
| `github.(*Client).createIssue` | `attention` | |
| `github.(*Client).handleOneIssueRequest` | `attention` | |
| `github.(*Client).noteDuplicateGuardOnIssues` | `attention` | tells the blocked agent's issue that its branch was not opened as a PR because another hive agent's open PR edits an overlapping set of files; write-back gate consulted first |
| `github.(*Client).parkClankerRequestedPR` | `attention` | |
| `github.(*Client).parkUnansweredNoPRIssue` | `attention` | the park note on a stalled 010 issue the hive answered without a PR |
| `github.(*Client).redirectToExistingPR` | `attention` | |
Expand Down
1 change: 1 addition & 0 deletions src/pkg/github/notification_write_sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ var notificationWriteSites = map[string]NotificationWriteSite{
"github.(*Client).parkClankerRequestedPR": {Class: NotificationClassAttention},
"github.(*Client).parkUnansweredNoPRIssue": {Class: NotificationClassAttention},
"github.(*Client).redirectToExistingPR": {Class: NotificationClassAttention},
"github.(*Client).noteDuplicateGuardOnIssues": {Class: NotificationClassAttention},
"github.(*Client).resolveDuplicateFixGroup": {Class: NotificationClassAttention},
"github.attentionDelivery.Post": {Class: NotificationClassAttention},
"main.claimEscalationLookup": {Class: NotificationClassAttention},
Expand Down
88 changes: 82 additions & 6 deletions src/pkg/github/pr_duplicate_guard.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ import (

"github.com/hivecommons/hive/pkg/dupsweep"
"github.com/hivecommons/hive/pkg/prstatus"
"github.com/hivecommons/hive/pkg/writeback"
)

// Duplicate reasons reported in CreatePRResult.DuplicateReason.
const (
// DuplicateReasonClosesIssue: an open PR already carries Closes/Fixes for
// an issue this PR also closes.
DuplicateReasonClosesIssue = "closes-issue"
// DuplicateReasonOverlappingFiles: an open App-bot PR edits an overlapping
// file set (identical, or one set contained in the other).
// DuplicateReasonOverlappingFiles: an open PR this hive's own App bot (or
// another configured hive bot) opened edits an overlapping file set
// (identical, or one set contained in the other).
DuplicateReasonOverlappingFiles = "overlapping-files"
// DuplicateReasonMergedFix: a merged PR on the same base already closed an
// issue this PR also closes.
Expand Down Expand Up @@ -147,13 +149,25 @@ func (c *Client) findDuplicatePR(ctx context.Context, owner, repo, head, base, b
}

// findOverlappingBotPR runs the dupsweep file-set clustering synchronously over
// head's changed files and each open App-bot PR's. Overlap means identical sets
// or one set contained in the other (the #1434 ⊃ #1413 shape); a single shared
// path is not enough, because that is where the false positives live.
// head's changed files and each open hive-agent PR's. Overlap means identical
// sets or one set contained in the other (the #1434 ⊃ #1413 shape); a single
// shared path is not enough, because that is where the false positives live.
//
// Only PRs this hive's agents opened count: the App bot's own login plus any
// github.additional_bot_logins (hiveBotLogins). The guard exists so two agents
// do not race each other on one change. Any other bot, such as Renovate,
// Dependabot or a custom-named dependency App (mergeraptor[bot] on
// projectbluefin), is not another agent: a one-line digest bump in a file a
// refactor also edits is contained in the refactor's file set, and treating
// it as the home for the refactor dropped the agent's work onto a PR nobody
// will continue (projectbluefin/server#637). A client with no hive bot login
// (PAT auth, nothing configured) has no agent PRs to recognise, so the check
// does not run.
func (c *Client) findOverlappingBotPR(ctx context.Context, owner, repo, head, base string, candidates []*gh.PullRequest) (*duplicateFinding, error) {
hiveBots := c.hiveBotLogins()
var bots []*gh.PullRequest
for _, pr := range candidates {
if !strings.HasSuffix(pr.GetUser().GetLogin(), "[bot]") {
if !isHiveBotLogin(hiveBots, pr.GetUser().GetLogin()) {
continue
}
if reason := c.unmergeableLabelReason(pr); reason != "" {
Expand Down Expand Up @@ -312,3 +326,65 @@ func (c *Client) redirectToExistingPR(ctx context.Context, owner, repo, head str
slog.String("repo", repo), slog.Int("number", f.pr.GetNumber()), slog.String("error", err.Error()))
}
}

// duplicateGuardIssueNote is the note left on an issue whose agent's PR the
// overlapping-files check refused. It names the branch, so the work can be
// found, and the PR that blocked it.
func duplicateGuardIssueNote(agent, head string, blocking int) string {
who := "A hive agent"
if agent = strings.TrimSpace(agent); agent != "" {
who = "Hive agent `" + agent + "`"
}
return fmt.Sprintf("%s\n%s pushed work for this issue on branch `%s`, but no pull request was opened for it: "+
"open PR #%d, from another hive agent, edits an overlapping set of files. The branch is still on the remote. "+
"Continue the work on #%d, or request the PR again once #%d has merged or closed.",
duplicateGuardMarker, who, head, blocking, blocking, blocking)
}

// noteDuplicateGuardOnIssues tells the issues a refused PR request was for
// that the agent's branch did not become a PR, and why. The redirect on the
// blocking PR (redirectToExistingPR) reaches that PR's watchers; this note
// reaches the people following the blocked work. Only the overlapping-files
// refusal needs it: the blocking PR shares files with the request, not
// issues, so nothing on the issue would otherwise say the work stalled.
//
// Best-effort like the redirect: the refusal is the enforcement. Each write
// consults the write-back gate first; with the attention pilot on the note
// is a section of the issue's status card, otherwise one marked comment per
// head, never stacked on a retry.
func (c *Client) noteDuplicateGuardOnIssues(ctx context.Context, req PRRequest, blocking int, issues []int) {
for _, n := range issues {
if n <= 0 || n == blocking {
continue
}
body := duplicateGuardIssueNote(req.Agent, req.Head, blocking)
item, err := c.WritebackItemForNumber(ctx, req.Repo, n)
if err != nil {
c.logger.Warn("duplicate guard: could not read the blocked issue, skipping its note",
slog.String("repo", req.Repo), slog.Int("issue", n), slog.String("error", err.Error()))
continue
}
if d := c.WritebackAllowed(req.Repo, item, writeback.KindDuplicateFixNotice, body); !d.Allowed {
c.logger.Info("duplicate guard: write-back policy withheld the note on the blocked issue",
slog.String("repo", req.Repo), slog.Int("issue", n), slog.String("reason", d.Reason))
continue
}
_, err = c.PostIssueNote(ctx, req.Repo, n, prstatus.KeyDuplicateGuard, body, func(ctx context.Context) error {
owner, name := c.splitRepo(req.Repo)
comments, err := c.listIssueComments(ctx, owner, name, n)
if err != nil {
return err
}
for _, cm := range comments {
if strings.Contains(cm.GetBody(), duplicateGuardMarker) && strings.Contains(cm.GetBody(), "`"+req.Head+"`") {
return nil
}
}
return c.CreateIssueComment(ctx, req.Repo, n, body) // writeback:gated — WritebackAllowed consulted above
})
if err != nil {
c.logger.Warn("duplicate guard: could not leave the note on the blocked issue",
slog.String("repo", req.Repo), slog.Int("issue", n), slog.String("error", err.Error()))
}
}
}
165 changes: 165 additions & 0 deletions src/pkg/github/pr_duplicate_guard_bots_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package github

import (
"context"
"encoding/json"
"os"
"strings"
"testing"
"time"

"github.com/hivecommons/hive/pkg/stageengine/stageenginetest"
)

// projectbluefin/server#637: a Renovate digest bump (authored by the
// custom-named mergeraptor[bot] App) edits only Justfile. An architect's
// refactor of the same Justfile plus two test files contains that set, and
// the overlapping-files check refused the refactor in favour of the bump.
// Only PRs this hive's agents opened may absorb a request.
func TestCreatePR_DuplicateGuardOnlyCountsHiveAgentPRs(t *testing.T) {
const appBot = "hivecommons-hive[bot]"
headFiles := []string{"Justfile", "tests/unit/test_nvidia_container_toolkit_sysext.py", "tests/unit/test_sysext_export_contract.py"}
for _, tc := range []struct {
name string
author string
appLogin string
extraBots []string
wantNumber int
}{
{name: "renovate-style custom App bump does not absorb the refactor", author: "mergeraptor[bot]", appLogin: appBot, wantNumber: 99},
{name: "renovate bump does not absorb the refactor", author: "renovate[bot]", appLogin: appBot, wantNumber: 99},
{name: "dependabot bump does not absorb the refactor", author: "dependabot[bot]", appLogin: appBot, wantNumber: 99},
{name: "the hive's own agent PR still absorbs it", author: appBot, appLogin: appBot, wantNumber: 637},
{name: "login match ignores case", author: "HiveCommons-Hive[bot]", appLogin: appBot, wantNumber: 637},
{name: "a configured additional hive bot still absorbs it", author: "other-hive[bot]", appLogin: appBot, extraBots: []string{"other-hive[bot]"}, wantNumber: 637},
{name: "no hive bot login known: no PR is treated as an agent's", author: "some-app[bot]", wantNumber: 99},
} {
t.Run(tc.name, func(t *testing.T) {
g := &guardServer{
open: []guardPR{{Number: 637, Ref: "renovate/registry.fedoraproject.org-fedora-44", User: tc.author, Files: []string{"Justfile"}}},
headFiles: headFiles,
}
srv := g.start(t)
c := NewClientForTest(srv.URL, "o", nil, prTestLogger())
c.SetAppBotLogin(tc.appLogin)
c.SetAdditionalBotLogins(tc.extraBots)
res, err := c.CreatePR(context.Background(), "o/r", "arch/refactor-sysext-export", "main", "title", "Refs #638")
if err != nil {
t.Fatalf("CreatePR: %v", err)
}
if res.Number != tc.wantNumber {
t.Fatalf("got #%d reason %q, want #%d", res.Number, res.DuplicateReason, tc.wantNumber)
}
if tc.wantNumber == 99 {
if res.IsDuplicate() || g.created != 1 || g.comments != 0 {
t.Errorf("want a fresh PR and no redirect: duplicate=%v created=%d comments=%d", res.IsDuplicate(), g.created, g.comments)
}
return
}
if res.DuplicateReason != DuplicateReasonOverlappingFiles || g.created != 0 {
t.Errorf("want an overlapping-files refusal: reason %q created=%d", res.DuplicateReason, g.created)
}
})
}
}

// When the guard refuses a request on overlapping files, the issues the
// request was for are not staged under the blocking PR (it shares files, not
// issues) and each gets a note naming the branch and the blocking PR. A
// closes-issue refusal still stages the issue under the PR that closes it.
func TestPRRequestWatcher_OverlapRefusalNotesTheBlockedIssue(t *testing.T) {
const appBot = "hivecommons-hive[bot]"
run := func(t *testing.T, g *guardServer, body string) (*guardServer, *stageenginetest.Recorder, PRResponse) {
t.Helper()
srv := g.start(t)
c := testClient(t, srv.URL)
enableInProgress(c, "hive-a", time.Hour)
c.SetAppBotLogin(appBot)
sink := &stageenginetest.Recorder{}
c.SetStageFactSink(sink)
dir := t.TempDir()
old := prRequestDirForTest
prRequestDirForTest = dir
t.Cleanup(func() { prRequestDirForTest = old })
reqPath, err := WritePRRequest(dir, PRRequest{Repo: "o/r", Head: "arch/refactor-sysext-export", Title: "refactor(just): share one export body", Body: body, Agent: "architect", IssueN: []int{638}})
if err != nil {
t.Fatal(err)
}
c.ProcessPRRequestsOnce(context.Background())
raw, err := os.ReadFile(strings.TrimSuffix(reqPath, ".json") + ".result.json")
if err != nil {
t.Fatalf("result file: %v", err)
}
var resp PRResponse
if err := json.Unmarshal(raw, &resp); err != nil {
t.Fatal(err)
}
return g, sink, resp
}

t.Run("overlapping files", func(t *testing.T) {
g, sink, resp := run(t, &guardServer{
open: []guardPR{{Number: 640, Ref: "scanner/fix-639", User: appBot, Files: []string{"Justfile"}}},
headFiles: []string{"Justfile", "tests/unit/test_sysext_export_contract.py"},
}, "Refs #638")
if !resp.OK || resp.Number != 640 || resp.DuplicateReason != DuplicateReasonOverlappingFiles {
t.Fatalf("result = %+v, want an overlapping-files refusal onto #640", resp)
}
if facts := sink.All(); len(facts) != 0 {
t.Errorf("issue staged under a PR that does not reference it: %+v", facts)
}
var onIssue, onPR int
for _, p := range g.commentPaths {
switch {
case strings.HasSuffix(p, "/issues/638/comments"):
onIssue++
case strings.HasSuffix(p, "/issues/640/comments"):
onPR++
}
}
if onIssue != 1 || onPR != 1 {
t.Errorf("comment paths = %v, want one note on #638 and one redirect on #640", g.commentPaths)
}
})

t.Run("note is not stacked on a retried request", func(t *testing.T) {
g, _, _ := run(t, &guardServer{
open: []guardPR{{Number: 640, Ref: "scanner/fix-639", User: appBot, Files: []string{"Justfile"}}},
headFiles: []string{"Justfile", "tests/unit/test_sysext_export_contract.py"},
existingComments: []string{duplicateGuardIssueNote("architect", "arch/refactor-sysext-export", 640)},
}, "Refs #638")
if len(g.commentPaths) != 0 {
t.Errorf("comment paths = %v, want none (both notes already present)", g.commentPaths)
}
})

t.Run("closes-issue refusal still stages the issue", func(t *testing.T) {
g, sink, resp := run(t, &guardServer{
open: []guardPR{{Number: 641, Ref: "scanner/fix-638", Body: "Fixes #638", User: appBot}},
}, "Closes #638")
if resp.DuplicateReason != DuplicateReasonClosesIssue {
t.Fatalf("result = %+v, want a closes-issue refusal", resp)
}
facts := sink.All()
if len(facts) != 1 || facts[0].Ref.Number != 638 || facts[0].PR == nil || facts[0].PR.Number != 641 {
t.Errorf("facts = %+v, want #638 staged under #641", facts)
}
for _, p := range g.commentPaths {
if strings.HasSuffix(p, "/issues/638/comments") {
t.Errorf("closes-issue refusal left a guard note on the issue: %v", g.commentPaths)
}
}
})
}

func TestDuplicateGuardIssueNote(t *testing.T) {
got := duplicateGuardIssueNote("architect", "arch/x", 640)
for _, want := range []string{duplicateGuardMarker, "Hive agent `architect`", "`arch/x`", "open PR #640", "still on the remote"} {
if !strings.Contains(got, want) {
t.Errorf("note missing %q:\n%s", want, got)
}
}
if anon := duplicateGuardIssueNote("", "arch/x", 640); !strings.Contains(anon, "A hive agent pushed") {
t.Errorf("anonymous note = %q", anon)
}
}
10 changes: 10 additions & 0 deletions src/pkg/github/pr_duplicate_guard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ type guardServer struct {
created int
comments int
edits int
// commentPaths records the path of every comment POST, so a test can
// tell a note on an issue from the redirect on the existing PR.
commentPaths []string
}

func (g *guardServer) start(t *testing.T) *httptest.Server {
Expand Down Expand Up @@ -165,6 +168,8 @@ func (g *guardServer) start(t *testing.T) *httptest.Server {
out = append(out, map[string]any{"id": i + 1, "body": body, "user": map[string]any{"login": "hive-app[bot]", "type": "Bot"}})
}
enc(w, out)
case r.Method == "GET" && strings.HasPrefix(path, "/repos/o/r/labels/"):
enc(w, map[string]any{"name": "x"})
case r.Method == "GET" && strings.Contains(path, "/issues/"):
var num int
fmt.Sscanf(strings.TrimPrefix(path, "/repos/o/r/issues/"), "%d", &num)
Expand All @@ -178,6 +183,7 @@ func (g *guardServer) start(t *testing.T) *httptest.Server {
case r.Method == "POST" && strings.HasSuffix(path, "/comments"):
g.mu.Lock()
g.comments++
g.commentPaths = append(g.commentPaths, path)
g.mu.Unlock()
_, _ = io.WriteString(w, `{"id":1}`)
case r.Method == "PATCH" && strings.Contains(path, "/issues/comments/"):
Expand Down Expand Up @@ -384,6 +390,7 @@ func TestCreatePR_DuplicateGuard(t *testing.T) {
g := tt.g
srv := g.start(t)
c := NewClientForTest(srv.URL, "o", nil, prTestLogger())
c.SetAppBotLogin(bot)
c.SetHoldLabels(tt.holdLabels)
res, err := c.CreatePR(context.Background(), "o/r", "new-branch", "main", "title", tt.body)
if err != nil {
Expand Down Expand Up @@ -425,6 +432,7 @@ func TestCreatePR_DuplicateGuardRetryableFailuresFailClosed(t *testing.T) {
}
srv := g.start(t)
c := NewClientForTest(srv.URL, "o", nil, prTestLogger())
c.SetAppBotLogin("hive[bot]")
_, err := c.CreatePR(context.Background(), "o/r", "new-branch", "main", "title", "Closes #42")
// The open-PR list is shared with the duplicate-tree guard, which
// may report the failure first; either way the request stays queued.
Expand Down Expand Up @@ -470,6 +478,7 @@ func TestCreatePR_DuplicateGuardSkipsUnreadableCandidate(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
srv := tc.g.start(t)
c := NewClientForTest(srv.URL, "o", nil, prTestLogger())
c.SetAppBotLogin("hive[bot]")
res, err := c.CreatePR(context.Background(), "o/r", "new-branch", "main", "title", "no keyword")
if err != nil {
t.Fatalf("CreatePR: %v", err)
Expand Down Expand Up @@ -498,6 +507,7 @@ func TestCreatePR_DuplicateGuardStopsAtCandidateCap(t *testing.T) {
g.open = append(g.open, guardPR{Number: 1999, Ref: "b1999", User: bot, Files: []string{"dup.go"}})
srv := g.start(t)
c := NewClientForTest(srv.URL, "o", nil, prTestLogger())
c.SetAppBotLogin("hive[bot]")
res, err := c.CreatePR(context.Background(), "o/r", "new-branch", "main", "title", "no keyword")
if err != nil {
t.Fatalf("CreatePR: %v", err)
Expand Down
18 changes: 16 additions & 2 deletions src/pkg/github/pr_request_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,8 +709,22 @@ func (c *Client) handleOnePRRequest(ctx context.Context, path string, nowFn func
// reconciler keeps them while the PR is open (the PR's own stage is
// narrated in the issue's stage reason, never written as its label).
covered, partial := c.prCoveredIssues(req.Repo, body, req.IssueN)
c.clearInProgressForPR(ctx, req.Repo, res.Number, covered)
c.addressInProgressForPR(ctx, req.Repo, res.Number, partial)
if res.DuplicateReason == DuplicateReasonOverlappingFiles {
// The guard pointed at a PR that shares files with this request, not
// issues: it does not cover them, so they are not staged under it
// (projectbluefin/server#638 sat at 040 under an unrelated digest
// bump until the reconciler noticed). The issues are told instead
// and the refusal is logged at WARN, because the agent's own
// hive-open-pr call returned before this ran and never sees it.
blocked := append(append([]int(nil), covered...), partial...)
c.logger.Warn("pr-request watcher: no PR opened — another hive agent's open PR edits an overlapping set of files; the branch stays on the remote",
slog.String("repo", req.Repo), slog.String("head", req.Head), slog.String("agent", req.Agent),
slog.Int("blocking_pr", res.Number), slog.Any("issues", blocked))
c.noteDuplicateGuardOnIssues(ctx, req, res.Number, blocked)
} else {
c.clearInProgressForPR(ctx, req.Repo, res.Number, covered)
c.addressInProgressForPR(ctx, req.Repo, res.Number, partial)
}
c.recordWriteAudit(AuditActionAgentPRCreated, meta,
WriteTarget{Repo: req.Repo, Number: res.Number},
"author", res.Author,
Expand Down
4 changes: 3 additions & 1 deletion src/pkg/writeback/kinds.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ const (
// grace period.
KindSupersessionClose Kind = "supersession_close"
// KindDuplicateFixNotice is the `<!-- hive:duplicate-fix -->` comment on
// the later of two PRs fixing one issue.
// the later of two PRs fixing one issue, and the
// `<!-- hive:create-pr-duplicate-guard -->` note on an issue whose
// agent's PR the overlapping-files guard refused.
KindDuplicateFixNotice Kind = "duplicate_fix_notice"
// KindReporterTrustHold is the reporter-trust notice and needs-human
// label on a PR whose linked issue reporter is untrusted.
Expand Down
Loading