From d3a4edf0676ead4bc5a82feb38ff3edd81ab5d2a Mon Sep 17 00:00:00 2001 From: thatguywob Date: Wed, 28 Oct 2026 14:30:08 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20fix:=20refresh=20tracker=20s?= =?UTF-8?q?napshots=20after=20hive-side=20unblocks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Queue targeted item reads after all five blocked-label removal paths, before posting unblock notes. Wire the client hook through boot to the dashboard refresher so the tracker and governor snapshot converge without an inbound webhook. Hive-Run: hivecommons/hive#14964 Hive-Plan: hivecommons/hive#14952/part-2b Hive-Spec: issue-14964#item-refresh-hook Signed-off-by: thatguywob --- .../fixed-14964-unblock-item-refresh.md | 1 + src/cmd/hive/github_client_config.go | 3 + src/pkg/dashboard/hive_item_refresh.go | 18 +++ src/pkg/dashboard/hive_item_refresh_test.go | 105 ++++++++++++++++ src/pkg/github/client.go | 2 + src/pkg/github/issue_blocked.go | 8 +- src/pkg/github/issue_blocked_test.go | 3 + src/pkg/github/item_refresh_hook.go | 33 +++++ src/pkg/github/item_refresh_hook_test.go | 117 ++++++++++++++++++ src/pkg/github/review_backlog_blocks.go | 2 +- 10 files changed, 287 insertions(+), 5 deletions(-) create mode 100644 changelog.d/fixed-14964-unblock-item-refresh.md create mode 100644 src/pkg/dashboard/hive_item_refresh.go create mode 100644 src/pkg/dashboard/hive_item_refresh_test.go create mode 100644 src/pkg/github/item_refresh_hook.go create mode 100644 src/pkg/github/item_refresh_hook_test.go diff --git a/changelog.d/fixed-14964-unblock-item-refresh.md b/changelog.d/fixed-14964-unblock-item-refresh.md new file mode 100644 index 0000000000..5500bc77d1 --- /dev/null +++ b/changelog.d/fixed-14964-unblock-item-refresh.md @@ -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. diff --git a/src/cmd/hive/github_client_config.go b/src/cmd/hive/github_client_config.go index 6db17e32be..caf40aa608 100644 --- a/src/cmd/hive/github_client_config.go +++ b/src/cmd/hive/github_client_config.go @@ -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 diff --git a/src/pkg/dashboard/hive_item_refresh.go b/src/pkg/dashboard/hive_item_refresh.go new file mode 100644 index 0000000000..3bfcc55b38 --- /dev/null +++ b/src/pkg/dashboard/hive_item_refresh.go @@ -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") +} diff --git a/src/pkg/dashboard/hive_item_refresh_test.go b/src/pkg/dashboard/hive_item_refresh_test.go new file mode 100644 index 0000000000..732155600b --- /dev/null +++ b/src/pkg/dashboard/hive_item_refresh_test.go @@ -0,0 +1,105 @@ +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/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/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 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") + } +} diff --git a/src/pkg/github/client.go b/src/pkg/github/client.go index eb3f4317ce..fc84db99e1 100644 --- a/src/pkg/github/client.go +++ b/src/pkg/github/client.go @@ -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 diff --git a/src/pkg/github/issue_blocked.go b/src/pkg/github/issue_blocked.go index 86f6fe14ff..07d281bf13 100644 --- a/src/pkg/github/issue_blocked.go +++ b/src/pkg/github/issue_blocked.go @@ -435,7 +435,7 @@ func (c *Client) reconcileBlockedTargets(ctx context.Context, targets []blockedI continue } if ySettled { - if err := c.RemoveLabel(ctx, t.repo, t.number, BlockedLabel); err != nil { + if err := c.unblockIssue(ctx, t.repo, t.number); err != nil { res.Errors++ logger.Warn("blocked sweep: unblock failed", "repo", t.repo, "issue", t.number, "error", err) continue @@ -476,7 +476,7 @@ func (c *Client) reconcileBlockedTargets(ctx context.Context, targets []blockedI continue } - if err := c.RemoveLabel(ctx, t.repo, t.number, BlockedLabel); err != nil { + if err := c.unblockIssue(ctx, t.repo, t.number); err != nil { res.Errors++ logger.Warn("blocked sweep: unblock failed", "repo", t.repo, "issue", t.number, "error", err) continue @@ -532,7 +532,7 @@ func liftBlockedMarkerless(ctx context.Context, client *Client, t blockedIssueTa } return false } - if err := client.RemoveLabel(ctx, t.repo, t.number, BlockedLabel); err != nil { + if err := client.unblockIssue(ctx, t.repo, t.number); err != nil { res.Errors++ logger.Warn("blocked sweep: unblock failed", "repo", t.repo, "issue", t.number, "error", err) return false @@ -602,7 +602,7 @@ func liftBlockedDeadline(ctx context.Context, client *Client, t blockedIssueTarg if noted { return } - if err := client.RemoveLabel(ctx, t.repo, t.number, BlockedLabel); err != nil { + if err := client.unblockIssue(ctx, t.repo, t.number); err != nil { res.Errors++ logger.Warn("blocked sweep: unblock failed", "repo", t.repo, "issue", t.number, "error", err) return diff --git a/src/pkg/github/issue_blocked_test.go b/src/pkg/github/issue_blocked_test.go index 6a547cafce..924cab46bf 100644 --- a/src/pkg/github/issue_blocked_test.go +++ b/src/pkg/github/issue_blocked_test.go @@ -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{ @@ -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}, @@ -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{ diff --git a/src/pkg/github/item_refresh_hook.go b/src/pkg/github/item_refresh_hook.go new file mode 100644 index 0000000000..8170f0a239 --- /dev/null +++ b/src/pkg/github/item_refresh_hook.go @@ -0,0 +1,33 @@ +package github + +import "context" + +// ItemRefreshHook requests a targeted re-read after the hive changes an item's +// state. It must enqueue work without blocking on a GitHub read. isPR selects +// the issue or pull-request endpoint for the read. +type ItemRefreshHook func(repo string, number int, isPR bool) + +// SetItemRefreshHook installs (or with nil, removes) the refresh hook. +// 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 requests a refresh as soon as removal succeeds, even if posting +// the explanatory comment later fails. Failed or refused writes never notify. +func (c *Client) unblockIssue(ctx context.Context, repo string, number int) error { + if err := c.RemoveLabel(ctx, repo, number, BlockedLabel); err != nil { + return err + } + if hook := c.itemRefreshHook.Load(); hook != nil && *hook != nil { + (*hook)(repo, number, false) + } + return nil +} diff --git a/src/pkg/github/item_refresh_hook_test.go b/src/pkg/github/item_refresh_hook_test.go new file mode 100644 index 0000000000..d49e76a35a --- /dev/null +++ b/src/pkg/github/item_refresh_hook_test.go @@ -0,0 +1,117 @@ +package github + +import ( + "context" + "net/http" + "net/http/httptest" + "reflect" + "testing" +) + +// 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 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) + } + }) + } +} diff --git a/src/pkg/github/review_backlog_blocks.go b/src/pkg/github/review_backlog_blocks.go index be9ddbeb90..2387a39afd 100644 --- a/src/pkg/github/review_backlog_blocks.go +++ b/src/pkg/github/review_backlog_blocks.go @@ -81,7 +81,7 @@ func (s githubIssueBacklogSink) LiftBlock(ctx context.Context, repo string, ref if ref.Number <= 0 { return fmt.Errorf("review backlog: GitHub item %q has no issue number", ref.ID) } - if err := s.c.RemoveLabel(ctx, repo, ref.Number, BlockedLabel); err != nil { + if err := s.c.unblockIssue(ctx, repo, ref.Number); err != nil { return err } return s.c.CreateIssueCommentBudgeted(ctx, repo, ref.Number, note) From 68be9a977c7510b3e19ac5668f7a32aaf878d800 Mon Sep 17 00:00:00 2001 From: thatguywob Date: Wed, 28 Oct 2026 14:37:52 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=90=9B=20fix:=20account=20for=20block?= =?UTF-8?q?er=20comment=20lookup=20in=20refresh=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: thatguywob --- src/pkg/dashboard/hive_item_refresh_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/pkg/dashboard/hive_item_refresh_test.go b/src/pkg/dashboard/hive_item_refresh_test.go index 732155600b..49975ff114 100644 --- a/src/pkg/dashboard/hive_item_refresh_test.go +++ b/src/pkg/dashboard/hive_item_refresh_test.go @@ -25,6 +25,8 @@ func TestHiveUnblockRefreshesSnapshotsWithoutWebhook(t *testing.T) { 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": @@ -79,9 +81,9 @@ func TestHiveUnblockRefreshesSnapshotsWithoutWebhook(t *testing.T) { 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/pulls/1", "DELETE /repos/o/r/issues/7/labels/blocked", + 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 list or webhook)", paths, want) + t.Fatalf("GitHub calls = %v, want %v (no issue list or webhook)", paths, want) } }