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-14814-webhook-pr-claims.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- A `pull_request` webhook now records the issues the PR claims (`Fixes #N`, branch name, references) into the duplicate-PR claim ledger and filters the cached actionable snapshot on the event, releases them when the PR is edited to drop them or closes unmerged, and converts them to merged claims on merge; the dispatch gate also refuses an issue the confirming read says is closed. Before, the PR→issue link was only made on the full reconcile pass (~1h apart on a busy spoke), so an owner PR opened with `Fixes #N` between passes left #N offerable and the `/contribute` lane handed it to a contributor minutes later ([#14814](https://github.com/hivecommons/hive/issues/14814)).
3 changes: 3 additions & 0 deletions src/cmd/hive/eval_cycle_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ func (b *boot) applyWebhookItemsFunc() func([]github.WebhookItemSnapshot) {
if b.evalLoop != nil {
b.snapshotPatch.onPatch = b.evalLoop.notePatch
}
// 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))
}
return func(snaps []github.WebhookItemSnapshot) { b.snapshotPatch.apply(snaps) }
}
77 changes: 77 additions & 0 deletions src/cmd/hive/webhook_pr_claims.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"log/slog"
"time"

"github.com/hivecommons/hive/pkg/github"
)

// prClaimsHookFor builds the github.PRClaimsHook the governor installs
// (hivecommons/hive#14814): a `pull_request` webhook's claims go into the
// duplicate-PR claim ledger at once, the ledger is persisted, and the cached
// actionable snapshot is re-filtered through the same FilterClaimedIssues
// the reconcile pass applies — so an issue a just-opened PR fixes leaves
// the offerable set the moment the delivery lands instead of on the next
// 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 {
return func(d github.PRClaimDelivery) {
if ledger == nil {
return
}
l := ledger()
if l == nil {
return
}
recorded, released := l.ApplyPRDelivery(d)
if recorded == 0 && released == 0 {
return
}
if err := l.Save(); err != nil && logger != nil {
logger.Warn("webhook PR claims: failed to persist claim ledger",
"repo", d.Repo, "pr", d.Number, "error", err)
}
suppressed := patch.suppressClaimed(l, logger)
if logger != nil {
logger.Info("webhook PR claims applied to claim ledger",
"repo", d.Repo, "pr", d.Number, "action", d.Action, "pr_state", d.State,
"issues", d.ClaimedIssues(), "recorded", recorded, "released", released,
"suppressed", suppressed, "source", "webhook")
}
}
}

// 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
// recorded is seconds old, and the reconcile pass re-applies the valve with
// live CI state. No stored snapshot is a no-op.
func (p *snapshotPatcher) suppressClaimed(ledger *github.ClaimLedger, logger *slog.Logger) int {
if p == nil || p.target == nil || ledger == nil {
return 0
}
p.mu.Lock()
defer p.mu.Unlock()
cur := p.target.Load()
if cur == nil {
return 0
}
next := cur.Clone()
suppressed := github.FilterClaimedIssues(next, ledger, nil, logger)
if suppressed == 0 {
return 0
}
p.target.Store(next)
if p.logger != nil {
p.logger.Info("webhook PR claims filtered the cached actionable snapshot",
"suppressed", suppressed, "issues", next.Issues.Count,
"snapshot_age", time.Since(next.GeneratedAt).Round(time.Second).String())
}
if p.onPatch != nil {
p.onPatch(suppressed)
}
return suppressed
}
67 changes: 67 additions & 0 deletions src/cmd/hive/webhook_pr_claims_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"io"
"log/slog"
"path/filepath"
"sync/atomic"
"testing"
"time"

"github.com/hivecommons/hive/pkg/github"
)

// hivecommons/hive#14814: the governor's PRClaimsHook must write the ledger,
// persist it, and drop the claimed issue from the cached actionable snapshot
// on the webhook — not on the next enumeration.
func TestPRClaimsHookRecordsAndFiltersSnapshot(t *testing.T) {
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
now := time.Now().UTC()
path := filepath.Join(t.TempDir(), "claims.json")
ledger := github.NewClaimLedger(path, logger)

var target atomic.Pointer[github.ActionableResult]
target.Store(&github.ActionableResult{GeneratedAt: now, Issues: github.IssueResult{
Count: 2, Items: []github.Issue{{Repo: "repo", Number: 14765}, {Repo: "repo", Number: 14766}},
}})
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(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}},
})

