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-skip-linked-issue-dispatch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- The scanner, issue-request watcher and `/contribute` relay lane no longer dispatch an issue an open pull request already fixes: a live open-PR check (`scanner.skip_issues_with_open_pr`, default on) skips the claim, labels the issue `has-open-pr` and logs `claim skipped: open PR already linked`; and a duplicate-fix sweep resolves two open PRs fixing one issue without an owner decision — the earlier green/pending PR stays, the later hive PR is closed as `duplicate-of:<keeper>` with one comment, a person's later PR is commented on and left open, and only an earlier red/stale PR facing a later green one is escalated to `stage/085` ([#14799](https://github.com/hivecommons/hive/issues/14799)).
1 change: 1 addition & 0 deletions src/cmd/hive/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ type boot struct {
lastTaskListSweep time.Time
lastDuplicateSweep time.Time
lastSupersessionSweep time.Time
lastDuplicateFixSweep time.Time
lastSharedCIRerunSweep time.Time
lastBlockedIssuesSweep time.Time
lastIssueUnparkSweep time.Time
Expand Down
56 changes: 56 additions & 0 deletions src/cmd/hive/claims_admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,59 @@ func TestKickClaimsRefuseEpicsAndParkedIssues(t *testing.T) {
})
}
}

// hivecommons/hive#14799: #14766 was dispatched to a relay contributor six
// minutes after a person's PR fixing it opened, because admission read only
// the eval cycle's claim ledger. A fresh automated claim now asks the forge
// for an open PR linking the issue and is refused; a PR-backed start signal,
// a renewal and the release tick's recheck do not consult the listing, so
// the agent whose own PR links the issue keeps its claim.
func TestKickClaimsRefuseIssueWithOpenLinkedPR(t *testing.T) {
var pullsCalls atomic.Int32
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.URL.Path == "/repos/o/r/pulls":
pullsCalls.Add(1)
fmt.Fprint(w, `[{"number":14775,"state":"open","title":"fix","body":"Fixes #14766","html_url":"https://github.com/o/r/pull/14775","created_at":"2026-10-27T01:00:00Z","user":{"login":"clubanderson","type":"User"}}]`)
case r.URL.Path == "/repos/o/r/issues/14766":
fmt.Fprint(w, `{"number":14766,"labels":[{"name":"bug"}]}`)
case r.URL.Path == "/repos/o/r/issues/14766/labels", r.URL.Path == "/repos/o/r/labels":
fmt.Fprint(w, `[]`)
default:
w.WriteHeader(http.StatusNotFound)
}
}))
defer api.Close()
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
client := github.NewClientForTest(api.URL, "o", []string{"r"}, logger)
ledger := testClaimsLedger(t)
installClaimAdmissionCheck(context.Background(), ledger, func() *github.Client { return client })

res, err := ledger.Claim(claims.Request{Repo: "o/r", Issue: 14766, Holder: "relay", HolderID: "c-relay", Kind: claims.KindContributor})
if err != nil || res.Outcome != claims.OutcomeRefused || res.Reason != github.ClaimBlockReasonOpenPRPrefix+"14775" {
t.Fatalf("fresh claim = %+v, %v; want refused by the open PR", res, err)
}
if pullsCalls.Load() != 1 {
t.Fatalf("open-PR listings = %d, want 1", pullsCalls.Load())
}

// The PR author's own agent start signal carries PR evidence: admitted.
res, err = ledger.Claim(claims.Request{Repo: "o/r", Issue: 14766, Holder: "scanner", HolderID: "scanner", Kind: claims.KindAgent, PRStarted: true})
if err != nil || res.Outcome != claims.OutcomeClaimed {
t.Fatalf("PR-backed claim = %+v, %v; want claimed", res, err)
}
// Renewal and the release tick recheck the held claim without listing.
res, err = ledger.Claim(claims.Request{Repo: "o/r", Issue: 14766, Holder: "scanner", HolderID: "scanner", Kind: claims.KindAgent})
if err != nil || res.Outcome != claims.OutcomeRenewed {
t.Fatalf("renewal = %+v, %v; want renewed", res, err)
}
if n := ledger.ReleaseBlocked(); n != 0 {
t.Fatalf("release tick dropped %d claims, want 0: the holder's own PR is the open PR", n)
}
if _, ok := ledger.Lookup("o/r", 14766); !ok {
t.Fatal("agent claim must survive")
}
if pullsCalls.Load() != 1 {
t.Fatalf("open-PR listings = %d, want still 1 (renewal/tick never list)", pullsCalls.Load())
}
}
7 changes: 7 additions & 0 deletions src/cmd/hive/claimswire.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ func installClaimAdmissionCheck(ctx context.Context, ledger *claims.Ledger, clie
if gh == nil {
return "", github.ErrNoGitHubClient
}
// A fresh claim with no PR behind it is a dispatch: it also asks
// whether an open PR already fixes the issue (#14799). A renewal,
// a PR-backed start signal and the release tick's recheck of live
// claims do not — the holder's own PR is the open PR.
if _, held := ledger.Lookup(req.Repo, req.Issue); !held && !req.PRStarted {
return gh.IssueDispatchBlockReason(ctx, req.Repo, req.Issue)
}
return gh.IssueClaimBlockReason(ctx, req.Repo, req.Issue)
})
}
Expand Down
101 changes: 101 additions & 0 deletions src/cmd/hive/duplicate_fix_sweep_wiring_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package main

import (
"context"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"

"github.com/hivecommons/hive/pkg/config"
"github.com/hivecommons/hive/pkg/github"
)

// Wiring tests for runDuplicateFixSweepIfDue (hivecommons/hive#14799): the
// throttle, the scanner.skip_issues_with_open_pr switch, lastRun stamping and
// the error path. The resolver's decisions live in pkg/github.

func newDuplicateFixAPI(t *testing.T, status int, requests *atomic.Int64) *httptest.Server {
t.Helper()
mux := http.NewServeMux()
mux.HandleFunc("/repos/testorg/widget/pulls", func(w http.ResponseWriter, r *http.Request) {
requests.Add(1)
if status != http.StatusOK {
w.WriteHeader(status)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`[]`))
})
srv := httptest.NewServer(mux)
t.Cleanup(srv.Close)
return srv
}

func TestRunDuplicateFixSweepIfDue_NilClientIsNoop(t *testing.T) {
var lastRun time.Time
runDuplicateFixSweepIfDue(context.Background(), nil, nil, nil, &lastRun, sweepTestLogger(nil))
if !lastRun.IsZero() {
t.Fatalf("nil client must not stamp lastRun, got %v", lastRun)
}
}

func TestRunDuplicateFixSweepIfDue_SwitchOffIsNoop(t *testing.T) {
var requests atomic.Int64
srv := newDuplicateFixAPI(t, http.StatusOK, &requests)
ghClient := github.NewClientForTest(srv.URL, "testorg", []string{"widget"}, sweepTestLogger(nil))
off := false
cfg := &config.Config{}
cfg.Scanner.SkipIssuesWithOpenPR = &off

var lastRun time.Time
runDuplicateFixSweepIfDue(context.Background(), ghClient, cfg, nil, &lastRun, sweepTestLogger(nil))
if requests.Load() != 0 || !lastRun.IsZero() {
t.Fatalf("sweep must be off with scanner.skip_issues_with_open_pr=false (requests=%d lastRun=%v)", requests.Load(), lastRun)
}
}

func TestRunDuplicateFixSweepIfDue_ThrottledWithinInterval(t *testing.T) {
var requests atomic.Int64
srv := newDuplicateFixAPI(t, http.StatusOK, &requests)
ghClient := github.NewClientForTest(srv.URL, "testorg", []string{"widget"}, sweepTestLogger(nil))

lastRun := time.Now()
before := lastRun
runDuplicateFixSweepIfDue(context.Background(), ghClient, &config.Config{}, nil, &lastRun, sweepTestLogger(nil))
if requests.Load() != 0 || !lastRun.Equal(before) {
t.Fatalf("throttled sweep must make no API calls and keep lastRun (requests=%d)", requests.Load())
}
}

func TestRunDuplicateFixSweepIfDue_DueSweepStampsLastRun(t *testing.T) {
var requests atomic.Int64
srv := newDuplicateFixAPI(t, http.StatusOK, &requests)
ghClient := github.NewClientForTest(srv.URL, "testorg", []string{"widget"}, sweepTestLogger(nil))

var lastRun time.Time
start := time.Now()
runDuplicateFixSweepIfDue(context.Background(), ghClient, nil, nil, &lastRun, sweepTestLogger(nil))
if requests.Load() == 0 || lastRun.Before(start) {
t.Fatalf("due sweep must list PRs and stamp lastRun (requests=%d lastRun=%v)", requests.Load(), lastRun)
}
runDuplicateFixSweepIfDue(context.Background(), ghClient, nil, nil, nil, sweepTestLogger(nil))
}

func TestRunDuplicateFixSweepIfDue_ErrorLogged(t *testing.T) {
var requests atomic.Int64
srv := newDuplicateFixAPI(t, http.StatusInternalServerError, &requests)
ghClient := github.NewClientForTest(srv.URL, "testorg", []string{"widget"}, sweepTestLogger(nil))

var buf strings.Builder
var lastRun time.Time
runDuplicateFixSweepIfDue(context.Background(), ghClient, nil, nil, &lastRun, sweepTestLogger(&buf))
if !strings.Contains(buf.String(), "duplicate-fix sweep failed") {
t.Fatalf("sweep failure must be logged, log:\n%s", buf.String())
}
if lastRun.IsZero() {
t.Fatal("failed sweep must still stamp lastRun so it is not retried every cycle")
}
}
8 changes: 8 additions & 0 deletions src/cmd/hive/github_client_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func (b *boot) githubHoldLabels() []string {
return append(labels, b.cfg.Project.IssueFilter.ReporterTrust.ExtraHoldLabels()...)
}

// openPRLinkGuardTimeout bounds the live open-PR listing behind the
// contribute lane's pre-dispatch guard (hivecommons/hive#14799) so a slow
// forge cannot stall candidate selection; on timeout the guard fails open.
const openPRLinkGuardTimeout = 15 * time.Second

// applyGitHubClientConfigHooks installs the settings that depend on config
// alone. All of them are applied on every client regardless of auth mode.
func (b *boot) applyGitHubClientConfigHooks(client *github.Client) {
Expand All @@ -89,6 +94,9 @@ func (b *boot) applyGitHubClientConfigHooks(client *github.Client) {
// Umbrella issues no automated claim may take and no claim stages
// (governor.claims.never_claim_labels, default `epic`).
client.SetNeverClaimLabels(b.cfg.Governor.Claims.EffectiveNeverClaimLabels())
// Pre-dispatch open-PR guard (hivecommons/hive#14799,
// scanner.skip_issues_with_open_pr, default on).
client.SetSkipIssuesWithOpenPR(b.cfg.Scanner.IsSkipIssuesWithOpenPR())
client.SetAdditionalBotLogins(b.cfg.GitHub.AdditionalBotLogins)
client.SetSentinelAlertLabel(b.cfg.Sentinel.LabelOrDefault())
client.SetAPIBudgetThresholds(b.cfg.GitHub.APIReserve, b.cfg.GitHub.APICritical)
Expand Down
15 changes: 8 additions & 7 deletions src/cmd/hive/github_client_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,14 @@ var clientSetterAllowlist = map[string]string{
// a config reload or heartbeat claim. Each is also applied by
// configureGitHubClient (or allowlisted above), so a rebuild still gets it.
var clientResyncSetters = map[string]bool{
"SetRepos": true,
"SetHoldLabels": true,
"SetNeverClaimLabels": true,
"SetSentinelAlertLabel": true,
"SetExemptLabels": true,
"SetAutoMergeLabel": true,
"SetIssueFilter": true,
"SetRepos": true,
"SetHoldLabels": true,
"SetNeverClaimLabels": true,
"SetSkipIssuesWithOpenPR": true,
"SetSentinelAlertLabel": true,
"SetExemptLabels": true,
"SetAutoMergeLabel": true,
"SetIssueFilter": true,
}

const (
Expand Down
61 changes: 61 additions & 0 deletions src/cmd/hive/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

import (
Expand Down Expand Up @@ -1525,6 +1525,18 @@
IssueClaimed: func(repo string, number int) (github.IssueClaim, bool) {
return getClaimLedger(b.logger).Lookup(repo, number)
},
// hivecommons/hive#14799: the ledger above is one eval cycle old;
// ask the forge directly whether an open PR already links the
// issue before the contribute lane dispatches it again.
OpenPRLinkingIssue: func(repo string, number int, labels []string) (github.OpenPRLink, bool) {
client := b.currentGitHubClient()
if client == nil {
return github.OpenPRLink{}, false
}
ctx, cancel := context.WithTimeout(context.Background(), openPRLinkGuardTimeout)
defer cancel()
return client.OpenPRLinkGuard(ctx, repo, number, labels)
},
// #7871: and write access for the one verified fact a no_work_needed
// verdict produces — the PR/commit that already settled the issue.
RecordIssueClaim: func(c github.IssueClaim) error {
Expand Down Expand Up @@ -2670,6 +2682,7 @@
b.ghClient.SetRepos(b.cfg.Project.Repos)
b.ghClient.SetHoldLabels(b.githubHoldLabels())
b.ghClient.SetNeverClaimLabels(b.cfg.Governor.Claims.EffectiveNeverClaimLabels())
b.ghClient.SetSkipIssuesWithOpenPR(b.cfg.Scanner.IsSkipIssuesWithOpenPR())
b.ghClient.SetSentinelAlertLabel(b.cfg.Sentinel.LabelOrDefault())
if len(b.cfg.Governor.Labels.Exempt) > 0 {
b.ghClient.SetExemptLabels(b.cfg.Governor.Labels.Exempt)
Expand Down Expand Up @@ -5807,6 +5820,7 @@
}
b.ghClient.SetHoldLabels(b.githubHoldLabels())
b.ghClient.SetNeverClaimLabels(b.cfg.Governor.Claims.EffectiveNeverClaimLabels())
b.ghClient.SetSkipIssuesWithOpenPR(b.cfg.Scanner.IsSkipIssuesWithOpenPR())
b.ghClient.SetSentinelAlertLabel(b.cfg.Sentinel.LabelOrDefault())
b.ghClient.SetIssueFilter(b.cfg.Project.IssueFilter)
syncAutoMergePolicyToGitHubClient(b.cfg, b.ghClient)
Expand Down Expand Up @@ -9152,6 +9166,12 @@
// the same conservative cadence as the task-list sweep.
const supersessionSweepInterval = 15 * time.Minute

// duplicateFixSweepInterval is the minimum spacing between sweeps that
// resolve two open PRs fixing the same issue (hivecommons/hive#14799). It
// reuses the open-PR listing the pre-dispatch guard caches and reads check
// runs per pair, so it runs on the supersession cadence.
const duplicateFixSweepInterval = 15 * time.Minute

// sharedCIRerunInterval is the minimum spacing between shared-CI rerun
// sweeps. Each pass lists recently closed issues per repo and only walks open
// PRs when a [shared-ci] incident closed inside the lookback window, so it
Expand Down Expand Up @@ -9535,6 +9555,47 @@
})
}

// runDuplicateFixSweepIfDue resolves same-issue duplicate open PRs
// (hivecommons/hive#14799) at most once per duplicateFixSweepInterval: the
// earlier green-or-pending PR stays, the later hive PR is closed as
// duplicate-of:<keeper>, a person's later PR gets the comment only, and the
// earlier-red-later-green case is parked for the owner. It follows the
// pre-dispatch guard's switch: scanner.skip_issues_with_open_pr off turns
// this off too, since both exist to keep one issue on one PR.
func runDuplicateFixSweepIfDue(ctx context.Context, ghClient *github.Client, cfg *config.Config, dashSrv *dashboard.Server, lastRun *time.Time, logger *slog.Logger) {
if ghClient == nil {
return
}
if cfg != nil && !cfg.Scanner.IsSkipIssuesWithOpenPR() {
return
}
now := time.Now()
if lastRun != nil && !lastRun.IsZero() && now.Sub(*lastRun) < duplicateFixSweepInterval {
return
}
if lastRun != nil {
*lastRun = now
}
result, err := ghClient.SweepDuplicateFixPRs(ctx, github.DuplicateFixSweepOptions{
MaxActions: github.DefaultDuplicateFixSweepMaxActions,
Audit: func(event github.DuplicateFixEvent) {
if dashSrv == nil {
return
}
detail := fmt.Sprintf("repo=%s, pr=%d, issue=%s#%d, keeper_pr=%d, action=%s, reason=%s",
event.Repo, event.Number, event.IssueRepo, event.Issue, event.KeeperPR, event.Action, event.Reason)
dashSrv.AuditLogRecord("system", github.DuplicateFixSweepAuditPrefix+event.Action, detail, "", event.Repo, event.Number)
},
})
if err != nil {
logger.Warn("duplicate-fix sweep failed", "error", err)
return
}
if result.Pairs > 0 {
logger.Info("duplicate-fix sweep complete", "pairs", result.Pairs, "closed", len(result.Closed), "commented", len(result.Commented), "escalated", len(result.Escalated), "skipped", result.Skipped)
}
}

// runSharedCIRerunSweepIfDue re-runs failed jobs on open PRs deferred to a
// [shared-ci] incident that has since closed (hivecommons/hive#12253), at most
// once per sharedCIRerunInterval. Gates and dedupe live in
Expand Down
1 change: 1 addition & 0 deletions src/cmd/hive/run_loop_deps.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

import (
Expand Down Expand Up @@ -93,6 +93,7 @@
}
if !decision.SkipUnstick {
runSupersessionSweepIfDue(b.ctx, b.ghClient, b.cfg, b.dashSrv, &b.lastSupersessionSweep, b.logger)
runDuplicateFixSweepIfDue(b.ctx, b.ghClient, b.cfg, b.dashSrv, &b.lastDuplicateFixSweep, b.logger)
runSharedCIRerunSweepIfDue(b.ctx, b.ghClient, b.cfg, b.dashSrv, &b.lastSharedCIRerunSweep, b.logger)
runBlockedIssuesSweepIfDue(b.ctx, b.ghClient, b.cfg, b.dashSrv, &b.lastBlockedIssuesSweep, b.logger)
}
Expand Down
1 change: 1 addition & 0 deletions src/docs/github-write-surface.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ writes no entry.
| Task-list sweep closes | `task_list_sweep.go` | yes (`task-list-sweep-closed`) | yes |
| Supersession sweep (notice, `hive/superseded` close, own-PR update-branch + body `Closes`→`Refs` rewrite, `needs-human` + `Parked` fact on escalation) | `pr_supersession_sweep.go`, `pr_supersession_autoclose.go`, `pr_supersession_resolve.go` | yes (`supersession-sweep-*`) | yes |
| Shared-CI rerun (`rerun-failed-jobs`, `hive-shared-ci-rerun-<n>` comment) | `shared_ci_rerun.go` | yes (`shared-ci-rerun`) | yes |
| Open-PR dispatch guard (`has-open-pr` label on a refused issue) and duplicate-fix sweep (one `hive:duplicate-fix` comment; `hive/superseded` close of the later hive PR, or `needs-human` + `Parked` fact on escalation) | `open_pr_links.go`, `pr_duplicate_fix_sweep.go` | yes (`duplicate-fix-*`) | yes |
| Duplicate-PR sweep suggestions | `duplicate_sweep.go` | yes (`duplicate-sweep-suggested`) | yes (target = surviving PR) |
| Hold-label migration (one-time) | `hive_hold_migration.go` | yes (`hold_migration_label_added`, one per item labeled; the migration report file is still written) | yes |
| Origin-label migration (one-time; maps `from-review` / `origin/review` / `origin/reviewer` to `agent/reviewer`, deletes retired reviewer-origin label definitions after open items are swapped, removes redundant `origin/<lane>` and bare `<lane>` labels from agent-filed issues, and backfills `origin/selfreport` on open `hive/selfreport` issues when no provenance label is present) | `origin_labels.go` | yes (`origin_label_migrated`, one per item swapped or backfilled, with `from`, `to`; report file `/data/origin-label-migration-v4.json`) | yes |
Expand Down
Loading
Loading