From c56db0343dd2ae1ccc3e35eff3acbee84ae3875f Mon Sep 17 00:00:00 2001 From: Danathar Date: Thu, 5 Nov 2026 17:50:44 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(advisory):=20never=20reopen?= =?UTF-8?q?=20an=20advisory=20issue=20a=20person=20closed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit projectbluefin/common#639: a maintainer closed the advisory digest ("no longer used") on 2026-10-27; the hive's #4167 reuse reopened it the next day with no comment. A person's close now stands, on both the list and the ForgeState-cache path: the hive files a new advisory issue that says which one it replaces and why, and names `advisory.target`. A close by the hive or a bot, or one GitHub does not attribute, is reused as before. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BQYnPGFJx81c8JXR3o1sg1 Signed-off-by: Danathar --- .../fixed-advisory-honour-person-close.md | 1 + src/docs/advisory.md | 11 +- src/pkg/github/advisory.go | 10 +- src/pkg/github/advisory_forgestate.go | 11 +- src/pkg/github/advisory_person_close.go | 93 ++++++++++++ src/pkg/github/advisory_person_close_test.go | 139 ++++++++++++++++++ 6 files changed, 260 insertions(+), 5 deletions(-) create mode 100644 changelog.d/fixed-advisory-honour-person-close.md create mode 100644 src/pkg/github/advisory_person_close.go create mode 100644 src/pkg/github/advisory_person_close_test.go diff --git a/changelog.d/fixed-advisory-honour-person-close.md b/changelog.d/fixed-advisory-honour-person-close.md new file mode 100644 index 0000000000..e27e9cc7f7 --- /dev/null +++ b/changelog.d/fixed-advisory-honour-person-close.md @@ -0,0 +1 @@ +- The hive no longer reopens an advisory issue a person closed. projectbluefin/common#639 was closed by a maintainer ("no longer used") and reopened by the hive the next day. A person's close now stands: the hive files a new advisory issue saying which one it replaces and why, and naming `advisory.target`. A close by a bot or the hive, or one GitHub does not attribute, is reused as before (#4167). diff --git a/src/docs/advisory.md b/src/docs/advisory.md index 0cd45eedc6..b3054f2df7 100644 --- a/src/docs/advisory.md +++ b/src/docs/advisory.md @@ -289,10 +289,17 @@ quietly: post error naming the remedy: enable Issues in the repo's **Settings > General > Features**, or point the hive at the upstream repo. This is not an App-permission problem; the App banner stays down. -- **Someone closed the pinned issue.** The hive reopens the existing issue rather +- **Someone closed the pinned issue.** When a bot (or the hive itself) closed it, + or GitHub does not say who did, the hive reopens the existing issue rather than filing a new one. Filing a new one splits the digest: the hive writes to the new issue while everyone subscribed to the old one watches a comment that - never changes again — which looks exactly like a wedged digest. If a repo + never changes again — which looks exactly like a wedged digest. When a + **person** closed it, that is a decision: the hive leaves it closed and files + a new advisory issue that opens with "This replaces #N, which X closed…", + says the hive does not reopen an issue a person closed, and names + `advisory.target: linear` (with `advisory.linear_issue`) as the way to keep + the digest off GitHub. To go back to the old issue, reopen it and close the + new one. If a repo already has several `🐝 Hive Advisory Report` issues from before this behavior, close the stale duplicates and keep the one the hive is currently updating (the one with the newest digest comment). diff --git a/src/pkg/github/advisory.go b/src/pkg/github/advisory.go index ae280099f5..ffec0fd1d7 100644 --- a/src/pkg/github/advisory.go +++ b/src/pkg/github/advisory.go @@ -230,7 +230,11 @@ func (c *Client) EnsureAdvisoryIssue(ctx context.Context, repo string) (int, err c.logger.Warn("could not create advisory label, issue will be created without it", slog.String("error", labelErr.Error())) } - body := "This issue collects advisory findings from Hive agents.\n\n" + + replaces := "" + if pc, ok := c.takeAdvisoryPersonClose(owner + "/" + repo); ok { + replaces = advisoryReplacesNote(pc) + } + body := replaces + "This issue collects advisory findings from Hive agents.\n\n" + "At lower ACMM levels, some agents work in advisory mode — they analyze code and post findings " + "here but do not create issues or PRs. At higher levels, designated agents (e.g. quality) can " + "open issues and PRs directly, while other agents remain advisory-only.\n\n" + @@ -937,7 +941,9 @@ func (c *Client) findAdvisoryIssueListed(ctx context.Context, owner, repo string // that never updates again, which reads exactly like a wedged digest. So // reopen the most recent closed one and keep posting to the URL people // already follow. Only after this finds nothing does the caller create. - if num, ok := c.findClosedAdvisoryIssue(ctx, owner, repo); ok { + // A person's close is a decision, not an accident: that issue stays + // closed and the caller files a new one (advisory_person_close.go). + if num, ok := c.findClosedAdvisoryIssue(ctx, owner, repo); ok && c.advisoryReusable(ctx, owner, repo, num, nil) { if _, _, err := c.client.Issues.Edit(ctx, owner, repo, num, &gh.IssueRequest{State: gh.Ptr("open")}); err != nil { // Cannot reopen (permissions, locked). Report NOT-FOUND rather than // the closed number: posting a digest to a closed issue would be diff --git a/src/pkg/github/advisory_forgestate.go b/src/pkg/github/advisory_forgestate.go index 19ae09d894..e94a13f4cd 100644 --- a/src/pkg/github/advisory_forgestate.go +++ b/src/pkg/github/advisory_forgestate.go @@ -20,6 +20,9 @@ import ( type forgeStateBinding struct { mu sync.RWMutex advisoryNums map[string]int + // advisoryPersonClosed is the person-closed advisory issue the next + // create replaces (advisory_person_close.go). + advisoryPersonClosed map[string]advisoryPersonClose } // forgeStateReader is the slice of *forgestate.Store the advisory lookup @@ -147,7 +150,13 @@ func (c *Client) confirmAdvisoryIssue(ctx context.Context, store forgeStateReade case issue.GetState() == "open": return n, nil } - // Closed: reuse it, as the list path's fallback 3 does. + // Closed: reuse it, as the list path's fallback 3 does — unless a person + // closed it; then the list path finds nothing open and the caller files + // a new one that says why. + if !c.advisoryReusable(ctx, owner, repo, n, issue) { + c.rememberAdvisoryIssue(full, 0) + return 0, errAdvisoryCacheMiss + } if _, _, err := c.client.Issues.Edit(ctx, owner, repo, n, &gh.IssueRequest{State: gh.Ptr("open")}); err != nil { c.logger.Warn("found closed advisory issue in cache but could not reopen it", slog.String("repo", full), slog.Int("number", n), slog.String("error", err.Error())) diff --git a/src/pkg/github/advisory_person_close.go b/src/pkg/github/advisory_person_close.go new file mode 100644 index 0000000000..d219f5998e --- /dev/null +++ b/src/pkg/github/advisory_person_close.go @@ -0,0 +1,93 @@ +package github + +import ( + "context" + "fmt" + "strings" + "time" + + gh "github.com/google/go-github/v72/github" +) + +// A closed advisory issue is reused (#4167) — but not one a person closed. +// projectbluefin/common#639: a maintainer closed the June advisory digest on +// 2026-10-27 ("no longer used"); the hive reopened it the next day, re-added +// hive/advisory and parked it at 045, with no comment. A person's close is a +// decision, so the hive leaves that issue closed and the caller files a new +// advisory issue whose body says why and names `advisory.target`, the setting +// that moves the digest off GitHub. A close by the hive or another bot, or one +// whose closer GitHub does not report, is reused as before. + +// advisoryPersonClose is the person-closed advisory issue a new one replaces. +type advisoryPersonClose struct { + Number int + ClosedBy string + ClosedAt time.Time +} + +// advisoryClosedByPerson reports whether issue was closed by a person: a +// closer GitHub names that is neither this hive's account nor a bot. +func (c *Client) advisoryClosedByPerson(issue *gh.Issue) (advisoryPersonClose, bool) { + if issue == nil || issue.GetState() != "closed" { + return advisoryPersonClose{}, false + } + closer := issue.GetClosedBy() + login := strings.TrimSpace(closer.GetLogin()) + if login == "" || strings.EqualFold(closer.GetType(), "Bot") || strings.HasSuffix(strings.ToLower(login), "[bot]") || c.isHiveAccount(closer) { + return advisoryPersonClose{}, false + } + return advisoryPersonClose{Number: issue.GetNumber(), ClosedBy: login, ClosedAt: issue.GetClosedAt().Time}, true +} + +// noteAdvisoryPersonClose remembers, per "owner/repo", the person-closed issue +// the next create replaces, so its body can say so. +func (c *Client) noteAdvisoryPersonClose(repo string, pc advisoryPersonClose) { + c.forgeState.mu.Lock() + defer c.forgeState.mu.Unlock() + if c.forgeState.advisoryPersonClosed == nil { + c.forgeState.advisoryPersonClosed = map[string]advisoryPersonClose{} + } + c.forgeState.advisoryPersonClosed[repo] = pc +} + +// takeAdvisoryPersonClose returns and forgets the note for repo. +func (c *Client) takeAdvisoryPersonClose(repo string) (advisoryPersonClose, bool) { + c.forgeState.mu.Lock() + defer c.forgeState.mu.Unlock() + pc, ok := c.forgeState.advisoryPersonClosed[repo] + delete(c.forgeState.advisoryPersonClosed, repo) + return pc, ok +} + +// advisoryReplacesNote is the paragraph a new advisory issue opens with when +// it replaces one a person closed. +func advisoryReplacesNote(pc advisoryPersonClose) string { + when := "" + if !pc.ClosedAt.IsZero() { + when = " on " + pc.ClosedAt.UTC().Format("2006-01-02") + } + return fmt.Sprintf("This replaces #%d, which %s closed%s. Hive does not reopen an issue a person closed, so the digest moved here. "+ + "To keep the digest off GitHub, set `advisory.target: linear` (with `advisory.linear_issue`) in hive.yaml; "+ + "to go back to #%d, reopen it and close this one.\n\n", pc.Number, pc.ClosedBy, when, pc.Number) +} + +// advisoryReusable reads closed advisory issue n and reports whether it may be +// reopened: false, with the note recorded, when a person closed it. +func (c *Client) advisoryReusable(ctx context.Context, owner, repo string, n int, issue *gh.Issue) bool { + if issue == nil { + got, _, err := c.client.Issues.Get(ctx, owner, repo, n) + if err != nil { + // Unknown closer: reuse as before (#4167). + return true + } + issue = got + } + pc, person := c.advisoryClosedByPerson(issue) + if !person { + return true + } + c.noteAdvisoryPersonClose(owner+"/"+repo, pc) + c.logger.Info("advisory issue was closed by a person; not reopening it, a new one will say why", + "repo", owner+"/"+repo, "number", n, "closed_by", pc.ClosedBy) + return false +} diff --git a/src/pkg/github/advisory_person_close_test.go b/src/pkg/github/advisory_person_close_test.go new file mode 100644 index 0000000000..c461330ce1 --- /dev/null +++ b/src/pkg/github/advisory_person_close_test.go @@ -0,0 +1,139 @@ +package github + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "strings" + "sync/atomic" + "testing" + + "github.com/hivecommons/hive/pkg/forgestate" +) + +// projectbluefin/common#639: a maintainer closed the advisory digest +// ("no longer used") on 2026-10-27 and the hive reopened it the next day. A +// person's close is honoured: the issue stays closed and a new advisory issue +// says why. A close by the hive or a bot, or an unknown closer, is reused +// (#4167). +func advisoryPersonCloseMux(t *testing.T, closedBy map[string]any, created *string, reopened *atomic.Int64) *http.ServeMux { + t.Helper() + org, repo, n := "testorg", "testrepo", 639 + mux := http.NewServeMux() + mux.HandleFunc("/search/issues", func(w http.ResponseWriter, r *http.Request) { + _ = json.NewEncoder(w).Encode(map[string]any{"total_count": 0, "items": []any{}}) + }) + mux.HandleFunc(fmt.Sprintf("/repos/%s/%s/labels", org, repo), func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusCreated) + _ = json.NewEncoder(w).Encode(map[string]any{"name": advisoryLabelName}) + }) + mux.HandleFunc(fmt.Sprintf("/repos/%s/%s/issues/%d/labels", org, repo, n), func(w http.ResponseWriter, r *http.Request) { + _ = json.NewEncoder(w).Encode([]map[string]any{{"name": advisoryLabelName}}) + }) + mux.HandleFunc(fmt.Sprintf("/repos/%s/%s/issues/%d", org, repo, n), func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodPatch { + reopened.Add(1) + _ = json.NewEncoder(w).Encode(map[string]any{"number": n, "title": advisoryTitle, "state": "open"}) + return + } + issue := map[string]any{"number": n, "title": advisoryTitle, "state": "closed", "closed_at": "2026-10-27T20:51:28Z"} + if closedBy != nil { + issue["closed_by"] = closedBy + } + _ = json.NewEncoder(w).Encode(issue) + }) + mux.HandleFunc(fmt.Sprintf("/repos/%s/%s/issues", org, repo), func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodPost { + var body struct { + Body string `json:"body"` + } + _ = json.NewDecoder(r.Body).Decode(&body) + *created = body.Body + w.WriteHeader(http.StatusCreated) + _ = json.NewEncoder(w).Encode(map[string]any{"number": 999, "title": advisoryTitle}) + return + } + if r.URL.Query().Get("state") == "closed" { + _ = json.NewEncoder(w).Encode([]map[string]any{{"number": n, "title": advisoryTitle, "state": "closed"}}) + return + } + _ = json.NewEncoder(w).Encode([]map[string]any{}) + }) + return mux +} + +func TestEnsureAdvisoryIssue_HonoursPersonClose(t *testing.T) { + for _, tc := range []struct { + name string + closedBy map[string]any + reuse bool + }{ + {name: "a maintainer closed it", closedBy: map[string]any{"login": "Danathar", "type": "User"}, reuse: false}, + {name: "the hive closed it", closedBy: map[string]any{"login": "hive[bot]", "type": "Bot"}, reuse: true}, + {name: "another bot closed it", closedBy: map[string]any{"login": "stale[bot]", "type": "Bot"}, reuse: true}, + {name: "GitHub names no closer", closedBy: nil, reuse: true}, + } { + t.Run(tc.name, func(t *testing.T) { + var created string + var reopened atomic.Int64 + srv := httptest.NewServer(advisoryPersonCloseMux(t, tc.closedBy, &created, &reopened)) + defer srv.Close() + c := newTestClient(t, srv, "testorg", []string{"testrepo"}) + c.SetAppBotLogin("hive[bot]") + num, err := c.EnsureAdvisoryIssue(context.Background(), "testrepo") + if err != nil { + t.Fatalf("EnsureAdvisoryIssue: %v", err) + } + if tc.reuse { + if num != 639 || reopened.Load() != 1 || created != "" { + t.Fatalf("num=%d reopened=%d created=%q; want #639 reopened, nothing created", num, reopened.Load(), created) + } + return + } + if reopened.Load() != 0 { + t.Fatal("an advisory issue a person closed must not be reopened") + } + if num != 999 { + t.Fatalf("num = %d, want the new issue 999", num) + } + for _, want := range []string{"This replaces #639, which Danathar closed on 2026-10-27", "does not reopen an issue a person closed", "`advisory.target: linear`", "reopen it and close this one"} { + if !strings.Contains(created, want) { + t.Errorf("new advisory body lacks %q:\n%s", want, created) + } + } + if strings.Contains(created, "@Danathar") { + t.Errorf("the body must not ping the person who closed it:\n%s", created) + } + }) + } +} + +// The ForgeState path (a memoized number) honours a person's close too: the +// cached issue is not reopened and the lookup falls through to create. +func TestFindAdvisoryIssue_CachedPersonCloseNotReopened(t *testing.T) { + var created string + var reopened atomic.Int64 + srv := httptest.NewServer(advisoryPersonCloseMux(t, map[string]any{"login": "Danathar", "type": "User"}, &created, &reopened)) + t.Cleanup(srv.Close) + c := newTestClient(t, srv, "testorg", []string{"testrepo"}) + c.SetAppBotLogin("hive[bot]") + store := &fakeForgeState{snapOK: true, snap: forgestate.Snapshot{Repo: "testorg/testrepo", Ready: true}, readOK: false} + useAdvisoryForgeState(t, store) + c.rememberAdvisoryIssue("testorg/testrepo", 639) + + num, err := c.EnsureAdvisoryIssue(context.Background(), "testrepo") + if err != nil || num != 999 { + t.Fatalf("EnsureAdvisoryIssue = %d, %v; want the new issue 999", num, err) + } + if reopened.Load() != 0 || len(store.mutations) != 0 { + t.Fatalf("person-closed cached issue reopened: reopens=%d mutations=%+v", reopened.Load(), store.mutations) + } + if !strings.Contains(created, "This replaces #639") { + t.Fatalf("new advisory body must say what it replaces:\n%s", created) + } + if _, ok := c.takeAdvisoryPersonClose("testorg/testrepo"); ok { + t.Fatal("the note is consumed by the create") + } +}