if _, ok := ledger.Lookup("repo", 14765); !ok {
t.Fatal("claim not recorded in the ledger")
}
cur := target.Load()
if cur.Issues.Count != 1 || len(cur.Issues.Items) != 1 || cur.Issues.Items[0].Number != 14766 {
t.Fatalf("snapshot after hook = %+v, want only #14766", cur.Issues)
}
if len(patched) != 1 || patched[0] != 1 {
t.Fatalf("onPatch calls = %v, want [1]", patched)
}
// Persisted: a fresh ledger from the same path sees the claim.
reloaded, err := github.LoadClaimLedger(path, logger)
if err != nil {
t.Fatal(err)
}
if _, ok := reloaded.Lookup("repo", 14765); !ok {
t.Fatal("claim not persisted")
}

// A delivery with nothing to apply does not touch the snapshot.
hook(github.PRClaimDelivery{Repo: "repo", Number: 99, Action: "edited", State: github.PRStateOpen, At: now})
if len(patched) != 1 {
t.Fatalf("onPatch calls after no-op delivery = %v", patched)
}

// 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})
if (*snapshotPatcher)(nil).suppressClaimed(ledger, logger) != 0 {
t.Fatal("nil patcher must be a no-op")
}
}
24 changes: 23 additions & 1 deletion src/docs/operator-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,29 @@ between. Two mechanisms close that gap
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.
claim. The gate also refuses an issue the confirming read says is already
**closed** (`issue closed`), so an issue closed between enumerations is
never dispatched from the cached snapshot.
- **Webhook claim linking.** A `pull_request` delivery (opened, reopened,
edited, synchronize, ready-for-review, draft, closed) is parsed with the
same closing-keyword and branch-name grammar and written into the claim
ledger the moment it lands
([#14814](https://github.com/hivecommons/hive/issues/14814);
`src/pkg/github/webhook_pr_claims.go`): an open PR's claims are recorded
(and the cached actionable snapshot re-filtered, so the hub's own agents
stop seeing the issue too), a PR edited to stop naming an issue or closed
without merging releases it, and a merged PR's claims become merged
claims. Before this the link was only made on the full reconcile pass —
about an hour apart on a busy spoke — and an owner PR opened with
`Fixes #N` in between left #N offerable for that long; the `/contribute`
lane handed [#14765](https://github.com/hivecommons/hive/issues/14765) out
nine minutes after #14772 (Fixes #14765) opened. The receiver's response
lists the issues as `claimed_issues`, the governor logs `webhook PR claims
applied to claim ledger` with the PR, state and counts, and the open-PR
listing cache for the repo is invalidated so the pre-dispatch guard
re-lists on its next call. An `issues` closed delivery (or a PR merged
with a closing keyword, which GitHub delivers as the issue's own close)
already evicts the issue from the status snapshot the lane offers from.
- **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
Expand Down
11 changes: 9 additions & 2 deletions src/pkg/dashboard/api_github_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ func (s *Server) registerGitHubWebhookRoutes() {
// events invalidate the matching cached PR state and mark it dirty for the
// next eval cycle, queue a targeted re-read of the named PR or issue for the
// activity monitor (webhook_tracker_refresh.go), and every delivery for a
// managed repo refreshes webhook health. A `pull_request` opened/synchronize/
// reopened delivery also hands the PR to the sentinel (#13379), and the
// managed repo refreshes webhook health. A `pull_request` delivery records
// the issues the PR claims (`Fixes #N`, branch name, references) into the
// duplicate-PR claim ledger on the event (hivecommons/hive#14814), so the
// contribute lane stops offering them without waiting for the reconcile
// pass; opened/synchronize/reopened also hands the PR to the sentinel
// (#13379), and the
// organization-scoped `projects_v2_item` delivery patches the GitHub
// Projects work source cache. Deliveries for repositories this hive does not
// manage are ignored.
Expand Down Expand Up @@ -106,6 +110,9 @@ func (s *Server) handleGitHubWebhook(w http.ResponseWriter, r *http.Request) {
// its own sample (WriterForgeState) when it applies the item patch.
forgestate.RecordEventToPatch(r.Context(), res.Repo, forgestate.WriterCacheInvalidate, receivedAt, time.Now())
resp := map[string]any{"status": "ok", "event": event, "repo": res.Repo, "invalidated": res.Invalidated}
if len(res.ClaimedIssues) > 0 {
resp["claimed_issues"] = res.ClaimedIssues
}
if n := s.scheduleWebhookTrackerRefresh(res, r.Header.Get("X-GitHub-Delivery")); n > 0 {
resp["tracker_refresh"] = n
}
Expand Down
145 changes: 145 additions & 0 deletions src/pkg/dashboard/webhook_pr_claims_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package dashboard

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"time"

ghpkg "github.com/hivecommons/hive/pkg/github"
)

// hivecommons/hive#14814 regression suite: an owner PR opened with
// `Fixes #N` between two reconcile passes left #N offerable for up to an
// hour, and the contribute lane handed #14765 to a contributor nine minutes
// after #14772 (Fixes #14765) opened. The `pull_request` webhook now records
// the claim into the duplicate-PR ledger on the event, and selectTask
// (which reads that ledger through Dependencies.IssueClaimed) stops
// offering the issue at once. An `issues` closed delivery already evicts
// the row from the status payload selectTask offers from; the second test
// pins that path for the same incident.

// webhookClaimFixture wires the dashboard server the way the governor does:
// the webhook receiver's claim hook writes a real ledger, and the contribute
// admission gate reads the same ledger.
func webhookClaimFixture(t *testing.T) (*ContributeWSHub, *Server, *ghpkg.ClaimLedger) {
t.Helper()
const secret = "s3cr3t"
t.Setenv(githubWebhookSecretEnvVar, secret)
hub, s := covK2Hub(t)
// Typed items, as the governor publishes them (the map shape other
// contribute fixtures use is a JSON round-trip artefact).
s.statusMu.Lock()
s.status = &StatusPayload{Repos: []FrontendRepo{{
Name: "dakota", Full: "projectbluefin/dakota",
ActionableIssues: []any{
ghpkg.Issue{Repo: "dakota", Number: 353, Title: "the issue everyone kept fixing", URL: "https://github.com/projectbluefin/dakota/issues/353", Author: "someone"},
ghpkg.Issue{Repo: "dakota", Number: 360, Title: "an unclaimed issue", URL: "https://github.com/projectbluefin/dakota/issues/360", Author: "someone"},
},
}}}
s.statusMu.Unlock()
ghSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// selectTask's trust check reads the contributor's permission;
// the webhook receiver itself must not call GitHub.
if strings.Contains(r.URL.Path, "/collaborators/") {
_ = json.NewEncoder(w).Encode(map[string]any{"permission": "read"})
return
}
t.Errorf("webhook receiver must not call GitHub: %s %s", r.Method, r.URL.Path)
w.WriteHeader(http.StatusInternalServerError)
}))
t.Cleanup(ghSrv.Close)
s.deps.GHClient = ghpkg.NewClientForTest(ghSrv.URL, "projectbluefin", []string{"dakota"}, s.logger)
s.deps.Ctx = context.Background()
timers := &fakeTimers{}
s.webhookRefresh = newWebhookTrackerRefresher()
s.webhookRefresh.newTimer = timers.newTimer

ledger := ghpkg.NewClaimLedger(filepath.Join(t.TempDir(), "claims.json"), s.logger)
ghpkg.SetPRClaimsHook(func(d ghpkg.PRClaimDelivery) { ledger.ApplyPRDelivery(d) })
t.Cleanup(func() { ghpkg.SetPRClaimsHook(nil) })
s.deps.IssueClaimed = ledger.Lookup
return hub, s, ledger
}

func postSignedWebhook(t *testing.T, s *Server, event string, body []byte) map[string]any {
t.Helper()
rec := postGitHubWebhook(s, event, signGitHubWebhookForTest("s3cr3t", body), body)
if rec.Code != http.StatusOK {
t.Fatalf("%s: status = %d (%s)", event, rec.Code, rec.Body.String())
}
var resp map[string]any
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
t.Fatal(err)
}
return resp
}

func TestWebhookPRClaimStopsSelectTaskOffering(t *testing.T) {
hub, s, ledger := webhookClaimFixture(t)

// Baseline: nothing claimed, the first issue is offered.
if msg := hub.selectTask(claimedIssueConn()); msg == nil || msg.Type != "task_assign" || msg.Number != 353 {
t.Fatalf("baseline offer = %+v, want #353", msg)
}

// An owner PR opens with `Fixes #353`. The claim must land before any
// reconcile pass.
body := []byte(`{"action":"opened","repository":{"full_name":"projectbluefin/dakota"},"pull_request":{"number":900,"state":"open","title":"fix it","body":"Fixes #353","html_url":"https://github.com/projectbluefin/dakota/pull/900","user":{"login":"castrojo"},"head":{"ref":"fix-353","sha":"abc"}}}`)
resp := postSignedWebhook(t, s, "pull_request", body)
claimed, _ := resp["claimed_issues"].([]any)
if len(claimed) != 1 || claimed[0] != float64(353) {
t.Fatalf("response claimed_issues = %v, want [353]", resp["claimed_issues"])
}
if c, ok := ledger.Lookup("dakota", 353); !ok || c.PRNumber != 900 || !c.ExternalAuthor {
t.Fatalf("ledger claim = %+v ok=%v, want external claim by PR 900", c, ok)
}

// Fresh contributor connection (the baseline one holds nothing: the
// offer above is not an assignment until accepted) — #353 is withheld.
msg := hub.selectTask(claimedIssueConn())
if msg == nil || msg.Type != "task_assign" {
t.Fatalf("expected task_assign, got %+v", msg)
}
if msg.Number != 353 && msg.Number != 360 {
t.Fatalf("unexpected issue #%d", msg.Number)
}
if msg.Number == 353 {
t.Fatal("#353 offered after the webhook recorded an open PR claiming it")
}

// The PR closes without merging: the claim is released and #353 is
// offerable again.
closed := []byte(`{"action":"closed","repository":{"full_name":"projectbluefin/dakota"},"pull_request":{"number":900,"state":"closed","title":"fix it","body":"Fixes #353","html_url":"https://github.com/projectbluefin/dakota/pull/900","user":{"login":"castrojo"},"head":{"ref":"fix-353","sha":"abc"}}}`)
postSignedWebhook(t, s, "pull_request", closed)
if _, ok := ledger.Lookup("dakota", 353); ok {
t.Fatal("claim must be released when the PR closes unmerged")
}
if msg := hub.selectTask(claimedIssueConn()); msg == nil || msg.Number != 353 {
t.Fatalf("offer after PR closed = %+v, want #353 back", msg)
}
}

func TestWebhookIssueClosedStopsSelectTaskOffering(t *testing.T) {
hub, s, _ := webhookClaimFixture(t)
now := time.Date(2026, 10, 20, 1, 0, 0, 0, time.UTC)
t.Cleanup(ghpkg.ResetWebhookTrackerForTestWithClock(func() time.Time { return now }))

body := []byte(`{"action":"closed","issue":{"number":353,"state":"closed","title":"the issue everyone kept fixing"},"repository":{"full_name":"projectbluefin/dakota"}}`)
postSignedWebhook(t, s, "issues", body)

s.statusMu.RLock()
remaining := len(s.status.Repos[0].ActionableIssues)
s.statusMu.RUnlock()
if remaining != 1 {
t.Fatalf("actionable issues after close = %d, want 1 (#353 evicted)", remaining)
}
msg := hub.selectTask(claimedIssueConn())
if msg == nil || msg.Type != "task_assign" || msg.Number != 360 {
t.Fatalf("offer after #353 closed = %+v, want #360", msg)
}
}
Loading
Loading