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/github_client_config_test.go b/src/cmd/hive/github_client_config_test.go index 381e2179dc..a85016dff9 100644 --- a/src/cmd/hive/github_client_config_test.go +++ b/src/cmd/hive/github_client_config_test.go @@ -84,6 +84,8 @@ var clientResyncSetters = map[string]bool{ "SetExemptLabels": true, "SetAutoMergeLabel": true, "SetIssueFilter": true, + // applyGitHubClientConfigHooks also installs it on every rebuilt client. + "SetSkipIssuesWithOpenPR": true, } const ( 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..200a7e17f7 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: