diff --git a/changelog.d/fixed-skip-linked-issue-dispatch.md b/changelog.d/fixed-skip-linked-issue-dispatch.md new file mode 100644 index 0000000000..7377564945 --- /dev/null +++ b/changelog.d/fixed-skip-linked-issue-dispatch.md @@ -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:` 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)). diff --git a/src/cmd/hive/boot.go b/src/cmd/hive/boot.go index 2110d29067..ff131bf1c0 100644 --- a/src/cmd/hive/boot.go +++ b/src/cmd/hive/boot.go @@ -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 diff --git a/src/cmd/hive/claims_admission_test.go b/src/cmd/hive/claims_admission_test.go index 0a7860c8a0..649cff06c8 100644 --- a/src/cmd/hive/claims_admission_test.go +++ b/src/cmd/hive/claims_admission_test.go @@ -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()) + } +} diff --git a/src/cmd/hive/claimswire.go b/src/cmd/hive/claimswire.go index 1f3defcd9f..a5944f65d9 100644 --- a/src/cmd/hive/claimswire.go +++ b/src/cmd/hive/claimswire.go @@ -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) }) } diff --git a/src/cmd/hive/duplicate_fix_sweep_wiring_test.go b/src/cmd/hive/duplicate_fix_sweep_wiring_test.go new file mode 100644 index 0000000000..f48fa16c12 --- /dev/null +++ b/src/cmd/hive/duplicate_fix_sweep_wiring_test.go @@ -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") + } +} diff --git a/src/cmd/hive/github_client_config.go b/src/cmd/hive/github_client_config.go index 4c690fc89f..c510f6f488 100644 --- a/src/cmd/hive/github_client_config.go +++ b/src/cmd/hive/github_client_config.go @@ -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) { @@ -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) diff --git a/src/cmd/hive/github_client_config_test.go b/src/cmd/hive/github_client_config_test.go index 381e2179dc..4a4944a3e9 100644 --- a/src/cmd/hive/github_client_config_test.go +++ b/src/cmd/hive/github_client_config_test.go @@ -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 ( diff --git a/src/cmd/hive/main.go b/src/cmd/hive/main.go index de3a7379cd..d858649305 100644 --- a/src/cmd/hive/main.go +++ b/src/cmd/hive/main.go @@ -1525,6 +1525,18 @@ func (b *boot) wireBootClosures() { 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 { @@ -2670,6 +2682,7 @@ func (b *boot) bootStateWith(deps bootStateDeps) { 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) @@ -5807,6 +5820,7 @@ func (b *boot) bootHeartbeatWith(deps bootHeartbeatDeps) { } 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) @@ -9152,6 +9166,12 @@ const taskListSweepInterval = 15 * time.Minute // 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 @@ -9535,6 +9555,47 @@ func runSupersessionSweepIfDue(ctx context.Context, ghClient *github.Client, cfg }) } +// 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:, 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 diff --git a/src/cmd/hive/run_loop_deps.go b/src/cmd/hive/run_loop_deps.go index 852351135f..cc60ee6d5e 100644 --- a/src/cmd/hive/run_loop_deps.go +++ b/src/cmd/hive/run_loop_deps.go @@ -93,6 +93,7 @@ func defaultRunLoopDeps() runLoopDeps { } 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) } diff --git a/src/docs/github-write-surface.md b/src/docs/github-write-surface.md index 5d0d7c9a6a..858eceaf28 100644 --- a/src/docs/github-write-surface.md +++ b/src/docs/github-write-surface.md @@ -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-` 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/` and bare `` 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 | diff --git a/src/docs/operator-reference.md b/src/docs/operator-reference.md index 7e5753bcd1..2b0a8041f8 100644 --- a/src/docs/operator-reference.md +++ b/src/docs/operator-reference.md @@ -739,6 +739,54 @@ someone re-runs it. The shared-CI rerun sweep |---|---|---| | `shared_ci_rerun.enabled` | `true` (unset) | Set `false` to stop re-running PRs when a `[shared-ci]` incident closes. | +## Open-PR dispatch guard and duplicate-fix sweep (`scanner`) + +An issue whose fix is already open must not be dispatched again +([#14799](https://github.com/hivecommons/hive/issues/14799)). The claim +ledger the admission gates read is refreshed by the eval cycle, so a PR a +person opened by hand between scans was invisible to the `/contribute` relay +lane and the scanner, and the issue was handed out a second time; the two +same-issue PRs then landed at `stage/085-needs-decision` for the owner to pick +between. Two mechanisms close that gap +(`src/pkg/github/open_pr_links.go`, `src/pkg/github/pr_duplicate_fix_sweep.go`): + +- **Pre-dispatch guard.** Before any automated producer claims an issue — + the central `IssueClaimBlockReason` gate, the ledger admission check, the + issue-request watcher and the `/contribute` relay lane — the hive lists + the repo's open PRs (once per repo per sweep, cached for a minute) and + parses each title/body with the claim ledger's closing-keyword grammar + (`Fixes|Closes|Resolves #N`; a `Refs #N` is not a fix). When an open PR + links the issue, the claim is skipped, the issue is labelled + `has-open-pr` (created on first use; never re-added), the refusal is + logged as `claim skipped: open PR already linked` with the PR number, URL + and author, and the tracker's withheld list shows *An open pull request + already links this issue (claim skipped)* with the PR as evidence + (reason `open_pr_linked`). A failed listing fails **open**: the + ledger-backed guards still apply, and a flaky listing must not stall every + claim. +- **Duplicate-fix sweep.** Every ~15 minutes (an unstick step, alongside the + supersession sweep) the hive looks for an open issue two or more open PRs + close and resolves the pair deterministically. The **earlier-created PR + wins** while its checks are green or pending: the later hive-authored PR + receives one comment — `Superseded by # (earlier fix for #). + Closing as duplicate.` — is labelled `hive/superseded` and closed, with the + reason `duplicate-of:` in the log and the `duplicate-fix-closed` + audit entry. A later PR a person authored (`author/human`) is never + closed: it gets the comment, with a request to close it or say why both + should land, and stays open at its current stage + (`duplicate-fix-commented`). Only when the earlier PR is **red or stale** + (no update for 24h) **and** the later PR is green does the hive ask the + owner: the later PR is parked `needs-human` → `stage/085-needs-decision` + with a comment naming both PRs and the check states + (`duplicate-fix-escalated`). A closed issue is left to the supersession + sweep, held / `do-not-merge` PRs are skipped, a PR already carrying the + sweep's `` marker is never written to again, + and at most 10 writes are made per pass. + +| Key | Default | Meaning | +|---|---|---| +| `scanner.skip_issues_with_open_pr` | `true` (unset) | Set `false` to turn off both the pre-dispatch guard and the duplicate-fix sweep; the claim ledger alone then decides, as before. | + ## Suspicious-activity alerts (`sentinel`) The sentinel evaluates open PRs in the watched repos — human, bot, or agent — diff --git a/src/docs/running-at-level-6.md b/src/docs/running-at-level-6.md index 7ba219788d..e39d0ab164 100644 --- a/src/docs/running-at-level-6.md +++ b/src/docs/running-at-level-6.md @@ -118,7 +118,8 @@ Config keys to cross-check: `project.issue_filter.require_labels`, `project.issu | `ci-flake` | CI/Hive scripts | Flake tracking, not an L6 hold itself. | Fix/quarantine the flake while keeping required checks meaningful. | | `user-feedback` | Dashboard/hub | Feedback provenance. | Triage reporter input; apply trust/hold policy as needed. | | `dco` / `no-dco` (Prow's `dco-signoff: yes|no` coexist on Prow-gated repos; the hive never adds or removes them) | Hive stage pass (commit check) | Merge-eligibility display; `no-dco` sends the PR to `stage/072` with the rebase command; branch rules enforce the gate. | Fix commit sign-offs (`git rebase --signoff`) rather than hand-editing labels. | -| `conflicts-with-open-pr` | PR-overlap workflow | Overlap signal. | Resolve or close conflicting work before merging; same-file overlap alone may be harmless. | +| `conflicts-with-open-pr` | PR-overlap workflow | Overlap signal. | Resolve or close conflicting work before merging; same-file overlap alone may be harmless. Two open PRs that both `Fixes` one issue are resolved by the duplicate-fix sweep without a decision: the earlier green/pending PR stays, the later hive PR is closed as `duplicate-of:`, a person's later PR is commented on and left open ([#14799](https://github.com/hivecommons/hive/issues/14799)). | +| `has-open-pr` | Pre-dispatch guard | An open PR already links the issue with a closing keyword; the hive skipped the claim. | Nothing — the label is a tracker signal, not a park; see `scanner.skip_issues_with_open_pr` in [operator-reference.md](operator-reference.md#open-pr-dispatch-guard-and-duplicate-fix-sweep-scanner). | | `no-changelog` | Dependabot/maintainer | Repo changelog exception, not a Hive safety gate. | Apply only under repo policy. | | `human-only` remainder text (not a default label) | Agent/reviewer | Task-list sweep can park remaining issue work as `needs-human`. | Treat as a handoff; add PR `hold` if merging must stop. | diff --git a/src/pkg/config/config.go b/src/pkg/config/config.go index f64dcaf5dd..a0a981f81d 100644 --- a/src/pkg/config/config.go +++ b/src/pkg/config/config.go @@ -165,6 +165,9 @@ type Config struct { // `` once incident #n closes // (hivecommons/hive#12253). Default on. SharedCIRerun SharedCIRerunConfig `yaml:"shared_ci_rerun,omitempty" json:"shared_ci_rerun,omitempty"` + // Scanner tunes the pre-dispatch guards every automated claim producer + // runs before an issue is handed out (hivecommons/hive#14799). + Scanner ScannerConfig `yaml:"scanner,omitempty" json:"scanner,omitempty"` // UpstreamWatch declares per-repo upstreams and filters for the fork // upstream watch (hivecommons/hive#9966). Default off. UpstreamWatch UpstreamWatchConfig `yaml:"upstream_watch,omitempty" json:"upstream_watch,omitempty"` @@ -7659,6 +7662,24 @@ func (c SharedCIRerunConfig) IsEnabled() bool { return c.Enabled == nil || *c.Enabled } +// ScannerConfig tunes the pre-dispatch guards (hivecommons/hive#14799). +type ScannerConfig struct { + // SkipIssuesWithOpenPR refuses every automated claim on an issue that an + // OPEN pull request already links with a closing keyword (`Fixes #N`, + // `Closes #N`, `Resolves #N`), checked live against the forge rather + // than against the eval cycle's claim ledger — the ledger lagged a + // just-opened human PR by one cycle, and the issue was dispatched again + // in that window. The issue is labelled `has-open-pr` and the refusal + // is logged as `claim skipped: open PR already linked`. *bool so unset + // means on; set `scanner.skip_issues_with_open_pr: false` to disable. + SkipIssuesWithOpenPR *bool `yaml:"skip_issues_with_open_pr,omitempty" json:"skip_issues_with_open_pr,omitempty"` +} + +// IsSkipIssuesWithOpenPR reports whether the open-PR guard runs; unset means on. +func (c ScannerConfig) IsSkipIssuesWithOpenPR() bool { + return c.SkipIssuesWithOpenPR == nil || *c.SkipIssuesWithOpenPR +} + // AutoMergeConfig gates the App-self-merge sweep (SweepSelfAuthoredAutoMerges). // Prow structurally forbids self-approval on a PR's own author, so a PR the // Forge App itself opens can never collect the lgtm+approved labels tide diff --git a/src/pkg/config/scanner_config_test.go b/src/pkg/config/scanner_config_test.go new file mode 100644 index 0000000000..f74443ee25 --- /dev/null +++ b/src/pkg/config/scanner_config_test.go @@ -0,0 +1,29 @@ +package config + +import ( + "testing" + + "gopkg.in/yaml.v3" +) + +// The open-PR dispatch guard is on unless +// `scanner.skip_issues_with_open_pr: false` (hivecommons/hive#14799). +func TestScannerSkipIssuesWithOpenPRDefaultsOn(t *testing.T) { + cases := []struct { + src string + want bool + }{ + {"project:\n org: acme\n", true}, + {"scanner:\n skip_issues_with_open_pr: true\n", true}, + {"scanner:\n skip_issues_with_open_pr: false\n", false}, + } + for _, tc := range cases { + var cfg Config + if err := yaml.Unmarshal([]byte(tc.src), &cfg); err != nil { + t.Fatalf("unmarshal %q: %v", tc.src, err) + } + if got := cfg.Scanner.IsSkipIssuesWithOpenPR(); got != tc.want { + t.Errorf("%q: IsSkipIssuesWithOpenPR = %v, want %v", tc.src, got, tc.want) + } + } +} diff --git a/src/pkg/config/testdata/agent_runtime_golden.json b/src/pkg/config/testdata/agent_runtime_golden.json index 3d31e6a2aa..90672acf7b 100644 --- a/src/pkg/config/testdata/agent_runtime_golden.json +++ b/src/pkg/config/testdata/agent_runtime_golden.json @@ -702,6 +702,7 @@ "good_first_issue": {}, "supersession_sweep": {}, "shared_ci_rerun": {}, + "scanner": {}, "upstream_watch": {}, "agent_sandbox": { "enabled": true, diff --git a/src/pkg/dashboard/contribute_admission.go b/src/pkg/dashboard/contribute_admission.go index 70878fcd6c..be1c1010a9 100644 --- a/src/pkg/dashboard/contribute_admission.go +++ b/src/pkg/dashboard/contribute_admission.go @@ -99,6 +99,9 @@ type contributorAdmissionDecision struct { admitted bool reason string claim ghpkg.IssueClaim + // openPR carries the live open PR behind an open_pr_linked refusal + // (hivecommons/hive#14799). Zero for every other reason. + openPR ghpkg.OpenPRLink // churn carries the pull-request history behind an issue_churn refusal // (#7995): which PRs merged, which closed unmerged. Zero-valued for every // other reason. @@ -118,6 +121,12 @@ type contributorAdmissionDecision struct { const ( contributorAdmissionReasonOpenPRClaim = "open_pr_claim" + // contributorAdmissionReasonOpenPRLinked: the forge says an OPEN pull + // request already links this issue with a closing keyword, checked live + // rather than through the eval cycle's ledger (hivecommons/hive#14799). + // It is what open_pr_claim becomes once the ledger catches up; the + // distinct code tells an operator the guard, not the ledger, caught it. + contributorAdmissionReasonOpenPRLinked = "open_pr_linked" // contributorAdmissionReasonIssueClaim: someone has claimed the issue on // the issue itself — a `hive:claim` marker comment or an assignee — and // the claim has not expired (hivecommons/hive#8380). It covers the window @@ -193,6 +202,16 @@ func (h *ContributeWSHub) evaluateContributorNeutralAdmission(sweep *contributor } } + // Live open-PR link (#14799): the ledger above is one eval cycle old; + // a PR opened since is only visible on the forge. One listing per repo + // per sweep, cached by the client. + if link, ok := h.openPRLinkingIssue(candidate); ok { + return contributorAdmissionDecision{ + reason: contributorAdmissionReasonOpenPRLinked, + openPR: link, + } + } + // Issue claim (#8380): checked after the open-PR claim — a PR is the // stronger, later signal and subsumes the claim — and before churn, which // is a property of the issue's history rather than of who is on it now. @@ -290,6 +309,26 @@ func (h *ContributeWSHub) issueClaimedByOpenPR(repoFull, repoName string, number return ghpkg.IssueClaim{}, false } +// openPRLinkingIssue asks the Dependencies.OpenPRLinkingIssue hook whether an +// open PR already links the candidate (hivecommons/hive#14799). Repo +// spellings are tried in the same order and for the same reason as +// issueClaimedByOpenPR. No hook wired (tests, or a hive booted without +// GitHub credentials) reports no link and selection proceeds as before. +func (h *ContributeWSHub) openPRLinkingIssue(candidate contributorAdmissionCandidate) (ghpkg.OpenPRLink, bool) { + if h == nil || h.server == nil || h.server.deps == nil || h.server.deps.OpenPRLinkingIssue == nil { + return ghpkg.OpenPRLink{}, false + } + if link, ok := h.server.deps.OpenPRLinkingIssue(candidate.repoFull, candidate.number, candidate.labels); ok { + return link, true + } + if candidate.repoName != "" && candidate.repoName != candidate.repoFull { + if link, ok := h.server.deps.OpenPRLinkingIssue(candidate.repoName, candidate.number, candidate.labels); ok { + return link, true + } + } + return ghpkg.OpenPRLink{}, false +} + // churnTriagedMarker is the label a maintainer applies to say "I have looked // at the churn on this issue; it is still ordinary work". It clears the churn // guard for that issue and nothing else — the guard's whole purpose is to ask diff --git a/src/pkg/dashboard/contribute_select.go b/src/pkg/dashboard/contribute_select.go index d795462d65..dbabd81741 100644 --- a/src/pkg/dashboard/contribute_select.go +++ b/src/pkg/dashboard/contribute_select.go @@ -896,6 +896,11 @@ func (h *ContributeWSHub) selectTaskPass(c *ContributorConnection, skippedUnmint "pr_url", decision.claim.PRURL, "pr_author", decision.claim.PRAuthor, "merged", decision.claim.MergedPR, "source", decision.claim.Source, "source_reporter", decision.claim.SourceReporter) + case contributorAdmissionReasonOpenPRLinked: + // #14799: the forge, not the ledger, says the fix is open. + h.logger.Info("[contribute-ws] claim skipped: open PR already linked", + "repo", repo.Full, "number", number, + "pr", decision.openPR.PRNumber, "pr_url", decision.openPR.PRURL, "pr_author", decision.openPR.PRAuthor) case contributorAdmissionReasonIssueClaim: // #8380: someone claimed the issue on the issue itself; the // claim expires on its own, so the log names when. diff --git a/src/pkg/dashboard/contribute_withheld.go b/src/pkg/dashboard/contribute_withheld.go index 93673288c6..b85f78021d 100644 --- a/src/pkg/dashboard/contribute_withheld.go +++ b/src/pkg/dashboard/contribute_withheld.go @@ -72,6 +72,7 @@ const ( // rule applies to the WORDS as much as to the decisions). var withheldReasonLabels = map[string]string{ contributorAdmissionReasonOpenPRClaim: "An open pull request already claims this issue", + contributorAdmissionReasonOpenPRLinked: "An open pull request already links this issue (claim skipped)", contributorAdmissionReasonIssueClaim: "Someone has claimed this issue; the claim expires on its own", contributorAdmissionReasonMergedClaimStale: "Fixed by a merged pull request; the issue is still open — close it or say what remains", contributorAdmissionReasonIssueChurn: "Too many pull requests on one issue — needs maintainer triage", @@ -110,6 +111,7 @@ func withheldReasonLabel(reason string) string { func isConvergenceWithheldReason(reason string) bool { switch reason { case contributorAdmissionReasonOpenPRClaim, + contributorAdmissionReasonOpenPRLinked, contributorAdmissionReasonIssueClaim, contributorAdmissionReasonMergedClaimStale, contributorAdmissionReasonIssueChurn, @@ -331,6 +333,14 @@ func withheldFromAdmissionDecision(c withheldCandidate, d contributorAdmissionDe item.Detail = fmt.Sprintf("Existing pull request #%d", d.claim.PRNumber) } return item + case contributorAdmissionReasonOpenPRLinked: + item := newWithheldItem(c, d.reason) + item.ClaimURL = d.openPR.PRURL + item.ClaimAuthor = d.openPR.PRAuthor + if d.openPR.PRNumber > 0 { + item.Detail = fmt.Sprintf("Claim skipped: open pull request #%d already links this issue", d.openPR.PRNumber) + } + return item case contributorAdmissionReasonMergedClaimStale: item := newWithheldItem(c, d.reason) item.ClaimURL = d.claim.PRURL diff --git a/src/pkg/dashboard/contribute_withheld_test.go b/src/pkg/dashboard/contribute_withheld_test.go index 269dea812f..744d6118b4 100644 --- a/src/pkg/dashboard/contribute_withheld_test.go +++ b/src/pkg/dashboard/contribute_withheld_test.go @@ -207,6 +207,38 @@ func withheldCases() []withheldCase { } }, }, + { + // hivecommons/hive#14799: the ledger is one eval cycle old; the + // live guard sees a PR opened by hand minutes ago and the lane + // skips the claim with the PR as evidence. + name: "open PR linked (live guard)", + reason: contributorAdmissionReasonOpenPRLinked, + arrange: func(t *testing.T, hub *ContributeWSHub, s *Server) { + s.deps.IssueClaimed = func(string, int) (ghpkg.IssueClaim, bool) { return ghpkg.IssueClaim{}, false } + s.deps.OpenPRLinkingIssue = func(repo string, number int, labels []string) (ghpkg.OpenPRLink, bool) { + if number == 601 { + return ghpkg.OpenPRLink{ + Issue: 601, + PRNumber: 14775, + PRURL: "https://github.com/projectbluefin/dakota/pull/14775", + PRAuthor: "clubanderson", + }, true + } + return ghpkg.OpenPRLink{}, false + } + }, + evidence: func(t *testing.T, item AdmissionWithheldItem) { + if item.ClaimURL != "https://github.com/projectbluefin/dakota/pull/14775" { + t.Errorf("claim_url = %q, want the linking PR's URL", item.ClaimURL) + } + if item.ClaimAuthor != "clubanderson" { + t.Errorf("claim_author = %q, want clubanderson", item.ClaimAuthor) + } + if !strings.Contains(item.Detail, "#14775") { + t.Errorf("detail = %q, want the linking PR number", item.Detail) + } + }, + }, { // #8003: chairlift#55 — fixed by merged #133 four days ago, still // open, re-offered every 72h. Withheld under its own reason that @@ -520,6 +552,7 @@ func TestWithheld_QueueEndpointOptIn(t *testing.T) { func TestWithheld_EveryReasonHasALabel(t *testing.T) { reasons := []string{ contributorAdmissionReasonOpenPRClaim, + contributorAdmissionReasonOpenPRLinked, contributorAdmissionReasonWorkflowBlocked, contributorAdmissionReasonLabelSkipped, contributorAdmissionReasonDependencyBlocked, diff --git a/src/pkg/dashboard/deps.go b/src/pkg/dashboard/deps.go index c8babd77b8..eafe570706 100644 --- a/src/pkg/dashboard/deps.go +++ b/src/pkg/dashboard/deps.go @@ -182,6 +182,17 @@ type Dependencies struct { // repo is tried in the same spelling the ledger keys on (the config repo // form); a nil func means "no claim data" and disables the check. IssueClaimed func(repo string, number int) (ghpkg.IssueClaim, bool) + // OpenPRLinkingIssue is the live companion to IssueClaimed + // (hivecommons/hive#14799): it asks the forge — one cached open-PR + // listing per repo per sweep — whether an OPEN pull request already + // links the issue with a closing keyword, labels the issue + // `has-open-pr` and logs the refusal. The ledger IssueClaimed reads is + // refreshed by the eval cycle, so a PR a person opened by hand stayed + // invisible to the contribute lane until the next scan and the issue was + // dispatched again in that window. labels are the issue's current labels + // so the label write is idempotent. Nil disables the live check; the + // client honours the scanner open-PR switch itself. + OpenPRLinkingIssue func(repo string, number int, labels []string) (ghpkg.OpenPRLink, bool) // Watsonx / OpenRouter are the LLM-provider gateway views (#5565 slice 3): // narrow consumer-defined interfaces over pkg/watsonx and pkg/openrouter, diff --git a/src/pkg/dashboard/settings_parity_allowlist.txt b/src/pkg/dashboard/settings_parity_allowlist.txt index c742958fcd..5910aa3917 100644 --- a/src/pkg/dashboard/settings_parity_allowlist.txt +++ b/src/pkg/dashboard/settings_parity_allowlist.txt @@ -449,6 +449,7 @@ forgestate.enabled # yaml-only until readers migrate (#13373) forgestate.freshness_bound_s # yaml-only until readers migrate (#13373) forgestate.reconcile_interval_s # yaml-only until readers migrate (#13373) forgestate.search_budget # yaml-only until readers migrate (#13373) +scanner.skip_issues_with_open_pr # open-PR dispatch guard (#14799); yaml-only self_experiments.scheduler.enabled # TODO #11854 self_experiments.scheduler.interval_s # TODO #11854 self_experiments.scheduler.mode # TODO #11854 diff --git a/src/pkg/github/client.go b/src/pkg/github/client.go index a125ad2993..3dfa60a473 100644 --- a/src/pkg/github/client.go +++ b/src/pkg/github/client.go @@ -96,8 +96,12 @@ type Client struct { // the config (nil = package default; see SetNeverClaimLabels). neverClaimMu sync.RWMutex neverClaimLabels []string - sentinelMu sync.RWMutex - sentinelLabel string + // openPRLinkState backs the pre-dispatch open-PR guard and the + // duplicate-fix resolver (open_pr_links.go); built lazily. + openPRLinkOnce sync.Once + openPRLinkState *openPRLinkCache + sentinelMu sync.RWMutex + sentinelLabel string // issueFilter is the operator's project.issue_filter (require_labels // allow-list) gating which issues become actionable at all. The exclude // polarity is NOT here — it is exemptLabels above (governor.labels.exempt, diff --git a/src/pkg/github/issue_claim_gate.go b/src/pkg/github/issue_claim_gate.go index 1a642b949f..43aab561c0 100644 --- a/src/pkg/github/issue_claim_gate.go +++ b/src/pkg/github/issue_claim_gate.go @@ -109,34 +109,64 @@ func (c *Client) issueClaimParkLabel(labels []string) string { // default `epic`): an umbrella issue is the list of work, not the work. // // Lookup errors are returned so callers cannot mistake unknown labels for -// permission to claim. Human claims do not use this gate. +// permission to claim. Human claims do not use this gate. A producer about +// to start fresh work uses IssueDispatchBlockReason, which adds the open-PR +// guard (hivecommons/hive#14799). func (c *Client) IssueClaimBlockReason(ctx context.Context, repo string, number int) (string, error) { + reason, _, err := c.issueClaimGate(ctx, repo, number) + return reason, err +} + +// issueClaimGate reads the issue once and applies every label-based refusal. +// labels are returned for the dispatch gate's open-PR step. +func (c *Client) issueClaimGate(ctx context.Context, repo string, number int) (reason string, labels []string, err error) { if c == nil || c.client == nil { - return "", ErrNoGitHubClient + return "", nil, ErrNoGitHubClient } owner, name := c.splitRepo(repo) issue, _, err := c.client.Issues.Get(ctx, owner, name, number) if err != nil { - return "", fmt.Errorf("checking issue claim labels: %w", err) + return "", nil, fmt.Errorf("checking issue claim labels: %w", err) } if issue.IsPullRequest() { - return ClaimBlockReasonPullRequest, nil + return ClaimBlockReasonPullRequest, nil, nil } - labels := extractLabels(issue.Labels) + labels = extractLabels(issue.Labels) if hasIssueNeedsHumanLabel(labels) { - return "needs-human", nil + return "needs-human", labels, nil } if c.isHeld(labels) { - return "hold", nil + return "hold", labels, nil } if hasBlockedLabel(labels) { - return blockedClaimReason(issue.GetBody()), nil + return blockedClaimReason(issue.GetBody()), labels, nil } if label := c.issueClaimParkLabel(labels); label != "" { - return ClaimBlockReasonParkedPrefix + label, nil + return ClaimBlockReasonParkedPrefix + label, labels, nil } if label := claims.NeverClaimLabel(labels, c.neverClaimLabelSet()); label != "" { - return ClaimBlockReasonNeverClaimPrefix + label, nil + return ClaimBlockReasonNeverClaimPrefix + label, labels, nil + } + return "", labels, nil +} + +// IssueDispatchBlockReason is IssueClaimBlockReason for a producer about to +// START work on an issue — a fresh claim with no PR behind it — plus the +// open-PR guard (hivecommons/hive#14799): an OPEN pull request already +// linking the issue with a closing keyword refuses the dispatch +// (ClaimBlockReasonOpenPRPrefix), labels the issue `has-open-pr` and logs +// the skip. Renewals, PR-backed start signals and the release tick keep +// using IssueClaimBlockReason: the agent whose own PR links the issue holds +// its claim legitimately, and a guard that released it would un-stage the +// very PR it exists to protect. Off with scanner.skip_issues_with_open_pr: +// false; a failed listing fails open with a warning. +func (c *Client) IssueDispatchBlockReason(ctx context.Context, repo string, number int) (string, error) { + reason, labels, err := c.issueClaimGate(ctx, repo, number) + if err != nil || reason != "" { + return reason, err } - return "", nil + // Last because it is the only step that costs a listing. The eval + // cycle's claim ledger learns of a hand-opened PR one scan late; this + // reads the forge now. + return c.openPRLinkBlockReason(ctx, repo, number, labels), nil } diff --git a/src/pkg/github/issue_request_watcher.go b/src/pkg/github/issue_request_watcher.go index 0d8d781d03..ba80d4a3cc 100644 --- a/src/pkg/github/issue_request_watcher.go +++ b/src/pkg/github/issue_request_watcher.go @@ -576,7 +576,7 @@ func (c *Client) handleOneIssueRequest(ctx context.Context, path string, nowFn f switch kind { case "claim": var reason string - reason, err = c.IssueClaimBlockReason(ctx, req.Repo, req.Number) + reason, err = c.IssueDispatchBlockReason(ctx, req.Repo, req.Number) if err != nil { break // Retry a failed lookup; never claim on unknown labels. } diff --git a/src/pkg/github/open_pr_links.go b/src/pkg/github/open_pr_links.go new file mode 100644 index 0000000000..e61f20e21d --- /dev/null +++ b/src/pkg/github/open_pr_links.go @@ -0,0 +1,278 @@ +package github + +import ( + "context" + "fmt" + "log/slog" + "sort" + "strings" + "sync" + "time" + + gh "github.com/google/go-github/v72/github" +) + +// Pre-dispatch guard against re-dispatching an issue whose fix is already +// open (hivecommons/hive#14799). Every automated claim producer runs +// IssueClaimBlockReason first; that gate used to see only the eval cycle's +// claim ledger, which lags a PR a person opened by hand until the next scan. +// #14766 and #14763 were each handed to a relay contributor six minutes +// after their fix PR opened, and the resulting pair of same-issue PRs ended +// up at stage/085 for the owner to pick between. The index here answers the +// same question live — one open-PR listing per repo per sweep, parsed with +// the claim ledger's own closing-keyword grammar — so the guard sees the PR +// the moment it exists. + +const ( + // HasOpenPRLabel marks an issue the guard refused to dispatch because an + // open pull request already links it with a closing keyword. It is a + // signal for the tracker, not a park: the label comes off with nothing + // — the stage engine re-derives the issue's stage from the PR itself. + HasOpenPRLabel = "has-open-pr" + hasOpenPRLabelColor = "0e8a16" + hasOpenPRLabelDescription = "An open pull request already links this issue with Fixes/Closes/Resolves; the hive will not dispatch it again" + + // ClaimBlockReasonOpenPRPrefix prefixes the IssueClaimBlockReason for an + // issue an open PR already links: "open PR already linked: #14775". + ClaimBlockReasonOpenPRPrefix = "open PR already linked: #" + + // openPRLinkCacheTTL bounds how long one repo's open-PR listing answers + // the guard. A sweep (contributor selectTask pass, eval-cycle claim + // burst) finishes well inside it, so the listing is paid once per sweep; + // a PR opened after the listing is seen on the next one. + openPRLinkCacheTTL = time.Minute +) + +// OpenPRLink is one open pull request that links an issue with a closing +// keyword, as the guard found it. +type OpenPRLink struct { + // Repo is the issue's repository in the configured spelling. + Repo string + // Issue is the linked issue number. + Issue int + // PRRepo / PRNumber / PRURL / PRAuthor identify the linking PR. + PRRepo string + PRNumber int + PRURL string + PRAuthor string + // CreatedAt is when the PR was opened; with several open PRs linking + // one issue the earliest is reported. + CreatedAt time.Time +} + +// openPRLinkIndex is one repo's open PRs indexed by the issue they close. +type openPRLinkIndex struct { + fetchedAt time.Time + // byIssue maps claimKey(issueRepo, issue) to every open PR closing it, + // sorted by creation time (earliest first). + byIssue map[string][]OpenPRLink + // prs is the raw listing, kept for the duplicate-fix resolver so it does + // not list the repo a second time in the same sweep. + prs []*gh.PullRequest +} + +// openPRLinkCache is the per-client, per-repo cache behind the guard. +type openPRLinkCache struct { + mu sync.Mutex + repos map[string]*openPRLinkIndex + // disabled is scanner.skip_issues_with_open_pr: false. + disabled bool + now func() time.Time +} + +func (c *Client) openPRLinks() *openPRLinkCache { + c.openPRLinkOnce.Do(func() { + c.openPRLinkState = &openPRLinkCache{repos: map[string]*openPRLinkIndex{}, now: time.Now} + }) + return c.openPRLinkState +} + +// SetSkipIssuesWithOpenPR installs scanner.skip_issues_with_open_pr. The +// guard is on until told otherwise. +func (c *Client) SetSkipIssuesWithOpenPR(enabled bool) { + if c == nil { + return + } + cache := c.openPRLinks() + cache.mu.Lock() + defer cache.mu.Unlock() + cache.disabled = !enabled +} + +// SkipIssuesWithOpenPR reports whether the open-PR guard is on. +func (c *Client) SkipIssuesWithOpenPR() bool { + if c == nil { + return false + } + cache := c.openPRLinks() + cache.mu.Lock() + defer cache.mu.Unlock() + return !cache.disabled +} + +// InvalidateOpenPRLinks drops the cached listing for repo (every repo when +// repo is ""), so the next guard call lists again. The PR-opened watcher +// path calls it when the hive itself opens a PR. +func (c *Client) InvalidateOpenPRLinks(repo string) { + if c == nil { + return + } + cache := c.openPRLinks() + cache.mu.Lock() + defer cache.mu.Unlock() + if repo == "" { + cache.repos = map[string]*openPRLinkIndex{} + return + } + delete(cache.repos, c.canonicalRepo(repo)) +} + +// canonicalRepo maps repo onto the configured spelling, so a caller naming +// the repo bare or org-qualified shares one cache entry and one claim key. +func (c *Client) canonicalRepo(repo string) string { + return canonicalConfigRepo(repo, c.getRepos()) +} + +// openPRIndexFor returns the (possibly cached) open-PR link index for repo. +func (c *Client) openPRIndexFor(ctx context.Context, repo string) (*openPRLinkIndex, error) { + repo = c.canonicalRepo(repo) + cache := c.openPRLinks() + cache.mu.Lock() + now := cache.now() + if idx, ok := cache.repos[repo]; ok && now.Sub(idx.fetchedAt) < openPRLinkCacheTTL { + cache.mu.Unlock() + return idx, nil + } + cache.mu.Unlock() + + owner, repoName := c.splitRepo(repo) + prs, err := c.listOpenPRsForSupersessionSweep(WithRESTCaller(ctx, "hive:open_pr_links"), owner, repoName) + if err != nil { + return nil, err + } + identity := c.getHiveIdentity() + if identity.AppLogin == "" { + identity.AppLogin = c.appBotLogin + } + idx := buildOpenPRLinkIndex(prs, repo, identity, now) + cache.mu.Lock() + cache.repos[repo] = idx + cache.mu.Unlock() + return idx, nil +} + +// buildOpenPRLinkIndex indexes prs by the issues their title/body close. +// Only closing-keyword claims count: a `Refs #N` reference or a branch-name +// heuristic is work-in-the-area, not "this PR is the fix". +func buildOpenPRLinkIndex(prs []*gh.PullRequest, repo string, identity HiveIdentity, now time.Time) *openPRLinkIndex { + idx := &openPRLinkIndex{fetchedAt: now, byIssue: map[string][]OpenPRLink{}, prs: prs} + for _, pr := range prs { + if pr == nil || pr.GetNumber() <= 0 || (pr.GetState() != "" && !strings.EqualFold(pr.GetState(), PRStateOpen)) { + continue + } + for _, claim := range claimsFromPR(pr, repo, identity, now) { + if !claim.Closing { + continue + } + key := claimKey(claim.Repo, claim.Issue) + idx.byIssue[key] = append(idx.byIssue[key], OpenPRLink{ + Repo: claim.Repo, + Issue: claim.Issue, + PRRepo: repo, + PRNumber: pr.GetNumber(), + PRURL: pr.GetHTMLURL(), + PRAuthor: claim.PRAuthor, + CreatedAt: pr.GetCreatedAt().Time, + }) + } + } + for key := range idx.byIssue { + links := idx.byIssue[key] + sort.SliceStable(links, func(i, j int) bool { + if !links[i].CreatedAt.Equal(links[j].CreatedAt) { + return links[i].CreatedAt.Before(links[j].CreatedAt) + } + return links[i].PRNumber < links[j].PRNumber + }) + } + return idx +} + +// OpenPRLinkingIssue reports the earliest open pull request in repo that +// links issue number with a closing keyword. ok is false when none does. +// A listing failure is returned so the caller decides how to fail. +func (c *Client) OpenPRLinkingIssue(ctx context.Context, repo string, number int) (OpenPRLink, bool, error) { + if c == nil || c.client == nil { + return OpenPRLink{}, false, ErrNoGitHubClient + } + idx, err := c.openPRIndexFor(ctx, repo) + if err != nil { + return OpenPRLink{}, false, fmt.Errorf("listing open PRs linking %s#%d: %w", repo, number, err) + } + links := idx.byIssue[claimKey(c.canonicalRepo(repo), number)] + if len(links) == 0 { + return OpenPRLink{}, false, nil + } + return links[0], true, nil +} + +// OpenPRLinkGuard is the pre-dispatch guard every automated claim producer +// runs: it reports the earliest open PR already linking the issue, labels +// the issue HasOpenPRLabel when it does not carry it yet (label created on +// first use) and logs `claim skipped: open PR already linked`. ok is false +// when the guard is off (scanner.skip_issues_with_open_pr: false), no open +// PR links the issue, or the listing failed — a failure fails OPEN with a +// warning: the ledger-backed guards downstream still stand, and a flaky +// listing must not stall every claim in the hive. labels are the issue's +// current labels as the caller read them (nil when unknown). +func (c *Client) OpenPRLinkGuard(ctx context.Context, repo string, number int, labels []string) (OpenPRLink, bool) { + if c == nil || !c.SkipIssuesWithOpenPR() { + return OpenPRLink{}, false + } + link, ok, err := c.OpenPRLinkingIssue(ctx, repo, number) + if err != nil { + if c.logger != nil { + c.logger.Warn("open-PR guard: could not list open PRs, admitting the claim on ledger evidence alone", + slog.String("repo", repo), slog.Int("issue", number), slog.String("error", err.Error())) + } + return OpenPRLink{}, false + } + if !ok { + return OpenPRLink{}, false + } + if c.logger != nil { + c.logger.Info("claim skipped: open PR already linked", + slog.String("repo", repo), slog.Int("issue", number), + slog.Int("pr", link.PRNumber), slog.String("pr_url", link.PRURL), slog.String("pr_author", link.PRAuthor)) + } + c.markIssueHasOpenPR(ctx, repo, number, labels, link) + return link, true +} + +// openPRLinkBlockReason is the IssueClaimBlockReason step for the guard: +// the refusal reason for an issue an open PR already links, "" otherwise. +func (c *Client) openPRLinkBlockReason(ctx context.Context, repo string, number int, labels []string) string { + link, ok := c.OpenPRLinkGuard(ctx, repo, number, labels) + if !ok { + return "" + } + return fmt.Sprintf("%s%d", ClaimBlockReasonOpenPRPrefix, link.PRNumber) +} + +// markIssueHasOpenPR applies HasOpenPRLabel once. Idempotent on the labels +// the caller just read; a label failure is logged, never fatal — the claim +// is refused either way. +func (c *Client) markIssueHasOpenPR(ctx context.Context, repo string, number int, labels []string, link OpenPRLink) { + for _, l := range labels { + if strings.EqualFold(strings.TrimSpace(l), HasOpenPRLabel) { + return + } + } + if err := c.EnsureIssueLabel(ctx, repo, HasOpenPRLabel, hasOpenPRLabelColor, hasOpenPRLabelDescription); err != nil && c.logger != nil { + c.logger.Warn("open-PR guard: could not ensure label", slog.String("label", HasOpenPRLabel), slog.String("repo", repo), slog.String("error", err.Error())) + } + if err := c.AddLabels(ctx, repo, number, []string{HasOpenPRLabel}); err != nil && c.logger != nil { + c.logger.Warn("open-PR guard: could not label issue", slog.String("label", HasOpenPRLabel), + slog.String("repo", repo), slog.Int("issue", number), slog.Int("pr", link.PRNumber), slog.String("error", err.Error())) + } +} diff --git a/src/pkg/github/open_pr_links_test.go b/src/pkg/github/open_pr_links_test.go new file mode 100644 index 0000000000..eaa7304cfe --- /dev/null +++ b/src/pkg/github/open_pr_links_test.go @@ -0,0 +1,247 @@ +package github + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + "time" +) + +// openPRGuardServer serves one repo with the given open PRs and issue #42 +// carrying issueLabels, recording label writes and refusing anything else. +type openPRGuardServer struct { + mu sync.Mutex + pullsCalls int + addedLabels []string + createdLbl []string + failPulls bool +} + +func newOpenPRGuardServer(t *testing.T, prs []map[string]any, issueLabels []string) (*httptest.Server, *openPRGuardServer) { + t.Helper() + obs := &openPRGuardServer{} + labels := make([]map[string]any, 0, len(issueLabels)) + for _, l := range issueLabels { + labels = append(labels, map[string]any{"name": l}) + } + mux := http.NewServeMux() + mux.HandleFunc("/repos/o/r/pulls", func(w http.ResponseWriter, r *http.Request) { + obs.mu.Lock() + obs.pullsCalls++ + fail := obs.failPulls + obs.mu.Unlock() + if fail { + w.WriteHeader(http.StatusBadGateway) + return + } + _ = json.NewEncoder(w).Encode(prs) + }) + mux.HandleFunc("/repos/o/r/issues/42", func(w http.ResponseWriter, r *http.Request) { + _ = json.NewEncoder(w).Encode(map[string]any{"number": 42, "state": "open", "labels": labels}) + }) + mux.HandleFunc("/repos/o/r/issues/42/labels", func(w http.ResponseWriter, r *http.Request) { + var names []string + _ = json.NewDecoder(r.Body).Decode(&names) + obs.mu.Lock() + obs.addedLabels = append(obs.addedLabels, names...) + obs.mu.Unlock() + _, _ = io.WriteString(w, `[]`) + }) + mux.HandleFunc("/repos/o/r/labels/", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + }) + mux.HandleFunc("/repos/o/r/labels", func(w http.ResponseWriter, r *http.Request) { + var body struct { + Name string `json:"name"` + } + _ = json.NewDecoder(r.Body).Decode(&body) + obs.mu.Lock() + obs.createdLbl = append(obs.createdLbl, body.Name) + obs.mu.Unlock() + _, _ = io.WriteString(w, `{"name":"`+body.Name+`"}`) + }) + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + w.WriteHeader(http.StatusNotFound) + }) + return httptest.NewServer(mux), obs +} + +func openPRFixture(number int, body, author string, createdAt time.Time) map[string]any { + return map[string]any{ + "number": number, + "state": "open", + "title": fmt.Sprintf("fix %d", number), + "body": body, + "html_url": fmt.Sprintf("https://github.com/o/r/pull/%d", number), + "created_at": createdAt.UTC().Format(time.RFC3339), + "updated_at": createdAt.UTC().Format(time.RFC3339), + "user": map[string]any{"login": author, "type": "User"}, + "head": map[string]any{"sha": fmt.Sprintf("sha%d", number)}, + } +} + +func TestOpenPRGuard_LinkedIssueIsRefusedAndLabelled(t *testing.T) { + t0 := time.Date(2026, 10, 27, 1, 0, 0, 0, time.UTC) + srv, obs := newOpenPRGuardServer(t, []map[string]any{ + openPRFixture(14775, "Fixes #42", "clubanderson", t0), + openPRFixture(14780, "Fixes #42", "thatguywob", t0.Add(9*time.Minute)), + openPRFixture(14790, "Refs #42", "someone", t0.Add(-time.Hour)), + }, nil) + defer srv.Close() + c := newTestClient(t, srv, "o", []string{"r"}) + + got, err := c.IssueDispatchBlockReason(context.Background(), "o/r", 42) + if err != nil { + t.Fatalf("IssueClaimBlockReason: %v", err) + } + if want := ClaimBlockReasonOpenPRPrefix + "14775"; got != want { + t.Fatalf("reason = %q, want %q (earliest closing PR wins; Refs #42 is not a fix)", got, want) + } + if len(obs.addedLabels) != 1 || obs.addedLabels[0] != HasOpenPRLabel { + t.Fatalf("labels added = %v, want [%s]", obs.addedLabels, HasOpenPRLabel) + } + if len(obs.createdLbl) != 1 || obs.createdLbl[0] != HasOpenPRLabel { + t.Fatalf("labels created = %v, want the guard label created on first use", obs.createdLbl) + } + + // Second call inside the TTL: no second listing, and the label is not + // re-added when the issue already carries it. + link, ok := c.OpenPRLinkGuard(context.Background(), "o/r", 42, []string{HasOpenPRLabel}) + if !ok || link.PRNumber != 14775 || link.PRAuthor != "clubanderson" { + t.Fatalf("guard = %+v, %v; want PR 14775 by clubanderson", link, ok) + } + if obs.pullsCalls != 1 { + t.Fatalf("open-PR listings = %d, want 1 (cached per sweep)", obs.pullsCalls) + } + if len(obs.addedLabels) != 1 { + t.Fatalf("labels added = %v, want no second write when the label is present", obs.addedLabels) + } +} + +// The plain claim gate — renewals, PR-backed start signals, the release +// tick — never lists PRs: the holder's own PR is the open PR. +func TestOpenPRGuard_ClaimGateDoesNotListPRs(t *testing.T) { + srv, obs := newOpenPRGuardServer(t, []map[string]any{ + openPRFixture(7, "Fixes #42", "hive-app[bot]", time.Now()), + }, nil) + defer srv.Close() + c := newTestClient(t, srv, "o", []string{"r"}) + got, err := c.IssueClaimBlockReason(context.Background(), "o/r", 42) + if err != nil || got != "" { + t.Fatalf("reason = %q, %v; want admitted", got, err) + } + if obs.pullsCalls != 0 { + t.Fatalf("open-PR listings = %d, want 0 from the claim gate", obs.pullsCalls) + } + var nilClient *Client + if _, err := nilClient.IssueDispatchBlockReason(context.Background(), "o/r", 42); err == nil { + t.Fatal("nil client must error") + } +} + +func TestOpenPRGuard_DispatchGateKeepsLabelRefusalsFirst(t *testing.T) { + srv, obs := newOpenPRGuardServer(t, []map[string]any{ + openPRFixture(7, "Fixes #42", "hive-app[bot]", time.Now()), + }, []string{"needs-human"}) + defer srv.Close() + c := newTestClient(t, srv, "o", []string{"r"}) + got, err := c.IssueDispatchBlockReason(context.Background(), "o/r", 42) + if err != nil || got != "needs-human" { + t.Fatalf("reason = %q, %v; want needs-human before any listing", got, err) + } + if obs.pullsCalls != 0 { + t.Fatalf("open-PR listings = %d, want 0 when a label already refuses", obs.pullsCalls) + } +} + +func TestOpenPRGuard_UnlinkedIssueIsAdmitted(t *testing.T) { + srv, obs := newOpenPRGuardServer(t, []map[string]any{ + openPRFixture(7, "Fixes #41", "someone", time.Now()), + }, nil) + defer srv.Close() + c := newTestClient(t, srv, "o", []string{"r"}) + got, err := c.IssueDispatchBlockReason(context.Background(), "o/r", 42) + if err != nil || got != "" { + t.Fatalf("reason = %q, %v; want admitted", got, err) + } + if len(obs.addedLabels) != 0 { + t.Fatalf("labels added = %v, want none", obs.addedLabels) + } +} + +func TestOpenPRGuard_ConfigOffAdmitsWithoutListing(t *testing.T) { + srv, obs := newOpenPRGuardServer(t, []map[string]any{ + openPRFixture(7, "Fixes #42", "someone", time.Now()), + }, nil) + defer srv.Close() + c := newTestClient(t, srv, "o", []string{"r"}) + c.SetSkipIssuesWithOpenPR(false) + if c.SkipIssuesWithOpenPR() { + t.Fatal("SkipIssuesWithOpenPR must report the switch off") + } + got, err := c.IssueDispatchBlockReason(context.Background(), "o/r", 42) + if err != nil || got != "" { + t.Fatalf("reason = %q, %v; want admitted with the guard off", got, err) + } + if obs.pullsCalls != 0 { + t.Fatalf("open-PR listings = %d, want 0 with the guard off", obs.pullsCalls) + } +} + +func TestOpenPRGuard_ListingFailureFailsOpen(t *testing.T) { + srv, obs := newOpenPRGuardServer(t, nil, nil) + defer srv.Close() + obs.failPulls = true + c := newTestClient(t, srv, "o", []string{"r"}) + got, err := c.IssueDispatchBlockReason(context.Background(), "o/r", 42) + if err != nil || got != "" { + t.Fatalf("reason = %q, %v; want admitted when the listing fails", got, err) + } + if _, ok, err := c.OpenPRLinkingIssue(context.Background(), "o/r", 42); ok || err == nil { + t.Fatalf("OpenPRLinkingIssue = ok=%v err=%v; want the listing error surfaced", ok, err) + } +} + +func TestOpenPRGuard_InvalidateForcesRelist(t *testing.T) { + srv, obs := newOpenPRGuardServer(t, nil, nil) + defer srv.Close() + c := newTestClient(t, srv, "o", []string{"r"}) + for i := 0; i < 2; i++ { + if _, ok, err := c.OpenPRLinkingIssue(context.Background(), "r", 42); ok || err != nil { + t.Fatalf("OpenPRLinkingIssue = %v, %v", ok, err) + } + } + if obs.pullsCalls != 1 { + t.Fatalf("listings = %d, want 1 before invalidation", obs.pullsCalls) + } + c.InvalidateOpenPRLinks("o/r") + if _, _, err := c.OpenPRLinkingIssue(context.Background(), "o/r", 42); err != nil { + t.Fatal(err) + } + c.InvalidateOpenPRLinks("") + if _, _, err := c.OpenPRLinkingIssue(context.Background(), "o/r", 42); err != nil { + t.Fatal(err) + } + if obs.pullsCalls != 3 { + t.Fatalf("listings = %d, want 3 after two invalidations", obs.pullsCalls) + } + var nilClient *Client + if nilClient.SkipIssuesWithOpenPR() { + t.Fatal("nil client must report the guard off") + } + nilClient.SetSkipIssuesWithOpenPR(true) + nilClient.InvalidateOpenPRLinks("") + if _, ok := nilClient.OpenPRLinkGuard(context.Background(), "o/r", 1, nil); ok { + t.Fatal("nil client guard must admit") + } + if !strings.HasPrefix(ClaimBlockReasonOpenPRPrefix, "open PR already linked") { + t.Fatalf("prefix %q drifted from the log line", ClaimBlockReasonOpenPRPrefix) + } +} diff --git a/src/pkg/github/pr_duplicate_fix_sweep.go b/src/pkg/github/pr_duplicate_fix_sweep.go new file mode 100644 index 0000000000..784f421ad0 --- /dev/null +++ b/src/pkg/github/pr_duplicate_fix_sweep.go @@ -0,0 +1,421 @@ +package github + +import ( + "context" + "fmt" + "log/slog" + "sort" + "strings" + "time" + + gh "github.com/google/go-github/v72/github" +) + +// Same-issue duplicate-PR resolver (hivecommons/hive#14799). When two open +// pull requests both link one issue with a closing keyword, the hive used +// to label the pair conflicts-with-open-pr and park the later one at +// stage/085 for the owner to pick. The pick is mechanical, so the sweep +// makes it: the earlier-created PR wins while its checks are green or +// pending, the later one gets one comment and is closed with the reason +// duplicate-of:. Only the genuinely ambiguous case — the earlier PR +// is red or stale and the later one is green — still goes to the owner. A +// person's PR is never closed by this sweep: it gets the comment and stays +// open where it is. + +const ( + // DuplicateFixSweepAuditPrefix prefixes the dashboard audit action for + // every write the sweep takes. + DuplicateFixSweepAuditPrefix = "duplicate-fix-" + // DuplicateFixActionClosed: the later hive-authored PR was closed. + DuplicateFixActionClosed = "closed" + // DuplicateFixActionCommented: the later PR is a person's; the comment + // was posted and the PR left open. + DuplicateFixActionCommented = "commented" + // DuplicateFixActionEscalated: the earlier PR is red or stale while the + // later is green; the later PR was parked for the owner. + DuplicateFixActionEscalated = "escalated" + + // DefaultDuplicateFixSweepMaxActions bounds writes per sweep. + DefaultDuplicateFixSweepMaxActions = 10 + // DuplicateFixStaleAfter is how long an earlier PR may go without an + // update before a green later PR earns the owner's attention instead of + // being closed. + DuplicateFixStaleAfter = 24 * time.Hour + + // duplicateFixMarker tags the sweep's comment so a later sweep posts it + // at most once per PR. + duplicateFixMarker = "" + // duplicateFixHumanLabel is the label the hive puts on PRs a person + // authored; such PRs are commented on, never closed. + duplicateFixHumanLabel = "author/human" + // duplicateFixAuthorLabelPrefix is the hive's author attribution family. + duplicateFixAuthorLabelPrefix = "author/" +) + +// DuplicateFixCheckState is the rollup of the latest check runs on a PR head. +type DuplicateFixCheckState string + +const ( + DuplicateFixChecksGreen DuplicateFixCheckState = "green" + DuplicateFixChecksPending DuplicateFixCheckState = "pending" + DuplicateFixChecksRed DuplicateFixCheckState = "red" +) + +// DuplicateFixFacts is everything DecideDuplicateFix needs about one pair. +type DuplicateFixFacts struct { + EarlierChecks DuplicateFixCheckState + LaterChecks DuplicateFixCheckState + // EarlierStale: the earlier PR has had no update for DuplicateFixStaleAfter. + EarlierStale bool + // LaterHuman: a person authored the later PR. + LaterHuman bool +} + +// DuplicateFixVerdict is DecideDuplicateFix's outcome. +type DuplicateFixVerdict string + +const ( + // DuplicateFixVerdictClose: comment on and close the later PR. + DuplicateFixVerdictClose DuplicateFixVerdict = "close" + // DuplicateFixVerdictCommentOnly: comment on the later PR, leave it open. + DuplicateFixVerdictCommentOnly DuplicateFixVerdict = "comment-only" + // DuplicateFixVerdictEscalate: park the later PR for the owner. + DuplicateFixVerdictEscalate DuplicateFixVerdict = "escalate" +) + +// DecideDuplicateFix is the pure rule: the earlier PR wins when its checks +// are green or pending; a red or stale earlier PR facing a green later one +// is the owner's call. A person's later PR is never closed — the verdict +// degrades to comment-only (or escalate, which also leaves it open). +func DecideDuplicateFix(f DuplicateFixFacts) DuplicateFixVerdict { + earlierWeak := f.EarlierChecks == DuplicateFixChecksRed || f.EarlierStale + if earlierWeak && f.LaterChecks == DuplicateFixChecksGreen { + return DuplicateFixVerdictEscalate + } + if f.LaterHuman { + return DuplicateFixVerdictCommentOnly + } + return DuplicateFixVerdictClose +} + +// DuplicateFixEvent is one write the sweep took. +type DuplicateFixEvent struct { + Repo string + Number int + Author string + IssueRepo string + Issue int + // KeeperPR is the earlier PR that stays open. + KeeperPR int + Action string + // Reason is the recorded disposition: "duplicate-of:" for a + // close or comment, "earlier--later-green" for an escalation. + Reason string +} + +// DuplicateFixSweepOptions configures SweepDuplicateFixPRs. +type DuplicateFixSweepOptions struct { + MaxActions int + Audit func(DuplicateFixEvent) + // Now overrides the clock (tests). + Now func() time.Time +} + +// DuplicateFixSweepResult summarises one sweep. +type DuplicateFixSweepResult struct { + Closed []DuplicateFixEvent + Commented []DuplicateFixEvent + Escalated []DuplicateFixEvent + // Pairs is the number of same-issue open-PR pairs examined. + Pairs int + Skipped int +} + +// Actions counts the writes the sweep took, against MaxActions. +func (r *DuplicateFixSweepResult) Actions() int { + if r == nil { + return 0 + } + return len(r.Closed) + len(r.Commented) + len(r.Escalated) +} + +// RenderDuplicateFixComment is the one comment the later PR receives. +func RenderDuplicateFixComment(keeper, issue int, human bool) string { + if human { + return fmt.Sprintf("%s\nSuperseded by #%d (earlier fix for #%d). Leaving open because a person authored this PR — "+ + "close it if #%d covers your change, or comment why both should land.", duplicateFixMarker, keeper, issue, keeper) + } + return fmt.Sprintf("%s\nSuperseded by #%d (earlier fix for #%d). Closing as duplicate.", duplicateFixMarker, keeper, issue) +} + +// SweepDuplicateFixPRs walks every configured repo's open PRs (the same +// listing the pre-dispatch guard caches) and resolves each issue two or more +// of them close. +func (c *Client) SweepDuplicateFixPRs(ctx context.Context, opts DuplicateFixSweepOptions) (*DuplicateFixSweepResult, error) { + if c == nil { + return nil, ErrNoGitHubClient + } + maxActions := opts.MaxActions + if maxActions <= 0 { + maxActions = DefaultDuplicateFixSweepMaxActions + } + now := time.Now + if opts.Now != nil { + now = opts.Now + } + result := &DuplicateFixSweepResult{} + for _, repo := range c.getRepos() { + if result.Actions() >= maxActions { + break + } + idx, err := c.openPRIndexFor(ctx, repo) + if err != nil { + return result, err + } + byNumber := map[int]*gh.PullRequest{} + for _, pr := range idx.prs { + if pr != nil { + byNumber[pr.GetNumber()] = pr + } + } + for _, key := range sortedKeys(idx.byIssue) { + if result.Actions() >= maxActions { + break + } + links := idx.byIssue[key] + if len(links) < 2 { + continue + } + result.Pairs++ + events, err := c.resolveDuplicateFixGroup(ctx, repo, links, byNumber, now()) + if err != nil { + return result, err + } + if len(events) == 0 { + result.Skipped++ + } + for _, ev := range events { + switch ev.Action { + case DuplicateFixActionClosed: + result.Closed = append(result.Closed, ev) + case DuplicateFixActionCommented: + result.Commented = append(result.Commented, ev) + case DuplicateFixActionEscalated: + result.Escalated = append(result.Escalated, ev) + } + if opts.Audit != nil { + opts.Audit(ev) + } + } + } + if result.Actions() > 0 { + c.InvalidateOpenPRLinks(repo) + } + } + return result, nil +} + +func sortedKeys(m map[string][]OpenPRLink) []string { + keys := make([]string, 0, len(m)) + for k := range m { + keys = append(keys, k) + } + sort.Strings(keys) + return keys +} + +// resolveDuplicateFixGroup resolves one issue's open PRs: the earliest is +// the keeper, every later one is decided against it. +func (c *Client) resolveDuplicateFixGroup(ctx context.Context, repo string, links []OpenPRLink, byNumber map[int]*gh.PullRequest, now time.Time) ([]DuplicateFixEvent, error) { + owner, repoName := c.splitRepo(repo) + keeperLink := links[0] + keeper := byNumber[keeperLink.PRNumber] + if keeper == nil { + return nil, nil + } + // The issue must still be open: a closed issue is the supersession + // sweep's territory (its merged closer decides), not this one's. + issueOwner, issueRepoName := c.splitRepo(keeperLink.Repo) + issue, _, err := c.client.Issues.Get(WithRESTCaller(ctx, "hive:duplicate_fix"), issueOwner, issueRepoName, keeperLink.Issue) + if err != nil { + c.logger.Warn("duplicate-fix sweep: could not read issue, skipping", slog.String("repo", keeperLink.Repo), slog.Int("issue", keeperLink.Issue), slog.String("error", err.Error())) + return nil, nil + } + if issue != nil && !strings.EqualFold(issue.GetState(), "open") { + return nil, nil + } + earlierChecks, err := c.duplicateFixCheckState(ctx, owner, repoName, keeper) + if err != nil { + return nil, err + } + earlierStale := now.Sub(keeper.GetUpdatedAt().Time) > DuplicateFixStaleAfter + + var events []DuplicateFixEvent + for _, link := range links[1:] { + later := byNumber[link.PRNumber] + if later == nil { + continue + } + labels := labelNames(later.Labels) + // An operator's hold or exemption is an instruction to leave the PR + // alone, as every other sweep honours it. + if c.isHeld(labels) || c.isExempt(labels) { + continue + } + already, err := c.hasDuplicateFixComment(ctx, owner, repoName, later.GetNumber()) + if err != nil { + return events, err + } + if already { + continue + } + laterChecks, err := c.duplicateFixCheckState(ctx, owner, repoName, later) + if err != nil { + return events, err + } + facts := DuplicateFixFacts{ + EarlierChecks: earlierChecks, + LaterChecks: laterChecks, + EarlierStale: earlierStale, + LaterHuman: c.duplicateFixIsHuman(later), + } + verdict := DecideDuplicateFix(facts) + event := DuplicateFixEvent{ + Repo: repo, + Number: later.GetNumber(), + Author: later.GetUser().GetLogin(), + IssueRepo: link.Repo, + Issue: link.Issue, + KeeperPR: keeper.GetNumber(), + } + switch verdict { + case DuplicateFixVerdictEscalate: + event.Action = DuplicateFixActionEscalated + event.Reason = "earlier-red-later-green" + if earlierChecks != DuplicateFixChecksRed { + event.Reason = "earlier-stale-later-green" + } + body := fmt.Sprintf("%s\nBoth this PR and #%d fix #%d. #%d opened first but its checks are %s%s while this PR is green, "+ + "so the hive is asking the owner to pick rather than closing either.", + duplicateFixMarker, keeper.GetNumber(), link.Issue, keeper.GetNumber(), earlierChecks, duplicateFixStaleNote(earlierStale)) + if err := c.CreateIssueComment(ctx, repo, later.GetNumber(), body); err != nil { + return events, fmt.Errorf("commenting on duplicate PR %s#%d: %w", repo, later.GetNumber(), err) + } + if err := c.parkSupersededPR(ctx, repo, later.GetNumber()); err != nil { + return events, err + } + case DuplicateFixVerdictCommentOnly: + event.Action = DuplicateFixActionCommented + event.Reason = fmt.Sprintf("duplicate-of:%d", keeper.GetNumber()) + if err := c.CreateIssueComment(ctx, repo, later.GetNumber(), RenderDuplicateFixComment(keeper.GetNumber(), link.Issue, true)); err != nil { + return events, fmt.Errorf("commenting on duplicate PR %s#%d: %w", repo, later.GetNumber(), err) + } + default: + event.Action = DuplicateFixActionClosed + event.Reason = fmt.Sprintf("duplicate-of:%d", keeper.GetNumber()) + if err := c.CreateIssueComment(ctx, repo, later.GetNumber(), RenderDuplicateFixComment(keeper.GetNumber(), link.Issue, false)); err != nil { + return events, fmt.Errorf("commenting on duplicate PR %s#%d: %w", repo, later.GetNumber(), err) + } + if err := c.labelAndCloseSupersededPR(ctx, owner, repoName, later.GetNumber()); err != nil { + return events, err + } + } + c.logger.Info("duplicate-fix sweep: resolved same-issue PR pair", + slog.String("repo", repo), slog.Int("issue", link.Issue), slog.Int("keeper", keeper.GetNumber()), + slog.Int("pr", later.GetNumber()), slog.String("action", event.Action), slog.String("reason", event.Reason), + slog.String("earlier_checks", string(earlierChecks)), slog.String("later_checks", string(laterChecks))) + events = append(events, event) + } + return events, nil +} + +func duplicateFixStaleNote(stale bool) string { + if stale { + return " and it has had no update for over 24h" + } + return "" +} + +// duplicateFixIsHuman: the hive's author/* attribution is authoritative +// when present — author/human is a person, author/clanker or another +// author/* is an agent run under a person's login and may be closed. With +// no attribution yet, any login that is not the hive's identity or a bot +// is treated as a person. +func (c *Client) duplicateFixIsHuman(pr *gh.PullRequest) bool { + for _, l := range pr.Labels { + name := strings.ToLower(strings.TrimSpace(l.GetName())) + if strings.HasPrefix(name, duplicateFixAuthorLabelPrefix) { + return name == duplicateFixHumanLabel + } + } + identity := c.getHiveIdentity() + if identity.AppLogin == "" { + identity.AppLogin = c.appBotLogin + } + return supersessionIsHuman(pr.GetUser().GetLogin(), pr.GetUser().GetType(), identity) +} + +// hasDuplicateFixComment reports whether the sweep already commented on the PR. +func (c *Client) hasDuplicateFixComment(ctx context.Context, owner, repo string, number int) (bool, error) { + opts := &gh.IssueListCommentsOptions{ListOptions: gh.ListOptions{PerPage: 100}} + for { + comments, resp, err := c.client.Issues.ListComments(WithRESTCaller(ctx, "hive:duplicate_fix"), owner, repo, number, opts) + if err != nil { + return false, fmt.Errorf("listing comments on %s/%s#%d: %w", owner, repo, number, err) + } + for _, cm := range comments { + if strings.Contains(cm.GetBody(), duplicateFixMarker) { + return true, nil + } + } + if resp == nil || resp.NextPage == 0 { + return false, nil + } + opts.Page = resp.NextPage + } +} + +// duplicateFixCheckState rolls the latest check runs on the PR head up to +// green, pending or red. No check runs at all is pending: the head may not +// have been built yet, and "nothing ran" must not read as a pass. +func (c *Client) duplicateFixCheckState(ctx context.Context, owner, repo string, pr *gh.PullRequest) (DuplicateFixCheckState, error) { + sha := pr.GetHead().GetSHA() + if sha == "" { + return DuplicateFixChecksPending, nil + } + ctx = WithRESTCaller(ctx, "hive:duplicate_fix") + var all []*gh.CheckRun + opts := &gh.ListCheckRunsOptions{ListOptions: gh.ListOptions{PerPage: 100}} + for { + runs, resp, err := c.client.Checks.ListCheckRunsForRef(ctx, owner, repo, sha, opts) + if err != nil { + return "", fmt.Errorf("listing check runs for %s/%s#%d: %w", owner, repo, pr.GetNumber(), err) + } + if runs != nil { + all = append(all, runs.CheckRuns...) + } + if resp == nil || resp.NextPage == 0 { + break + } + opts.Page = resp.NextPage + } + counted, pending := 0, false + for _, cr := range latestCheckRunsByNameAndApp(all) { + name := cr.GetName() + if name == "" || isMetaCheck(name) || isIgnorableCICheck(name) { + continue + } + counted++ + if cr.GetStatus() != "completed" { + pending = true + continue + } + if checkConclusionIsRed(cr.GetConclusion()) { + return DuplicateFixChecksRed, nil + } + } + if counted == 0 || pending { + return DuplicateFixChecksPending, nil + } + return DuplicateFixChecksGreen, nil +} diff --git a/src/pkg/github/pr_duplicate_fix_sweep_test.go b/src/pkg/github/pr_duplicate_fix_sweep_test.go new file mode 100644 index 0000000000..338bc97400 --- /dev/null +++ b/src/pkg/github/pr_duplicate_fix_sweep_test.go @@ -0,0 +1,365 @@ +package github + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + "time" +) + +type dupFixPR struct { + Number int + Body string + Author string + Human bool // carries author/human + CreatedAt time.Time + UpdatedAt time.Time + Checks string // "green", "red", "pending", "" (none) + Comments []string + Labels []string +} + +type dupFixObs struct { + mu sync.Mutex + comments map[int][]string + labels map[int][]string + closed []int + pulls int +} + +func dupFixServer(t *testing.T, prs []dupFixPR, issueState string) (*httptest.Server, *dupFixObs) { + t.Helper() + obs := &dupFixObs{comments: map[int][]string{}, labels: map[int][]string{}} + byNum := map[int]dupFixPR{} + for _, pr := range prs { + byNum[pr.Number] = pr + } + mux := http.NewServeMux() + mux.HandleFunc("/repos/o/r/pulls", func(w http.ResponseWriter, r *http.Request) { + obs.mu.Lock() + obs.pulls++ + obs.mu.Unlock() + out := []map[string]any{} + for _, pr := range prs { + labels := []map[string]any{} + if pr.Human { + labels = append(labels, map[string]any{"name": duplicateFixHumanLabel}) + } + for _, l := range pr.Labels { + labels = append(labels, map[string]any{"name": l}) + } + userType := "User" + if strings.HasSuffix(pr.Author, "[bot]") { + userType = "Bot" + } + out = append(out, map[string]any{ + "number": pr.Number, "state": "open", "title": fmt.Sprintf("fix %d", pr.Number), "body": pr.Body, + "html_url": fmt.Sprintf("https://github.com/o/r/pull/%d", pr.Number), + "created_at": pr.CreatedAt.UTC().Format(time.RFC3339), + "updated_at": pr.UpdatedAt.UTC().Format(time.RFC3339), + "user": map[string]any{"login": pr.Author, "type": userType}, + "head": map[string]any{"sha": fmt.Sprintf("sha%d", pr.Number)}, + "labels": labels, + }) + } + _ = json.NewEncoder(w).Encode(out) + }) + mux.HandleFunc("/repos/o/r/issues/", func(w http.ResponseWriter, r *http.Request) { + rest := strings.TrimPrefix(r.URL.Path, "/repos/o/r/issues/") + parts := strings.Split(rest, "/") + var n int + fmt.Sscanf(parts[0], "%d", &n) + switch { + case len(parts) == 1 && r.Method == http.MethodGet: + _ = json.NewEncoder(w).Encode(map[string]any{"number": n, "state": issueState}) + case len(parts) == 1 && r.Method == http.MethodPatch: + var body map[string]any + _ = json.NewDecoder(r.Body).Decode(&body) + if body["state"] == "closed" { + obs.mu.Lock() + obs.closed = append(obs.closed, n) + obs.mu.Unlock() + } + _, _ = io.WriteString(w, `{}`) + case parts[1] == "comments" && r.Method == http.MethodGet: + out := []map[string]any{} + obs.mu.Lock() + existing := append([]string{}, byNum[n].Comments...) + existing = append(existing, obs.comments[n]...) + obs.mu.Unlock() + for i, body := range existing { + out = append(out, map[string]any{"id": i + 1, "body": body}) + } + _ = json.NewEncoder(w).Encode(out) + case parts[1] == "comments" && r.Method == http.MethodPost: + var body struct { + Body string `json:"body"` + } + _ = json.NewDecoder(r.Body).Decode(&body) + obs.mu.Lock() + obs.comments[n] = append(obs.comments[n], body.Body) + obs.mu.Unlock() + _, _ = io.WriteString(w, `{"id":99}`) + case parts[1] == "labels" && r.Method == http.MethodPost: + var names []string + _ = json.NewDecoder(r.Body).Decode(&names) + obs.mu.Lock() + obs.labels[n] = append(obs.labels[n], names...) + obs.mu.Unlock() + _, _ = io.WriteString(w, `[]`) + default: + t.Errorf("unexpected issues request %s %s", r.Method, r.URL.Path) + w.WriteHeader(http.StatusNotFound) + } + }) + mux.HandleFunc("/repos/o/r/commits/", func(w http.ResponseWriter, r *http.Request) { + sha := strings.TrimSuffix(strings.TrimPrefix(r.URL.Path, "/repos/o/r/commits/"), "/check-runs") + var n int + fmt.Sscanf(sha, "sha%d", &n) + runs := []map[string]any{} + switch byNum[n].Checks { + case "green": + runs = append(runs, map[string]any{"id": 1, "name": "build", "status": "completed", "conclusion": "success"}) + case "red": + runs = append(runs, map[string]any{"id": 1, "name": "build", "status": "completed", "conclusion": "failure"}) + case "pending": + runs = append(runs, map[string]any{"id": 1, "name": "build", "status": "in_progress"}) + } + _ = json.NewEncoder(w).Encode(map[string]any{"total_count": len(runs), "check_runs": runs}) + }) + mux.HandleFunc("/repos/o/r/labels/", func(w http.ResponseWriter, r *http.Request) { + _, _ = io.WriteString(w, `{"name":"x"}`) + }) + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + w.WriteHeader(http.StatusNotFound) + }) + return httptest.NewServer(mux), obs +} + +var dupT0 = time.Date(2026, 10, 27, 1, 0, 0, 0, time.UTC) + +func dupNow() time.Time { return dupT0.Add(2 * time.Hour) } + +func TestDecideDuplicateFix(t *testing.T) { + for _, tc := range []struct { + name string + facts DuplicateFixFacts + want DuplicateFixVerdict + }{ + {"earlier green, later green", DuplicateFixFacts{EarlierChecks: DuplicateFixChecksGreen, LaterChecks: DuplicateFixChecksGreen}, DuplicateFixVerdictClose}, + {"earlier pending, later green", DuplicateFixFacts{EarlierChecks: DuplicateFixChecksPending, LaterChecks: DuplicateFixChecksGreen}, DuplicateFixVerdictClose}, + {"earlier red, later pending", DuplicateFixFacts{EarlierChecks: DuplicateFixChecksRed, LaterChecks: DuplicateFixChecksPending}, DuplicateFixVerdictClose}, + {"earlier red, later green", DuplicateFixFacts{EarlierChecks: DuplicateFixChecksRed, LaterChecks: DuplicateFixChecksGreen}, DuplicateFixVerdictEscalate}, + {"earlier stale, later green", DuplicateFixFacts{EarlierChecks: DuplicateFixChecksGreen, EarlierStale: true, LaterChecks: DuplicateFixChecksGreen}, DuplicateFixVerdictEscalate}, + {"human later", DuplicateFixFacts{EarlierChecks: DuplicateFixChecksGreen, LaterChecks: DuplicateFixChecksGreen, LaterHuman: true}, DuplicateFixVerdictCommentOnly}, + {"human later, earlier red", DuplicateFixFacts{EarlierChecks: DuplicateFixChecksRed, LaterChecks: DuplicateFixChecksGreen, LaterHuman: true}, DuplicateFixVerdictEscalate}, + } { + t.Run(tc.name, func(t *testing.T) { + if got := DecideDuplicateFix(tc.facts); got != tc.want { + t.Fatalf("verdict = %q, want %q", got, tc.want) + } + }) + } +} + +func TestSweepDuplicateFixPRs_EarlierGreenWinsLaterClosedOnce(t *testing.T) { + srv, obs := dupFixServer(t, []dupFixPR{ + {Number: 14775, Body: "Fixes #14766", Author: "clubanderson", Human: true, CreatedAt: dupT0, UpdatedAt: dupT0, Checks: "green"}, + // A relay contributor's agent run (author/clanker) under a person's login. + {Number: 14780, Body: "Fixes #14766", Author: "thatguywob", Labels: []string{"author/clanker"}, CreatedAt: dupT0.Add(9 * time.Minute), UpdatedAt: dupT0.Add(9 * time.Minute), Checks: "green"}, + {Number: 14771, Body: "Fixes #14763", Author: "clubanderson", Human: true, CreatedAt: dupT0, UpdatedAt: dupT0, Checks: "green"}, + {Number: 14776, Body: "Fixes #14763", Author: "hive-app[bot]", CreatedAt: dupT0.Add(2 * time.Minute), UpdatedAt: dupT0.Add(2 * time.Minute), Checks: "pending"}, + {Number: 14790, Body: "Fixes #14700", Author: "hive-app[bot]", CreatedAt: dupT0, UpdatedAt: dupT0, Checks: "green"}, + }, "open") + defer srv.Close() + c, rec := newSupersessionTestClient(t, srv, "o", "r") + + var audited []DuplicateFixEvent + res, err := c.SweepDuplicateFixPRs(context.Background(), DuplicateFixSweepOptions{Now: dupNow, Audit: func(e DuplicateFixEvent) { audited = append(audited, e) }}) + if err != nil { + t.Fatalf("SweepDuplicateFixPRs: %v", err) + } + if res.Pairs != 2 || len(res.Closed) != 2 || len(res.Commented) != 0 || len(res.Escalated) != 0 { + t.Fatalf("result = %+v, want two pairs both closed", res) + } + if len(obs.closed) != 2 || obs.closed[0] != 14776 || obs.closed[1] != 14780 { + t.Fatalf("closed = %v, want [14776 14780]", obs.closed) + } + want := "Superseded by #14775 (earlier fix for #14766). Closing as duplicate." + if cs := obs.comments[14780]; len(cs) != 1 || !strings.Contains(cs[0], want) { + t.Fatalf("comments on 14780 = %v, want exactly one: %q", cs, want) + } + if got := obs.labels[14780]; len(got) != 1 || got[0] != SupersededLabel { + t.Fatalf("labels on 14780 = %v, want [%s]", got, SupersededLabel) + } + if len(obs.comments[14775])+len(obs.comments[14771])+len(obs.comments[14790]) != 0 { + t.Fatal("keepers and the lone PR must be untouched") + } + if len(parkedPRs(rec)) != 0 { + t.Fatalf("parked = %v, want none", parkedPRs(rec)) + } + for _, e := range audited { + if e.Action != DuplicateFixActionClosed || !strings.HasPrefix(e.Reason, "duplicate-of:") || e.KeeperPR == 0 { + t.Fatalf("audit event %+v lacks the duplicate-of: reason", e) + } + } + if audited[1].Reason != "duplicate-of:14775" || audited[1].Issue != 14766 { + t.Fatalf("audit = %+v", audited[1]) + } + if res.Actions() != 2 { + t.Fatalf("Actions = %d", res.Actions()) + } + + // Second sweep: the marker comment is already there, nothing is written. + res, err = c.SweepDuplicateFixPRs(context.Background(), DuplicateFixSweepOptions{Now: dupNow}) + if err != nil { + t.Fatal(err) + } + if res.Actions() != 0 || res.Skipped != 2 || len(obs.closed) != 2 || len(obs.comments[14780]) != 1 { + t.Fatalf("second sweep = %+v closed=%v comments=%v, want idempotent", res, obs.closed, obs.comments[14780]) + } +} + +func TestSweepDuplicateFixPRs_EarlierRedLaterGreenEscalates(t *testing.T) { + srv, obs := dupFixServer(t, []dupFixPR{ + {Number: 10, Body: "Fixes #1", Author: "hive-app[bot]", CreatedAt: dupT0, UpdatedAt: dupT0, Checks: "red"}, + {Number: 20, Body: "Closes #1", Author: "hive-app[bot]", CreatedAt: dupT0.Add(time.Hour), UpdatedAt: dupT0.Add(time.Hour), Checks: "green"}, + }, "open") + defer srv.Close() + c, rec := newSupersessionTestClient(t, srv, "o", "r") + res, err := c.SweepDuplicateFixPRs(context.Background(), DuplicateFixSweepOptions{Now: dupNow}) + if err != nil { + t.Fatal(err) + } + if len(res.Escalated) != 1 || res.Escalated[0].Number != 20 || res.Escalated[0].Reason != "earlier-red-later-green" { + t.Fatalf("escalated = %+v", res.Escalated) + } + if len(obs.closed) != 0 { + t.Fatalf("closed = %v, want none when the owner must pick", obs.closed) + } + if got := parkedPRs(rec); len(got) != 1 || got[0] != 20 { + t.Fatalf("parked = %v, want [20]", got) + } + if cs := obs.comments[20]; len(cs) != 1 || !strings.Contains(cs[0], "#10 opened first but its checks are red") { + t.Fatalf("comments on 20 = %v", cs) + } +} + +func TestSweepDuplicateFixPRs_EarlierStaleLaterGreenEscalates(t *testing.T) { + stale := dupNow().Add(-DuplicateFixStaleAfter - time.Minute) + srv, obs := dupFixServer(t, []dupFixPR{ + {Number: 10, Body: "Fixes #1", Author: "hive-app[bot]", CreatedAt: stale, UpdatedAt: stale, Checks: "green"}, + {Number: 20, Body: "Fixes #1", Author: "hive-app[bot]", CreatedAt: dupT0, UpdatedAt: dupT0, Checks: "green"}, + }, "open") + defer srv.Close() + c, rec := newSupersessionTestClient(t, srv, "o", "r") + res, err := c.SweepDuplicateFixPRs(context.Background(), DuplicateFixSweepOptions{Now: dupNow}) + if err != nil { + t.Fatal(err) + } + if len(res.Escalated) != 1 || res.Escalated[0].Reason != "earlier-stale-later-green" || len(obs.closed) != 0 || len(parkedPRs(rec)) != 1 { + t.Fatalf("res=%+v closed=%v parked=%v", res, obs.closed, parkedPRs(rec)) + } + if cs := obs.comments[20]; len(cs) != 1 || !strings.Contains(cs[0], "no update for over 24h") { + t.Fatalf("comments on 20 = %v", cs) + } +} + +func TestSweepDuplicateFixPRs_HumanLaterPRCommentedNotClosed(t *testing.T) { + srv, obs := dupFixServer(t, []dupFixPR{ + {Number: 10, Body: "Fixes #1", Author: "hive-app[bot]", CreatedAt: dupT0, UpdatedAt: dupT0, Checks: "green"}, + {Number: 20, Body: "Fixes #1", Author: "alice", Human: true, CreatedAt: dupT0.Add(time.Hour), UpdatedAt: dupT0.Add(time.Hour), Checks: "green"}, + // No author/human label but a person's login: still a person. + {Number: 30, Body: "Fixes #1", Author: "bob", CreatedAt: dupT0.Add(2 * time.Hour), UpdatedAt: dupT0.Add(2 * time.Hour), Checks: ""}, + }, "open") + defer srv.Close() + c, rec := newSupersessionTestClient(t, srv, "o", "r") + res, err := c.SweepDuplicateFixPRs(context.Background(), DuplicateFixSweepOptions{Now: dupNow}) + if err != nil { + t.Fatal(err) + } + if len(res.Commented) != 2 || len(res.Closed) != 0 || len(obs.closed) != 0 || len(parkedPRs(rec)) != 0 { + t.Fatalf("res=%+v closed=%v parked=%v, want comment-only for both human PRs", res, obs.closed, parkedPRs(rec)) + } + for _, n := range []int{20, 30} { + cs := obs.comments[n] + if len(cs) != 1 || !strings.Contains(cs[0], "Superseded by #10 (earlier fix for #1)") || !strings.Contains(cs[0], "Leaving open") { + t.Fatalf("comments on %d = %v", n, cs) + } + if len(obs.labels[n]) != 0 { + t.Fatalf("labels on %d = %v, want none (no park, no superseded)", n, obs.labels[n]) + } + } + if res.Commented[0].Reason != "duplicate-of:10" { + t.Fatalf("reason = %q", res.Commented[0].Reason) + } +} + +func TestSweepDuplicateFixPRs_SkipsClosedIssueHeldPRAndRespectsMaxActions(t *testing.T) { + t.Run("closed issue", func(t *testing.T) { + srv, obs := dupFixServer(t, []dupFixPR{ + {Number: 10, Body: "Fixes #1", Author: "hive-app[bot]", CreatedAt: dupT0, UpdatedAt: dupT0, Checks: "green"}, + {Number: 20, Body: "Fixes #1", Author: "hive-app[bot]", CreatedAt: dupT0.Add(time.Hour), UpdatedAt: dupT0.Add(time.Hour), Checks: "green"}, + }, "closed") + defer srv.Close() + c, _ := newSupersessionTestClient(t, srv, "o", "r") + res, err := c.SweepDuplicateFixPRs(context.Background(), DuplicateFixSweepOptions{Now: dupNow}) + if err != nil || res.Actions() != 0 || res.Skipped != 1 || len(obs.closed) != 0 { + t.Fatalf("res=%+v err=%v closed=%v, want the supersession sweep's case left alone", res, err, obs.closed) + } + }) + t.Run("held later PR", func(t *testing.T) { + srv, obs := dupFixServer(t, []dupFixPR{ + {Number: 10, Body: "Fixes #1", Author: "hive-app[bot]", CreatedAt: dupT0, UpdatedAt: dupT0, Checks: "green"}, + {Number: 20, Body: "Fixes #1", Author: "hive-app[bot]", Labels: []string{"hold"}, CreatedAt: dupT0.Add(time.Hour), UpdatedAt: dupT0.Add(time.Hour), Checks: "green"}, + }, "open") + defer srv.Close() + c, _ := newSupersessionTestClient(t, srv, "o", "r") + res, err := c.SweepDuplicateFixPRs(context.Background(), DuplicateFixSweepOptions{Now: dupNow}) + if err != nil || res.Actions() != 0 || len(obs.closed) != 0 || len(obs.comments[20]) != 0 { + t.Fatalf("res=%+v err=%v, want an operator hold honoured", res, err) + } + }) + t.Run("max actions", func(t *testing.T) { + srv, obs := dupFixServer(t, []dupFixPR{ + {Number: 10, Body: "Fixes #1", Author: "hive-app[bot]", CreatedAt: dupT0, UpdatedAt: dupT0, Checks: "green"}, + {Number: 20, Body: "Fixes #1", Author: "hive-app[bot]", CreatedAt: dupT0.Add(time.Hour), UpdatedAt: dupT0.Add(time.Hour), Checks: "green"}, + {Number: 11, Body: "Fixes #2", Author: "hive-app[bot]", CreatedAt: dupT0, UpdatedAt: dupT0, Checks: "green"}, + {Number: 21, Body: "Fixes #2", Author: "hive-app[bot]", CreatedAt: dupT0.Add(time.Hour), UpdatedAt: dupT0.Add(time.Hour), Checks: "green"}, + }, "open") + defer srv.Close() + c, _ := newSupersessionTestClient(t, srv, "o", "r") + res, err := c.SweepDuplicateFixPRs(context.Background(), DuplicateFixSweepOptions{Now: dupNow, MaxActions: 1}) + if err != nil || res.Actions() != 1 || len(obs.closed) != 1 { + t.Fatalf("res=%+v err=%v closed=%v, want one action", res, err, obs.closed) + } + }) + t.Run("nil client", func(t *testing.T) { + var c *Client + if _, err := c.SweepDuplicateFixPRs(context.Background(), DuplicateFixSweepOptions{}); err == nil { + t.Fatal("nil client must error") + } + var r *DuplicateFixSweepResult + if r.Actions() != 0 { + t.Fatal("nil result Actions must be 0") + } + }) +} + +func TestSweepDuplicateFixPRs_ListingErrorSurfaces(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusBadGateway) + })) + defer srv.Close() + c, _ := newSupersessionTestClient(t, srv, "o", "r") + if _, err := c.SweepDuplicateFixPRs(context.Background(), DuplicateFixSweepOptions{}); err == nil { + t.Fatal("want the listing error surfaced") + } +} diff --git a/src/pkg/github/repo_pause_test.go b/src/pkg/github/repo_pause_test.go index cf818fe365..fd4f3ea760 100644 --- a/src/pkg/github/repo_pause_test.go +++ b/src/pkg/github/repo_pause_test.go @@ -225,6 +225,9 @@ func TestIssueRequestWatcher_RepoPause(t *testing.T) { case r.Method == http.MethodGet && r.URL.Path == "/repos/o/r/issues/42/comments": // The claim path reads comments for the re-verify guard (#12733). _, _ = io.WriteString(w, `[]`) + case r.Method == http.MethodGet && r.URL.Path == "/repos/o/r/pulls": + // The claim gate lists open PRs for the open-PR guard (#14799). + _, _ = io.WriteString(w, `[]`) case r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/repos/o/r/labels/"): _, _ = io.WriteString(w, `{"name":"agent/security"}`) case r.Method == http.MethodPatch && strings.HasPrefix(r.URL.Path, "/repos/o/r/labels/origin/"):