Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-14804.md
Original file line number Diff line number Diff line change
@@ -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)).
87 changes: 87 additions & 0 deletions src/cmd/hive/claims_open_pr_guard_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
17 changes: 16 additions & 1 deletion src/cmd/hive/claimswire.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}

Expand Down
3 changes: 3 additions & 0 deletions src/cmd/hive/github_client_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/hive/github_client_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
13 changes: 13 additions & 0 deletions src/cmd/hive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion src/docs/hive-open-issue.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: #<pr>`, 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
Expand Down
1 change: 1 addition & 0 deletions src/docs/labels-and-control-signals.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: <label>`, and a claim or start mark that exists on one never derives `stage/020`/`stage/030` β€” the epic stays `stage/010-actionable` ([#12999](https://github.com/hivecommons/hive/issues/12999)). | Hard for agent claims | Human | Claim admission, stage derivation | Remove label or set `never_claim_labels: []` | `src/pkg/github/issue_claim_gate.go`, `src/pkg/github/issue_stage_labels.go`, `src/pkg/claims/neverclaim.go` |
| `has-open-pr` | Issues | Open-PR claim guard | An open pull request already closes this issue (`Fixes`/`Closes`/`Resolves #N` in its title or body), checked live before every automated claim: the relay `claim`, a start signal and the contributor lane are refused with `open PR already linked: #<pr>` ([#14804](https://github.com/hivecommons/hive/issues/14804)). | Informational; the refusal follows the open PR, not the label | Hive | Claim admission; removed by the hive when it next admits the issue after the PR merges or closes | Close or merge the PR, or set `scanner.skip_issues_with_open_pr: false` | `src/pkg/github/issue_open_pr_guard.go` |
| `hive-plan`, `hive-design` | Issues | Planning label sweep | `hive-plan` mints/decomposes an epic; `hive-design` asks architect for design first. Only works when planning-from-label is enabled and ACMM >= 5. | Gate when enabled | Human | Planning sweep | Remove label or process plan/design | `planning.plan_from_label`, `planning.plan_labels`, `planning.design_labels` (`src/pkg/config/config.go:400-460`, `src/pkg/planning/issue.go:483-510`) |
| `lgtm` | PRs | Queued auto-merge sweep | Queue label for human/owner merge action; the sweep also requires its normal authorization and skips held/exempt PRs. Adding by hand is not enough if the App approval/authorization is absent. | Permit | Dashboard/merger | Auto-merge sweep | Remove label; head changes can de-queue | `governor.labels.automerge`, default `lgtm` (`src/pkg/config/config.go:3388-3396`, `src/pkg/config/config.go:4952-4957`, `src/pkg/github/automerge/automerge_sweep.go:966-1036`) |
| `reviewer-passed` | PRs | Reviewer lane/escalation reconciliation | Marks one reviewer pass/de-escalation; with no `needs-human`, un-escalates. | Workflow | Reviewer lane/Hive | Reviewer/escalation pass | Remove only if intentionally re-reviewing | Fixed label (`src/pkg/escalation/escalation.go:1185-1205`) |
Expand Down
21 changes: 21 additions & 0 deletions src/docs/operator-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,27 @@ 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 claim guard (`scanner`)

The claim ledger learns of open pull requests only when the eval cycle's
`FetchClaimScan` runs, so a PR opened between scans used to leave its issue
claimable β€” and dispatched again β€” for several minutes. Before every
automated claim (ledger admission, the `hive-open-issue claim` relay request,
the contributor lane) the hive now lists the repo's open PRs live β€” one
listing per repo per sweep, reused for a minute β€” and refuses the claim when
an open PR's title or body closes the issue (`Fixes`/`Closes`/`Resolves #N`,
any case). The refused issue is labelled `has-open-pr` (the label is created
if missing; the hive removes it again when it next admits the issue after the
linked PR merges or closes), the hive logs `claim skipped: open PR already
linked` with the PR number, the relay request fails with `open PR already linked: #<pr>`, and the
contributor queue withholds the issue as `open_pr_claim`. A claimant's own PR
start signal, a renewal of its claim and the cleanup recheck are not refused,
and a failed listing admits the claim (the next ledger scan still covers it).

