From 5eebcc7183d77871d25f55f9f5651873204767d9 Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Tue, 27 Oct 2026 22:35:58 -0400 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20scanner:=20refuse=20claims?= =?UTF-8?q?=20on=20issues=20that=20already=20have=20an=20open=20linked=20P?= =?UTF-8?q?R?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every automated claim (ledger admission, the hive-open-issue claim relay request, the contributor lane) now checks the repo's open PRs live, one cached listing per repo per sweep, and refuses the claim when an open PR closes the issue. The issue is labelled has-open-pr, the skip is logged, and scanner.skip_issues_with_open_pr (default true) turns it off. Closes #14804 Part of #14799 Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Co-authored-by: clubanderson <407614+clubanderson@users.noreply.github.com> --- changelog.d/fixed-14804.md | 1 + src/cmd/hive/claims_open_pr_guard_test.go | 87 ++++++++ src/cmd/hive/claimswire.go | 17 +- src/cmd/hive/github_client_config.go | 3 + src/cmd/hive/main.go | 13 ++ src/docs/hive-open-issue.md | 6 +- src/docs/labels-and-control-signals.md | 1 + src/docs/operator-reference.md | 20 ++ src/pkg/config/config.go | 21 ++ src/pkg/config/scanner_config_test.go | 28 +++ .../config/testdata/agent_runtime_golden.json | 1 + src/pkg/dashboard/contribute_admission.go | 22 +- .../contribute_open_pr_guard_test.go | 64 ++++++ src/pkg/dashboard/deps.go | 7 + .../dashboard/settings_parity_allowlist.txt | 1 + src/pkg/github/client.go | 8 +- src/pkg/github/issue_open_pr_guard.go | 199 +++++++++++++++++ src/pkg/github/issue_open_pr_guard_test.go | 200 ++++++++++++++++++ src/pkg/github/issue_request_watcher.go | 3 + 19 files changed, 693 insertions(+), 9 deletions(-) create mode 100644 changelog.d/fixed-14804.md create mode 100644 src/cmd/hive/claims_open_pr_guard_test.go create mode 100644 src/pkg/config/scanner_config_test.go create mode 100644 src/pkg/dashboard/contribute_open_pr_guard_test.go create mode 100644 src/pkg/github/issue_open_pr_guard.go create mode 100644 src/pkg/github/issue_open_pr_guard_test.go diff --git a/changelog.d/fixed-14804.md b/changelog.d/fixed-14804.md new file mode 100644 index 0000000000..c5a0d0116c --- /dev/null +++ b/changelog.d/fixed-14804.md @@ -0,0 +1 @@ +- Issues that already have an open pull request closing them (`Fixes #N`) are no longer claimed or dispatched again: every automated claim now checks open PRs live, labels such an issue `has-open-pr` and logs `claim skipped: open PR already linked`, instead of waiting for the next claim-ledger scan. Disable with `scanner.skip_issues_with_open_pr: false` ([#14804](https://github.com/hivecommons/hive/issues/14804)). diff --git a/src/cmd/hive/claims_open_pr_guard_test.go b/src/cmd/hive/claims_open_pr_guard_test.go new file mode 100644 index 0000000000..008a41b2bb --- /dev/null +++ b/src/cmd/hive/claims_open_pr_guard_test.go @@ -0,0 +1,87 @@ +package main + +import ( + "context" + "io" + "log/slog" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/hivecommons/hive/pkg/claims" + "github.com/hivecommons/hive/pkg/github" +) + +// hivecommons/hive#14804: #14763 already had open PR #14771 ("Fixes #14763") +// when it was claimed and dispatched again, because the claim ledger had not +// yet scanned that PR. Ledger admission now checks open PRs live: a claim +// that would start new work is refused, while the claimant's own PR start +// signal, a renewal and the cleanup recheck of an existing claim are not. +func TestClaimAdmissionRefusesIssueWithOpenPR(t *testing.T) { + for _, tc := range []struct { + name string + guardOn bool + wantReason string + }{ + {name: "guard on", guardOn: true, wantReason: github.ClaimBlockReasonOpenPRPrefix + "14771"}, + {name: "guard off", guardOn: false}, + } { + t.Run(tc.name, func(t *testing.T) { + api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodGet && r.URL.Path == "/repos/o/r/pulls": + _, _ = io.WriteString(w, `[{"number":14771,"state":"open","title":"fix","body":"Fixes #1","user":{"login":"alice"}}]`) + case r.Method == http.MethodGet && r.URL.Path == "/repos/o/r/issues/1": + _, _ = io.WriteString(w, `{"number":1,"labels":[{"name":"bug"}]}`) + case r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/repos/o/r/labels/"): + _, _ = io.WriteString(w, `{"name":"has-open-pr"}`) + case r.Method == http.MethodPost && r.URL.Path == "/repos/o/r/issues/1/labels": + _, _ = io.WriteString(w, `[]`) + default: + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + w.WriteHeader(http.StatusInternalServerError) + } + })) + defer api.Close() + client := github.NewClientForTest(api.URL, "o", []string{"r"}, slog.New(slog.NewTextHandler(io.Discard, nil))) + client.SetSkipIssuesWithOpenPR(tc.guardOn) + ledger := testClaimsLedger(t) + installClaimAdmissionCheck(context.Background(), ledger, func() *github.Client { return client }) + + req := claims.Request{Repo: "o/r", Issue: 1, Holder: "scanner", HolderID: "scanner", Kind: claims.KindAgent} + res, err := ledger.Claim(req) + if err != nil { + t.Fatal(err) + } + if tc.wantReason == "" { + if res.Outcome != claims.OutcomeClaimed { + t.Fatalf("claim with the guard off = %+v; want claimed", res) + } + return + } + if res.Outcome != claims.OutcomeRefused || res.Reason != tc.wantReason { + t.Fatalf("new claim = %+v; want refused with %q", res, tc.wantReason) + } + + // The claimant's own PR start signal is its evidence, not a duplicate. + started := req + started.PRStarted = true + if res, err := ledger.Claim(started); err != nil || res.Outcome != claims.OutcomeClaimed { + t.Fatalf("PR start signal = %+v, %v; want claimed", res, err) + } + // Renewing that claim, and the cleanup recheck of it, keep it. + if res, err := ledger.Claim(req); err != nil || res.Outcome != claims.OutcomeRenewed { + t.Fatalf("renewal = %+v, %v; want renewed", res, err) + } + if n := ledger.ReleaseBlocked(); n != 0 { + t.Fatalf("cleanup tick released %d claims on the holder's own open PR", n) + } + // A different agent starting new work on it is refused. + other := claims.Request{Repo: "o/r", Issue: 1, Holder: "fixer", HolderID: "fixer", Kind: claims.KindAgent, Force: true} + if res, err := ledger.Claim(other); err != nil || res.Outcome != claims.OutcomeRefused || res.Reason != tc.wantReason { + t.Fatalf("second agent claim = %+v, %v; want refused with %q", res, err, tc.wantReason) + } + }) + } +} diff --git a/src/cmd/hive/claimswire.go b/src/cmd/hive/claimswire.go index 1f3defcd9f..8c431e10cd 100644 --- a/src/cmd/hive/claimswire.go +++ b/src/cmd/hive/claimswire.go @@ -200,7 +200,22 @@ func installClaimAdmissionCheck(ctx context.Context, ledger *claims.Ledger, clie if gh == nil { return "", github.ErrNoGitHubClient } - return gh.IssueClaimBlockReason(ctx, req.Repo, req.Issue) + reason, err := gh.IssueClaimBlockReason(ctx, req.Repo, req.Issue) + if err != nil || reason != "" { + return reason, err + } + // The open-PR guard (scanner.skip_issues_with_open_pr) refuses only a + // claim that would start new work. A start signal carrying PR evidence + // is the claimant's own PR, and a renewal or the ReleaseBlocked recheck + // of a claim the same holder already has must not release it because + // that holder's PR is now open. + if req.PRStarted { + return "", nil + } + if held, ok := ledger.Lookup(req.Repo, req.Issue); ok && held.Holder == req.Holder && held.Kind == req.Kind { + return "", nil + } + return gh.IssueOpenPRClaimBlockReason(ctx, req.Repo, req.Issue), nil }) } diff --git a/src/cmd/hive/github_client_config.go b/src/cmd/hive/github_client_config.go index 4c690fc89f..59946e8683 100644 --- a/src/cmd/hive/github_client_config.go +++ b/src/cmd/hive/github_client_config.go @@ -89,6 +89,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()) + // Live open-PR guard on every automated claim + // (scanner.skip_issues_with_open_pr, default on). + client.SetSkipIssuesWithOpenPR(b.cfg.Scanner.SkipIssuesWithOpenPREnabled()) 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/main.go b/src/cmd/hive/main.go index 2c34a3bb90..33144ec9ad 100644 --- a/src/cmd/hive/main.go +++ b/src/cmd/hive/main.go @@ -1525,6 +1525,17 @@ func (b *boot) wireBootClosures() { IssueClaimed: func(repo string, number int) (github.IssueClaim, bool) { return getClaimLedger(b.logger).Lookup(repo, number) }, + // #14804: and a live open-PR check behind the ledger, so a PR + // opened since the last eval cycle withholds its issue now. + IssueOpenPRClaimed: func(repo string, number int) (github.IssueClaim, bool) { + ghc := b.ghClient + if ghc == nil { + return github.IssueClaim{}, false + } + ctx, cancel := context.WithTimeout(b.ctx, claimsGitHubTimeout) + defer cancel() + return ghc.IssueOpenPRClaim(ctx, repo, number) + }, // #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 { @@ -2669,6 +2680,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.SkipIssuesWithOpenPREnabled()) b.ghClient.SetSentinelAlertLabel(b.cfg.Sentinel.LabelOrDefault()) if len(b.cfg.Governor.Labels.Exempt) > 0 { b.ghClient.SetExemptLabels(b.cfg.Governor.Labels.Exempt) @@ -5806,6 +5818,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.SkipIssuesWithOpenPREnabled()) b.ghClient.SetSentinelAlertLabel(b.cfg.Sentinel.LabelOrDefault()) b.ghClient.SetIssueFilter(b.cfg.Project.IssueFilter) syncAutoMergePolicyToGitHubClient(b.cfg, b.ghClient) diff --git a/src/docs/hive-open-issue.md b/src/docs/hive-open-issue.md index 7e7861db7f..641c6b0501 100644 --- a/src/docs/hive-open-issue.md +++ b/src/docs/hive-open-issue.md @@ -255,7 +255,11 @@ issue labelled `needs-human` or held, on an issue parked on a person's decision (`stage/045-needs-decision`, `needs-spec`, the retired decision labels), and on an umbrella issue wearing a never-claim label (`governor.claims.never_claim_labels`, default `epic`, case-insensitive): -an epic is the list of work, not the work. The same gate admits every +an epic is the list of work, not the work. It is also refused, with +`open PR already linked: #`, on an issue an open pull request already +closes (`Fixes #N` in its title or body), checked live and labelled +`has-open-pr` (`scanner.skip_issues_with_open_pr`, default on; +[#14804](https://github.com/hivecommons/hive/issues/14804)). The same gate admits every automated claim producer, so a start signal on such an issue records nothing. The `stage/030-pr-in-progress` label is swapped for `stage/040-covered-by-pr` when the agent's `hive-open-pr --issues` request opens the PR, and cleared when the diff --git a/src/docs/labels-and-control-signals.md b/src/docs/labels-and-control-signals.md index e1f0ff5168..bc02af9b05 100644 --- a/src/docs/labels-and-control-signals.md +++ b/src/docs/labels-and-control-signals.md @@ -98,6 +98,7 @@ Some hive-authored labels look like provenance but are read as signals, so they | `runs.triage.spec_labels` / `fix_labels` (v6/edge Spek) | Issues | Spek/runs triage | Configured exact labels route to spec or fix; mark this v6/edge when documenting Spek behavior. | Gate when enabled | Operator/human | Triage | Remove label/config | `runs.triage.spec_labels`, `runs.triage.fix_labels` (`src/pkg/classify/triage.go:35-55`) | | `tracker`, `meta-tracker`, `tracking`, `epic` (last segment) | Issues | Tracker detector, PR claim rewrite, contributor queue | Marks coordination-only/tracker issues; agents see tracker tags, contributors do not get them. | Soft for agents; hard for contributors | Human/Hive | Enumeration/contributor admission | Remove label or close tracker | Tracker detector (`src/pkg/github/client.go:2095-2161`) | | `epic` (any case; `governor.claims.never_claim_labels`) | Issues | Issue-claim admission, issue stage derivation | Umbrella issue is never a unit of agent work: every automated claim producer (relay `claim`, start signal, contributor lane) is refused with `never-claim label: