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-14964-unblock-item-refresh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- The tracker and cached actionable snapshot now queue a targeted re-read when the hive removes `blocked`, without waiting for an inbound GitHub webhook.
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 @@ -328,6 +328,9 @@ func (b *boot) applyGitHubClientDashboardHooks(client *github.Client) {
return
}
client.SetMergeFailureAlertSink(b.dashSrv)
// A hive-side unblock must reach the tracker and cached actionable snapshot
// even when its inbound unlabeled webhook is delayed or lost (#14964).
client.SetItemRefreshHook(b.dashSrv.RefreshHiveItem)
// Typed audit sink (#9587) for the attribution trail: repo and target land
// as first-class audit fields alongside the legacy repo=/number= detail
// pairs. Every hive-created PR/issue is recorded here whether or not the
Expand Down
18 changes: 18 additions & 0 deletions src/pkg/dashboard/hive_item_refresh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dashboard

import "strings"

// RefreshHiveItem invalidates the tracker cache and queues the same targeted
// re-read as an inbound label webhook, without depending on GitHub delivering
// that webhook back to the hive. Reads share its debounce, configuration and
// ApplyWebhookItems path into the governor's cached snapshot.
func (s *Server) RefreshHiveItem(repo string, number int, isPR bool) {
if s == nil || strings.TrimSpace(repo) == "" || number <= 0 {
return
}
s.invalidateStageTrackerForWebhook()
if !s.webhookTrackerRefreshEnabled() {
return
}
s.trackerRefresher().enqueue([]trackerRefreshKey{{repo: repo, number: number, pr: isPR}}, hiveWriteDeliveryPrefix+"item-refresh")
}
107 changes: 107 additions & 0 deletions src/pkg/dashboard/hive_item_refresh_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package dashboard

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"

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

func TestHiveUnblockRefreshesSnapshotsWithoutWebhook(t *testing.T) {
s := newTestServer()
status := minimalPayload()
status.Repos = []FrontendRepo{{Name: "r", Full: "o/r", Issues: 1,
ActionableIssues: []any{github.Issue{Repo: "o/r", Number: 7, State: "open", Labels: []string{"blocked"}}},
}}
s.status = status
var paths []string
ghSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
paths = append(paths, r.Method+" "+r.URL.Path)
w.Header().Set("Content-Type", "application/json")
switch {
case r.Method == http.MethodGet && r.URL.Path == "/repos/o/r/issues/7/comments":
_, _ = w.Write([]byte(`[]`))
case r.Method == http.MethodGet && r.URL.Path == "/repos/o/r/pulls/1":
_, _ = w.Write([]byte(`{"number":1,"state":"closed","merged":true}`))
case r.Method == http.MethodDelete && r.URL.Path == "/repos/o/r/issues/7/labels/blocked":
_, _ = w.Write([]byte(`[]`))
case r.Method == http.MethodPost && r.URL.Path == "/repos/o/r/issues/7/comments":
_, _ = w.Write([]byte(`{"id":1}`))
case r.Method == http.MethodGet && r.URL.Path == "/repos/o/r/issues/7":
_ = json.NewEncoder(w).Encode(map[string]any{"number": 7, "title": "Unblocked", "state": "open",
"labels": []map[string]string{{"name": "kind/bug"}}, "updated_at": time.Now().UTC().Format(time.RFC3339),
"user": map[string]string{"login": "reporter"}})
default:
t.Errorf("unexpected GitHub call %s %s", r.Method, r.URL.Path)
w.WriteHeader(http.StatusNotFound)
}
}))
t.Cleanup(ghSrv.Close)
client := github.NewClientForTest(ghSrv.URL, "o", []string{"r"}, s.logger)
var handed []github.WebhookItemSnapshot
s.deps = &Dependencies{GHClient: client, ApplyWebhookItems: func(snaps []github.WebhookItemSnapshot) {
handed = append(handed, snaps...)
}}
client.SetItemRefreshHook(s.RefreshHiveItem)
timers := &fakeTimers{}
s.webhookRefresh = newWebhookTrackerRefresher()
s.webhookRefresh.newTimer = timers.newTimer

res := github.ReconcileBlockedIssues(context.Background(), client, &github.ActionableResult{
WorkDetailsByRepo: map[string]github.RepoWorkDetails{"o/r": {Issues: []github.RepoWorkIssue{
{Repo: "o/r", Number: 7, Labels: []string{"blocked"}, Bucket: "filtered", Reason: "blocked on o/r#1"},
}}},
}, s.logger)
if res.Unblocked != 1 || res.Errors != 0 {
t.Fatalf("unblock = %+v", res)
}
if !s.stageTrackerCache.stale || s.webhookRefresh.pendingCount() != 1 || timers.count() != 1 {
t.Fatal("unblock must invalidate the tracker and queue a targeted read without a webhook")
}
if len(handed) != 0 {
t.Fatal("read must wait for the debounce window")
}
// Repeated local hints share the webhook refresher's one-read-per-item queue.
s.RefreshHiveItem("o/r", 7, false)
s.FlushWebhookTrackerRefresh()
if len(handed) != 1 || handed[0].Repo != "o/r" || handed[0].Number != 7 || handed[0].IsPR ||
!reflect.DeepEqual(handed[0].Labels, []string{"kind/bug"}) || handed[0].DeliveryID != "hive-write:item-refresh" {
t.Fatalf("governor snapshot patches = %+v", handed)
}
got := s.status.Repos[0].ActionableIssues[0].(github.Issue)
if got.Title != "Unblocked" || !reflect.DeepEqual(got.Labels, []string{"kind/bug"}) {
t.Fatalf("published issue = %+v", got)
}
if !reflect.DeepEqual(status.Repos[0].ActionableIssues[0].(github.Issue).Labels, []string{"blocked"}) {
t.Fatal("refresh mutated the previously published snapshot")
}
if want := []string{"GET /repos/o/r/issues/7/comments", "GET /repos/o/r/pulls/1", "DELETE /repos/o/r/issues/7/labels/blocked",
"POST /repos/o/r/issues/7/comments", "GET /repos/o/r/issues/7"}; !reflect.DeepEqual(paths, want) {
t.Fatalf("GitHub calls = %v, want %v (no issue list or webhook)", paths, want)
}
}

func TestRefreshHiveItemValidationAndDisabledReads(t *testing.T) {
var nilServer *Server
nilServer.RefreshHiveItem("o/r", 7, false)
s := newTestServer()
s.RefreshHiveItem(" ", 7, false)
s.RefreshHiveItem("o/r", 0, false)
if s.stageTrackerGen != 0 || s.webhookRefresh != nil {
t.Fatal("invalid item must not invalidate or queue a read")
}
disabled := false
cfg := &config.Config{}
cfg.Dashboard.StageTracker.WebhookRefresh = &disabled
s.deps = &Dependencies{Config: cfg}
s.RefreshHiveItem("o/r", 7, false)
if !s.stageTrackerCache.stale || s.webhookRefresh != nil {
t.Fatal("disabled targeted reads must still invalidate the tracker cache")
}
}
2 changes: 2 additions & 0 deletions src/pkg/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ type Client struct {
// reviewVerdictHook is told when the relay records a verdict bound to a
// review dispatch (review_verdict_hook.go).
reviewVerdictHook atomic.Pointer[ReviewVerdictHook]
// itemRefreshHook queues targeted reads after hive-side issue unblocks.
itemRefreshHook atomic.Pointer[ItemRefreshHook]
// mergeAuthz gates merge requests from the merge-request watcher against the
// per-agent ACMM merge-policy (CanMerge) + forge-resistance AND the merge
// TARGET (pinned SHA + governor merge-eligible membership; see
Expand Down
3 changes: 3 additions & 0 deletions src/pkg/github/issue_blocked_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ func TestReconcileBlockedIssues(t *testing.T) {
}))
defer srv.Close()
c := testClient(t, srv.URL)
expectIssueRefreshes(t, c, map[int]int{10: 1, 30: 1, 70: 1})
c.appBotLogin = testBot
result := &ActionableResult{WorkDetailsByRepo: map[string]RepoWorkDetails{
"o/r": {Issues: []RepoWorkIssue{
Expand Down Expand Up @@ -508,6 +509,7 @@ func TestReconcileBlockedIssuesRecheckAfter(t *testing.T) {
}))
defer srv.Close()
c := testClient(t, srv.URL)
expectIssueRefreshes(t, c, map[int]int{60: 1})
result := &ActionableResult{WorkDetailsByRepo: map[string]RepoWorkDetails{
"o/r": {Issues: []RepoWorkIssue{
{Repo: "o/r", Number: 60, Labels: []string{"blocked"}, Bucket: workBucketFiltered, Reason: pastReason, RecheckAfter: past},
Expand Down Expand Up @@ -747,6 +749,7 @@ func TestReconcileBlockedIssuesSuperseded(t *testing.T) {
}))
defer srv.Close()
c := testClient(t, srv.URL)
expectIssueRefreshes(t, c, map[int]int{2: 1, 3: 1})
c.appBotLogin = testBot
result := &ActionableResult{WorkDetailsByRepo: map[string]RepoWorkDetails{
"o/r": {Issues: []RepoWorkIssue{
Expand Down
21 changes: 20 additions & 1 deletion src/pkg/github/item_refresh_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ func notifyItemRefresh(repo string, number int, isPR bool) {
}
}

// SetItemRefreshHook installs a client-specific refresh hook. With nil, the
// client falls back to the process-wide hook. A client hook takes precedence
// so an unblock queues only one refresh when both hooks are installed.
// Safe to call before or after the watchers start.
func (c *Client) SetItemRefreshHook(fn ItemRefreshHook) {
if c == nil {
return
}
if fn == nil {
c.itemRefreshHook.Store(nil)
return
}
c.itemRefreshHook.Store(&fn)
}

// unblockIssue removes the `blocked` label from an issue the hive itself
// blocked and tells the ItemRefreshHook, so the cached actionable snapshot and
// the Activity Monitor re-derive the row immediately rather than after the
Expand All @@ -47,6 +62,10 @@ func (c *Client) unblockIssue(ctx context.Context, repo string, number int) erro
if err := c.RemoveLabel(ctx, repo, number, BlockedLabel); err != nil {
return err
}
notifyItemRefresh(repo, number, false)
if hook := c.itemRefreshHook.Load(); hook != nil && *hook != nil {
(*hook)(repo, number, false)
} else {
notifyItemRefresh(repo, number, false)
}
return nil
}
130 changes: 130 additions & 0 deletions src/pkg/github/item_refresh_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
)
Expand Down Expand Up @@ -189,3 +190,132 @@ func TestReplayWebhookPatches_ClosedReadAndBoundedMemory(t *testing.T) {
t.Fatal("newest read should be kept")
}
}

// Used by sweep tests to pin every unblock branch and ensure untouched issues
// and help-wanted removals do not generate refreshes.
func expectIssueRefreshes(t *testing.T, c *Client, want map[int]int) {
t.Helper()
got := map[int]int{}
c.SetItemRefreshHook(func(repo string, number int, isPR bool) {
if repo != "o/r" || isPR {
t.Errorf("refresh = %s#%d pr=%v, want o/r issue", repo, number, isPR)
}
got[number]++
})
t.Cleanup(func() {
if !reflect.DeepEqual(got, want) {
t.Errorf("issue refreshes = %v, want %v", got, want)
}
})
}

func TestItemRefreshHookAfterUnblock(t *testing.T) {
for _, tc := range []struct {
name string
status int
clear bool
want int
}{
{"removed", http.StatusOK, false, 1},
{"already absent", http.StatusNotFound, false, 1},
{"write failed", http.StatusForbidden, false, 0},
{"hook cleared", http.StatusOK, true, 0},
} {
t.Run(tc.name, func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete || r.URL.Path != "/repos/o/r/issues/7/labels/blocked" {
t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(tc.status)
_, _ = w.Write([]byte(`[]`))
}))
defer srv.Close()
c := testClient(t, srv.URL)
calls := 0
c.SetItemRefreshHook(func(repo string, n int, isPR bool) {
calls++
if repo != "o/r" || n != 7 || isPR {
t.Errorf("refresh = %s#%d pr=%v", repo, n, isPR)
}
})
if tc.clear {
c.SetItemRefreshHook(nil)
}
err := c.unblockIssue(context.Background(), "o/r", 7)
if (err != nil) != (tc.status == http.StatusForbidden) {
t.Fatalf("unblock error = %v", err)
}
if calls != tc.want {
t.Fatalf("refresh calls = %d, want %d", calls, tc.want)
}
})
}
var c *Client
c.SetItemRefreshHook(nil)
if err := c.unblockIssue(context.Background(), "o/r", 7); err != ErrNoGitHubClient {
t.Fatalf("nil client error = %v", err)
}
}

func TestClientItemRefreshHookTakesPrecedence(t *testing.T) {
globalCalls, clientCalls := 0, 0
SetItemRefreshHook(func(string, int, bool) { globalCalls++ })
t.Cleanup(func() { SetItemRefreshHook(nil) })
_, c := newLabelMutationServer(t, "agent/reviewer", BlockedLabel)
c.SetItemRefreshHook(func(string, int, bool) { clientCalls++ })
if err := c.unblockIssue(context.Background(), "acme/widgets", 7); err != nil {
t.Fatal(err)
}
if clientCalls != 1 || globalCalls != 0 {
t.Fatalf("refresh calls: client=%d global=%d, want 1 and 0", clientCalls, globalCalls)
}
c.SetItemRefreshHook(nil)
if err := c.unblockIssue(context.Background(), "acme/widgets", 7); err != nil {
t.Fatal(err)
}
if clientCalls != 1 || globalCalls != 1 {
t.Fatalf("refresh calls after clearing client hook: client=%d global=%d, want 1 and 1", clientCalls, globalCalls)
}
}

func TestBacklogLiftRefreshesBeforeComment(t *testing.T) {
for _, failLabel := range []bool{false, true} {
t.Run(map[bool]string{false: "comment fails", true: "label fails"}[failLabel], func(t *testing.T) {
calls := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.Method {
case http.MethodDelete:
if failLabel {
w.WriteHeader(http.StatusForbidden)
return
}
_, _ = w.Write([]byte(`[]`))
case http.MethodPost:
if calls != 1 {
t.Error("refresh must be queued before the comment")
}
w.WriteHeader(http.StatusForbidden)
default:
t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
w.WriteHeader(http.StatusNotFound)
}
}))
defer srv.Close()
c := testClient(t, srv.URL)
c.SetItemRefreshHook(func(string, int, bool) { calls++ })
err := (githubIssueBacklogSink{c: c}).LiftBlock(context.Background(), "o/r", ReviewBacklogRef{Number: 7}, "unblocked")
if err == nil {
t.Fatal("expected write error")
}
want := 1
if failLabel {
want = 0
}
if calls != want {
t.Fatalf("refresh calls = %d, want %d", calls, want)
}
})
}
}
Loading