| Key | Default | Meaning |
|---|---|---|
| `scanner.skip_issues_with_open_pr` | `true` (unset) | Set `false` to claim issues regardless of open linked PRs (ledger-only behaviour). |

## Suspicious-activity alerts (`sentinel`)

The sentinel evaluates open PRs in the watched repos β€” human, bot, or agent β€”
Expand Down
21 changes: 21 additions & 0 deletions src/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ type Config struct {
// `<!-- hive-shared-ci-<n> -->` once incident #n closes
// (hivecommons/hive#12253). Default on.
SharedCIRerun SharedCIRerunConfig `yaml:"shared_ci_rerun,omitempty" json:"shared_ci_rerun,omitempty"`
// Scanner tunes the issue-claim guards in front of automated dispatch.
// Default: an issue an open PR already links is never claimed
// (hivecommons/hive#14804).
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"`
Expand Down Expand Up @@ -7654,6 +7658,23 @@ func (c SharedCIRerunConfig) IsEnabled() bool {
return c.Enabled == nil || *c.Enabled
}

// ScannerConfig tunes the guards every automated issue claim passes.
type ScannerConfig struct {
// SkipIssuesWithOpenPR refuses an automated claim (ledger admission, the
// `claim` relay request, the contributor lane) on an issue an OPEN pull
// request already links with a closing keyword, checked live rather than
// from the eval cycle's claim ledger, and labels the issue `has-open-pr`.
// *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"`
}

// SkipIssuesWithOpenPREnabled reports whether the open-PR claim guard runs;
// unset means on.
func (c ScannerConfig) SkipIssuesWithOpenPREnabled() 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
Expand Down
28 changes: 28 additions & 0 deletions src/pkg/config/scanner_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package config

import (
"testing"

"gopkg.in/yaml.v3"
)

// The open-PR claim guard is on unless
// `scanner.skip_issues_with_open_pr: false` (hivecommons/hive#14804).
func TestScannerSkipIssuesWithOpenPRDefaultsOn(t *testing.T) {
for _, tc := range []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},
} {
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.SkipIssuesWithOpenPREnabled(); got != tc.want {
t.Errorf("%q: SkipIssuesWithOpenPREnabled = %v, want %v", tc.src, got, tc.want)
}
}
}
1 change: 1 addition & 0 deletions src/pkg/config/testdata/agent_runtime_golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@
"good_first_issue": {},
"supersession_sweep": {},
"shared_ci_rerun": {},
"scanner": {},
"upstream_watch": {},
"agent_sandbox": {
"enabled": true,
Expand Down
22 changes: 17 additions & 5 deletions src/pkg/dashboard/contribute_admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,27 @@ func observeExternalDependencies(candidate contributorAdmissionCandidate) conver
// first since cross-repo `owner/repo#N` closing references key on it. A hub
// with no hook wired (tests, or a hive booted without GitHub credentials)
// reports no claims and selection proceeds exactly as before.
//
// The ledger learns of a PR only at the next eval cycle, so a ledger miss
// falls through to the live Dependencies.IssueOpenPRClaimed check
// (hivecommons/hive#14804): a PR opened between scans withholds its issue now
// rather than minutes later, after a second contributor has been handed it.
func (h *ContributeWSHub) issueClaimedByOpenPR(repoFull, repoName string, number int) (ghpkg.IssueClaim, bool) {
if h == nil || h.server == nil || h.server.deps == nil || h.server.deps.IssueClaimed == nil {
if h == nil || h.server == nil || h.server.deps == nil {
return ghpkg.IssueClaim{}, false
}
if claim, ok := h.server.deps.IssueClaimed(repoFull, number); ok {
return claim, true
if lookup := h.server.deps.IssueClaimed; lookup != nil {
if claim, ok := lookup(repoFull, number); ok {
return claim, true
}
if repoName != "" && repoName != repoFull {
if claim, ok := lookup(repoName, number); ok {
return claim, true
}
}
}
if repoName != "" && repoName != repoFull {
if claim, ok := h.server.deps.IssueClaimed(repoName, number); ok {
if live := h.server.deps.IssueOpenPRClaimed; live != nil && repoFull != "" {
if claim, ok := live(repoFull, number); ok {
return claim, true
}
}
Expand Down
Loading