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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-claim-release-rederives-covering-pr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Issue stage records no longer regress to `010-actionable` on a claim release while the duplicate-PR claim ledger holds an open PR that covers the issue: the out-of-cycle stage facts consult the ledger (not only the store's memory) and re-derive `040-covered-by-pr`; a merged `pull_request` webhook now runs close-on-merge for that PR's issues at once instead of waiting for the next reconcile pass; and every eval cycle (cached ones included) backfills store records that sit below 040 under an open ledger claim, so the scanner stops being re-offered issues an open PR already fixes.
32 changes: 19 additions & 13 deletions src/cmd/hive/close_on_merge_sweep.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

import (
Expand Down Expand Up @@ -96,6 +96,23 @@
return fn(client)
}

// closeOnMergeOptionsFor is the policy both the scheduled sweep and the
// webhook-triggered one (closeOnMergeForDelivery) close issues under.
func closeOnMergeOptionsFor(cfg *config.Config, logger *slog.Logger, now func() time.Time) github.CloseOnMergeOptions {
return github.CloseOnMergeOptions{
Identity: hiveIdentity(cfg),
Logger: logger,
TrustedAuthor: func(repo, login string) bool {
decision := trustedAuthorFunc(cfg)(login, cfg.AutoMerge.TrustedAuthors.EffectiveRequireRole())
return decision.Allowed
},
RecheckAfter: cfg.Issues.EffectiveCloseOnMergeRecheck(),
Now: now,
LabelDriven: cfg.RepoLabelDriven,
RepoOnGitHub: cfg.RepoOnGitHub,
}
}

func runCloseOnMergeSweep(ctx context.Context, cfg *config.Config, ghClient *github.Client, ledger *github.ClaimLedger, logger *slog.Logger) {
runCloseOnMergeSweepWith(ctx, cfg, ghClient, ledger, logger, closeOnMergeSched)
}
Expand All @@ -114,19 +131,8 @@
if plan.BudgetChanged && logger != nil {
logger.Info("close-on-merge: sweeps resumed", "budget_mode", plan.Mode.String())
}
opts := github.CloseOnMergeOptions{
Identity: hiveIdentity(cfg),
Logger: logger,
TrustedAuthor: func(repo, login string) bool {
decision := trustedAuthorFunc(cfg)(login, cfg.AutoMerge.TrustedAuthors.EffectiveRequireRole())
return decision.Allowed
},
RecheckAfter: cfg.Issues.EffectiveCloseOnMergeRecheck(),
MergedAfter: plan.MergedAfter,
Now: sched.clock(),
LabelDriven: cfg.RepoLabelDriven,
RepoOnGitHub: cfg.RepoOnGitHub,
}
opts := closeOnMergeOptionsFor(cfg, logger, sched.clock())
opts.MergedAfter = plan.MergedAfter
sweep := ghClient.CloseOnMergeLedgerSweep(ctx, ledger, opts)
msg := "close-on-merge ledger sweep"
if !plan.FullSweep {
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/hive/eval_cycle_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (b *boot) applyWebhookItemsFunc() func([]github.WebhookItemSnapshot) {
}
// A `pull_request` delivery's issue claims land in the ledger and
// drop out of this snapshot on the event (hivecommons/hive#14814).
github.SetPRClaimsHook(prClaimsHookFor(func() *github.ClaimLedger { return getClaimLedger(b.logger) }, b.snapshotPatch, b.logger))
github.SetPRClaimsHook(prClaimsHookFor(func() *github.ClaimLedger { return getClaimLedger(b.logger) }, b.snapshotPatch, b.logger, b.closeOnMergeForDelivery))
}
return func(snaps []github.WebhookItemSnapshot) { b.snapshotPatch.apply(snaps) }
}
Expand Down
4 changes: 4 additions & 0 deletions src/cmd/hive/github_client_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ func (b *boot) applyGitHubClientConfigHooks(client *github.Client) {
// in-progress sweep: a ledger claim whose 🔒 comment was budget-suppressed
// must still read as a claim (claimswire.go, ledgerClaimLookup).
client.SetIssueClaimLedger(ledgerClaimLookup(func() *claims.Ledger { return b.issueClaims }))
// Duplicate-PR claim ledger fallback for the out-of-cycle stage facts:
// a claim release on an issue whose covering PR the ledger already holds
// re-derives 040, not 010 (stage_facts_sink.go, ledgerCoveringPR).
client.SetPRClaimLookup(b.prClaimCoverLookup())
// A marker this hive wrote is live only while its ledger still holds the
// claim (hivecommons/hive#13579, issue_claims.go). The ledger itself is
// built after this client (main.go), so the hive is resolved per call
Expand Down
24 changes: 24 additions & 0 deletions src/cmd/hive/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

import (
Expand Down Expand Up @@ -7207,6 +7207,10 @@
if !budgetDecision.SkipOptional {
applyDuplicatePRGuard(ctx, cfg, ghClient, actionable, logger)
}
// Every cycle, cached ones included: heal issue stage records the
// claim ledger says an open PR covers but the store still holds below
// 040 (a claim release written over the cover between reconcile passes).
reconcileLedgerCoverRecords(ctx, cfg, ghClient, actionable, logger)

// PR review queue (#9590): stamp each PR's rank, priority and reasons
// onto the snapshot so last-actionable.json carries them next to
Expand Down Expand Up @@ -10119,6 +10123,26 @@
// applyDuplicatePRGuard filters issues already claimed by an open hive-authored
// PR out of the actionable set. Failures are logged, never fatal: the guard is
// a safety net, and a broken net must not take the hive down with it.
// reconcileLedgerCoverRecords runs github.BackfillCoveredByLedger with the
// cycle's red+stale valve. It is not an optional step: with webhook-driven
// cached cycles the reconcile pass (and the stage sweep with it) can be
// hours apart, and a record at 010 under an open covering PR re-offers the
// issue to the scanner for all of that time.
func reconcileLedgerCoverRecords(ctx context.Context, cfg *config.Config, ghClient *github.Client, actionable *github.ActionableResult, logger *slog.Logger) {
ledger := getClaimLedger(logger)
if ledger == nil || ledger.Len() == 0 || ghClient == nil {
return
}
backfillCtx, cancel := context.WithTimeout(ctx, issueStageSyncTimeout)
defer cancel()
res := ghClient.BackfillCoveredByLedger(backfillCtx, ledger, claimingPRRedStale(cfg, actionable))
if res.Covered+res.Errors > 0 && logger != nil {
logger.Info("stage backfill: issue records reconciled with the claim ledger",
"checked", res.Checked, "covered", res.Covered, "skipped_red_stale", res.SkippedRedStale,
"skipped_not_open", res.SkippedNotOpen, "errors", res.Errors)
}
}

func applyDuplicatePRGuard(
ctx context.Context,
cfg *config.Config,
Expand Down
65 changes: 63 additions & 2 deletions src/cmd/hive/webhook_pr_claims.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"log/slog"
"time"

Expand All @@ -16,8 +17,10 @@
// enumeration, for the contribute lane (which reads the ledger at offer
// time) and the hub's own agents (which plan from the snapshot) alike. The
// ledger is resolved per call because it loads lazily; a nil patcher skips
// the snapshot step.
func prClaimsHookFor(ledger func() *github.ClaimLedger, patch *snapshotPatcher, logger *slog.Logger) github.PRClaimsHook {
// the snapshot step. onMerged, when non-nil, runs after a MERGED delivery
// whose claims the ledger recorded, so the PR's issues close on the event
// (closeOnMergeForDelivery) rather than on the next reconcile pass.
func prClaimsHookFor(ledger func() *github.ClaimLedger, patch *snapshotPatcher, logger *slog.Logger, onMerged func(github.PRClaimDelivery)) github.PRClaimsHook {
return func(d github.PRClaimDelivery) {
if ledger == nil {
return
Expand All @@ -41,9 +44,45 @@
"issues", d.ClaimedIssues(), "recorded", recorded, "released", released,
"suppressed", suppressed, "source", "webhook")
}
if onMerged != nil && d.State == github.PRStateMerged && recorded > 0 {
onMerged(d)
}
}
}

// closeOnMergeForDelivery runs the close-on-merge ledger sweep for the one
// PR a MERGED `pull_request` delivery names, detached from the webhook
// handler and bounded by closeOnMergeDeliveryTimeout. Before this, an issue
// whose PR merged right after the reconcile pass's sweep waited for the next
// pass — hours, when webhook-driven cached cycles skip the optional sweeps —
// and in the meantime kept its stale stage record (hivecommons/hive#14646
// sat at 010 under a PR merged at 12:54Z). The sweep re-reads the PR and the
// issue live and applies the same identity/trusted-author policy as the
// scheduled one; it does not need the sweep scheduler's cadence.
func (b *boot) closeOnMergeForDelivery(d github.PRClaimDelivery) {
cfg, client := b.cfg, b.ghClient
if cfg == nil || client == nil || !cfg.Issues.CloseOnMergeEnabled() || d.Number <= 0 {
return
}
ledger := getClaimLedger(b.logger)
parent := b.ctx
if parent == nil {
parent = context.Background()
}
go func() {
ctx, cancel := context.WithTimeout(parent, closeOnMergeDeliveryTimeout)
defer cancel()
opts := closeOnMergeOptionsFor(cfg, b.logger, time.Now)
opts.OnlyPRRepo, opts.OnlyPRNumber = d.Repo, d.Number
sweep := client.CloseOnMergeLedgerSweep(ctx, ledger, opts)
logCloseOnMergeResults(b.logger, "close-on-merge webhook sweep", sweep.Results)
}()
}

// closeOnMergeDeliveryTimeout bounds one webhook-triggered close-on-merge
// sweep: a handful of reads and at most a few closes.
const closeOnMergeDeliveryTimeout = 2 * time.Minute

// suppressClaimed re-filters the stored actionable snapshot against the
// claim ledger, copy-on-write, and reports how many issues it dropped. The
// red+stale release valve is not consulted: a claim the webhook just
Expand Down Expand Up @@ -75,3 +114,25 @@
}
return suppressed
}

// prClaimCoverLookup is the github.Client's read of the duplicate-PR claim
// ledger for the stage-fact sink: the ledger's live claim on (repo, issue),
// unless the claiming PR is red and stale on this spoke's last snapshot, in
// which case FilterClaimedIssues would have released the issue and the
// fact sink must not re-cover it. The ledger loads lazily, so it is
// resolved per call.
func (b *boot) prClaimCoverLookup() func(repo string, number int) (github.IssueClaim, bool) {
return func(repo string, number int) (github.IssueClaim, bool) {
ledger := getClaimLedger(b.logger)
claim, ok := ledger.Lookup(repo, number)
if !ok {
return github.IssueClaim{}, false
}
if b.cfg != nil && !claim.MergedPR {
if redStale := claimingPRRedStale(b.cfg, b.lastActionable.Load()); redStale != nil && redStale(claim.PRRepo, claim.PRNumber) {
return github.IssueClaim{}, false
}
}
return claim, true
}
}
35 changes: 32 additions & 3 deletions src/cmd/hive/webhook_pr_claims_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestPRClaimsHookRecordsAndFiltersSnapshot(t *testing.T) {
var patched []int
patch := &snapshotPatcher{target: &target, logger: logger, onPatch: func(n int) { patched = append(patched, n) }}

hook := prClaimsHookFor(func() *github.ClaimLedger { return ledger }, patch, logger)
hook := prClaimsHookFor(func() *github.ClaimLedger { return ledger }, patch, logger, nil)
hook(github.PRClaimDelivery{
Repo: "repo", Number: 14772, Action: "opened", State: github.PRStateOpen, At: now,
Claims: []github.IssueClaim{{Repo: "repo", Issue: 14765, PRNumber: 14772, PRRepo: "repo", PRAuthor: "owner", ExternalAuthor: true, ObservedAt: now}},
Expand Down Expand Up @@ -59,9 +59,38 @@ func TestPRClaimsHookRecordsAndFiltersSnapshot(t *testing.T) {
}

// Nil ledger resolver / nil patcher are safe.
prClaimsHookFor(nil, nil, logger)(github.PRClaimDelivery{Repo: "repo", Number: 1})
prClaimsHookFor(func() *github.ClaimLedger { return nil }, nil, logger)(github.PRClaimDelivery{Repo: "repo", Number: 1})
prClaimsHookFor(nil, nil, logger, nil)(github.PRClaimDelivery{Repo: "repo", Number: 1})
prClaimsHookFor(func() *github.ClaimLedger { return nil }, nil, logger, nil)(github.PRClaimDelivery{Repo: "repo", Number: 1})
if (*snapshotPatcher)(nil).suppressClaimed(ledger, logger) != 0 {
t.Fatal("nil patcher must be a no-op")
}
}

// A MERGED delivery whose claims the ledger recorded hands the PR to the
// close-on-merge path at once; an open delivery and a merged one with
// nothing to record do not (hivecommons/hive#14646 waited on the next
// reconcile pass for its close).
func TestPRClaimsHookRunsCloseOnMergeForMergedDelivery(t *testing.T) {
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
now := time.Now().UTC()
ledger := github.NewClaimLedger(filepath.Join(t.TempDir(), "claims.json"), logger)
var merged []int
hook := prClaimsHookFor(func() *github.ClaimLedger { return ledger }, nil, logger, func(d github.PRClaimDelivery) { merged = append(merged, d.Number) })

claims := []github.IssueClaim{{Repo: "repo", Issue: 14646, PRNumber: 14920, PRRepo: "repo", PRAuthor: "dev", ExternalAuthor: true, ObservedAt: now}}
hook(github.PRClaimDelivery{Repo: "repo", Number: 14920, Action: "opened", State: github.PRStateOpen, At: now, Claims: claims})
if len(merged) != 0 {
t.Fatalf("open delivery triggered close-on-merge: %v", merged)
}
hook(github.PRClaimDelivery{Repo: "repo", Number: 14920, Action: "closed", State: github.PRStateMerged, MergedAt: now, At: now, Claims: claims})
if len(merged) != 1 || merged[0] != 14920 {
t.Fatalf("close-on-merge calls = %v, want [14920]", merged)
}
if c, ok := ledger.Lookup("repo", 14646); !ok || !c.MergedPR {
t.Fatalf("ledger claim after merge = %+v ok=%v, want merged", c, ok)
}
hook(github.PRClaimDelivery{Repo: "repo", Number: 99, Action: "closed", State: github.PRStateMerged, MergedAt: now, At: now})
if len(merged) != 1 {
t.Fatalf("merged delivery with no claims triggered close-on-merge: %v", merged)
}
}
2 changes: 1 addition & 1 deletion src/docs/scale-envelope.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ones to:
2. **Truncation must preserve the prioritization signal, not cut an arbitrary slice.**
This holds today and is easy to assume backwards:
- Issues are sorted by actionable rank tier, then **oldest first** within each tier before any cap is applied
(`RankActionableIssues`, `src/pkg/github/client.go:4767-4777`, descending `AgeMinutes`).
(`RankActionableIssues`, `src/pkg/github/client.go:4772-4782`, descending `AgeMinutes`).
- PRs are sorted by **review class then oldest-first within class**
(`SortPullRequestsForReview`, `src/pkg/github/review_priority.go:334`;
fixes → refactors/docs → tests).
Expand Down
5 changes: 5 additions & 0 deletions src/pkg/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ type Client struct {
// carries no live marker: the 🔒 comment is budgeted and can be
// suppressed, and a claim the ledger holds must still read as a claim.
issueClaimLedger func(repo string, number int) (issueclaim.Claim, bool)
// prClaimLookup resolves the duplicate-PR claim ledger's record for an
// issue (SetPRClaimLookup). An out-of-cycle fact on an issue the ledger
// knows an open PR covers re-derives under that PR (stage_facts_sink.go,
// enrichCoveringPR) instead of falling to 010/020 until the next sweep.
prClaimLookup func(repo string, number int) (IssueClaim, bool)
// issueClaimLedgerHive is the hive= this hub writes on its own markers;
// a live forge marker carrying it while the ledger holds nothing is a
// marker the hub already released (hivecommons/hive#13579).
Expand Down
28 changes: 27 additions & 1 deletion src/pkg/github/close_on_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ type CloseOnMergeOptions struct {
MergedAfter time.Time
// Now is the clock; nil means time.Now.
Now func() time.Time
// OnlyPRRepo/OnlyPRNumber, when OnlyPRNumber is set, restrict the ledger
// sweep to the claims one pull request makes: the webhook path, which
// closes a just-merged PR's issues on the delivery instead of waiting for
// the reconcile pass (whose sweep a cached cycle skips). OnlyPRRepo is
// matched by bare repository name; empty matches any.
OnlyPRRepo string
OnlyPRNumber int
// LabelDriven reports a repo that opted in to label-driven triage; the
// backfill posts no "What to reply" notice there when it reclassifies a
// dedupe checklist as needs-decision. nil means no repo opts in.
Expand All @@ -76,6 +83,25 @@ func (o CloseOnMergeOptions) onGitHub(repo string) bool {
return o.RepoOnGitHub == nil || o.RepoOnGitHub(repo)
}

// wantsPR reports whether the OnlyPR filter admits a claim by (repo, number).
func (o CloseOnMergeOptions) wantsPR(repo string, number int) bool {
if o.OnlyPRNumber <= 0 {
return true
}
if number != o.OnlyPRNumber {
return false
}
return o.OnlyPRRepo == "" || strings.EqualFold(bareRepo(o.OnlyPRRepo), bareRepo(repo))
}

// bareRepo strips an owner/ prefix.
func bareRepo(repo string) string {
if i := strings.LastIndex(repo, "/"); i >= 0 {
return repo[i+1:]
}
return repo
}

func (o CloseOnMergeOptions) recheckAfter() time.Duration {
if o.RecheckAfter <= 0 {
return DefaultCloseOnMergeRecheck
Expand Down Expand Up @@ -194,7 +220,7 @@ func (c *Client) CloseOnMergeLedgerSweep(ctx context.Context, ledger *ClaimLedge
if !claim.MergedPR || claim.PRNumber <= 0 || claim.Issue <= 0 || strings.TrimSpace(claim.Repo) == "" {
continue
}
if claim.Reference {
if claim.Reference || !opts.wantsPR(claim.PRRepo, claim.PRNumber) {
continue
}
if !opts.onGitHub(claim.Repo) || (claim.PRRepo != "" && !opts.onGitHub(claim.PRRepo)) {
Expand Down
37 changes: 37 additions & 0 deletions src/pkg/github/close_on_merge_forge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,40 @@ func TestCloseOnMergeBackfillSkipsOtherForgeRepos(t *testing.T) {
t.Fatalf("results = %+v, want none", results)
}
}

// The webhook path restricts the ledger sweep to the PR that just merged:
// the other merged claims cost no API call and stay unsettled for the
// scheduled sweep.
func TestCloseOnMergeLedgerSweepOnlyPRFilter(t *testing.T) {
defer resetPRDetailCacheForTest(nil, 0)()
f := newLedgerSweepFixture(t, 7, 8)
srv := f.server()
defer srv.Close()
ledger := NewClaimLedger(filepath.Join(t.TempDir(), "ledger.json"), nil)
merged := time.Date(2026, 10, 9, 12, 0, 0, 0, time.UTC)
for _, issue := range []int{7, 8} {
if err := ledger.Record(mergedLedgerClaim(issue, merged)); err != nil {
t.Fatal(err)
}
}
c := newTestClient(t, srv, "o", []string{"r"})
opts := CloseOnMergeOptions{
Identity: HiveIdentity{AppLogin: "hive[bot]"},
Now: func() time.Time { return merged.Add(time.Hour) },
OnlyPRRepo: "r",
OnlyPRNumber: 8,
}

sweep := c.CloseOnMergeLedgerSweep(context.Background(), ledger, opts)
if len(sweep.Results) != 1 || sweep.Results[0].Issue != 8 || sweep.Results[0].Action != CloseOnMergeClosed {
t.Fatalf("results = %+v, want only issue 8 closed", sweep.Results)
}
if got := f.count("pull_get"); got != 1 {
t.Fatalf("PR GETs = %d, want 1", got)
}
for _, claim := range ledger.Claims() {
if settled := !claim.CloseOnMergeSettledAt.IsZero(); settled != (claim.Issue == 8) {
t.Fatalf("claim %d settled=%v", claim.Issue, settled)
}
}
}
12 changes: 12 additions & 0 deletions src/pkg/github/prclaims.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,18 @@ func (c IssueClaim) Settled() bool {
return c.MergedPR && !c.Reference && !c.ExternalAuthor
}

// CoversOpenIssue reports whether the claim is one FilterClaimedIssues
// suppresses the issue for: an OPEN pull request that either closes the
// issue or was opened by someone other than this hive. A merged claim is a
// verification matter, a hive-authored non-closing reference only annotates.
// The red+stale release valve is not part of this predicate.
func (c IssueClaim) CoversOpenIssue() bool {
if c.PRNumber <= 0 || c.MergedPR {
return false
}
return c.ExternalAuthor || !c.Reference
}

// SettledAt is the instant the settled evidence was produced: the merge for
// a merged-PR claim, otherwise the first observation. Zero for an unsettled
// claim.
Expand Down
Loading
Loading