diff --git a/changelog.d/fixed-advisory-digest-foreign-anchor.md b/changelog.d/fixed-advisory-digest-foreign-anchor.md new file mode 100644 index 0000000000..2b61c14bc8 --- /dev/null +++ b/changelog.d/fixed-advisory-digest-foreign-anchor.md @@ -0,0 +1 @@ +- Advisory digest: when the newest digest comment on the advisory issue is not App-authored, re-pin to the App's own digest comment (or create one) and persist that anchor; log the foreign-comment warning once per issue per process (Debug thereafter) with the action taken, and skip the edit when the digest's material content is unchanged for the anchor. diff --git a/src/docs/scale-envelope.md b/src/docs/scale-envelope.md index d778a423ab..8beda804f2 100644 --- a/src/docs/scale-envelope.md +++ b/src/docs/scale-envelope.md @@ -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:5096-5106`, descending `AgeMinutes` within each tier). + (`RankActionableIssues`, `src/pkg/github/client.go:5110-5120`, descending `AgeMinutes` within each tier). - PRs are sorted by **review class then oldest-first within class** (`SortPullRequestsForReview`, `src/pkg/github/review_priority.go:334`; fixes → refactors/docs → tests). diff --git a/src/pkg/github/advisory.go b/src/pkg/github/advisory.go index b19668703a..ae280099f5 100644 --- a/src/pkg/github/advisory.go +++ b/src/pkg/github/advisory.go @@ -13,10 +13,10 @@ import ( "unicode/utf8" gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/crux/logscrub" "github.com/hivecommons/hive/pkg/config" - "github.com/hivecommons/hive/pkg/issueshape" "github.com/hivecommons/hive/pkg/effects" - "github.com/hivecommons/hive/crux/logscrub" + "github.com/hivecommons/hive/pkg/issueshape" ) const ( @@ -497,6 +497,30 @@ func (c *Client) PostAdvisoryDigest(ctx context.Context, repo string, issueNum i commentID := existing.id var author string + if commentID > 0 && !writeThrough { + // Per-anchor unchanged guard (#16439). The listed body the #5507 + // suppression compares against can lag the last PATCH (the comment + // list is eventually consistent), so at steady state the digest was + // still re-edited with identical material content every cycle. + // Compare the pending material fingerprint with the one this process + // last WROTE to this exact anchor and skip the PATCH when they match; + // the periodic write-through still forces a real write. + anchorKey := fmt.Sprintf("%s@%d", key, commentID) + pendingFP := materialDigestFingerprint(digest) + c.advisoryMu.Lock() + unchanged := c.advisoryDigestAnchorHashes[anchorKey] == pendingFP + if unchanged { + c.advisoryDigestHashes = ensureStringMap(c.advisoryDigestHashes) + c.advisoryDigestHashes[key] = digestHash + c.advisoryDigestSkips[key]++ + } + c.advisoryMu.Unlock() + if unchanged { + c.logger.Debug("advisory digest material content unchanged for anchor — skipping edit", + slog.String("repo", repo), slog.Int("issue", issueNum), slog.Int("comment_id", commentID)) + return nil + } + } if commentID > 0 { edited, _, err := c.client.Issues.EditComment(ctx, owner, repoName, int64(commentID), &gh.IssueComment{ Body: gh.Ptr(digest), @@ -528,6 +552,7 @@ func (c *Client) PostAdvisoryDigest(ctx context.Context, repo string, issueNum i return fmt.Errorf("posting advisory digest to %s#%d: %w", repo, issueNum, c.classifyDigestWriteError(ctx, owner, repoName, err)) } author = comment.GetUser().GetLogin() + commentID = int(comment.GetID()) } // Audit the FIRST post and every advisoryDigestAuditInterval-th one, not the @@ -547,6 +572,18 @@ func (c *Client) PostAdvisoryDigest(ctx context.Context, repo string, issueNum i // over from any real write. c.advisoryDigestHashes[key] = digestHash c.advisoryDigestSkips[key] = 0 + if commentID > 0 { + // Persist the anchor this write landed on and its material + // fingerprint (#16439): the next cycle re-pins to it past any + // foreign-authored digest comment and skips the PATCH when the + // rendered content has not materially changed. + if c.advisoryDigestAnchors == nil { + c.advisoryDigestAnchors = make(map[string]int) + } + c.advisoryDigestAnchors[key] = commentID + c.advisoryDigestAnchorHashes = ensureStringMap(c.advisoryDigestAnchorHashes) + c.advisoryDigestAnchorHashes[fmt.Sprintf("%s@%d", key, commentID)] = materialDigestFingerprint(digest) + } c.advisoryDigestPosts[key]++ count := c.advisoryDigestPosts[key] c.advisoryMu.Unlock() @@ -659,7 +696,7 @@ func (c *Client) findDigestCommentDetail(ctx context.Context, owner, repo string PerPage: advisoryDigestCommentsPerPage, }, } - var botAuthored digestComment + var scan digestScan pages := 0 for { comments, resp, err := c.client.Issues.ListComments(ctx, owner, repo, issueNum, opts) @@ -667,15 +704,15 @@ func (c *Client) findDigestCommentDetail(ctx context.Context, owner, repo string // A partial scan that already found a usable fallback still // beats creating a duplicate, so surface the error but keep // whatever the earlier pages turned up. - return botAuthored, err + return c.resolveDigestScan(owner, repo, issueNum, scan.botAuthored, &scan), err } pages++ - found, done := c.scanDigestPage(owner, repo, issueNum, comments, &botAuthored) + found, done := c.scanDigestPage(comments, &scan) if done { - return found, nil + return c.resolveDigestScan(owner, repo, issueNum, found, &scan), nil } if resp == nil || resp.NextPage == 0 { - return botAuthored, nil + return c.resolveDigestScan(owner, repo, issueNum, scan.botAuthored, &scan), nil } if pages >= advisoryDigestScanMaxPages { c.logger.Warn("advisory digest comment scan hit its page cap — treating the digest as absent, which will CREATE a new one", @@ -683,19 +720,33 @@ func (c *Client) findDigestCommentDetail(ctx context.Context, owner, repo string slog.Int("issue", issueNum), slog.Int("pages_scanned", pages), slog.Int("comments_scanned", pages*advisoryDigestCommentsPerPage)) - return botAuthored, nil + return c.resolveDigestScan(owner, repo, issueNum, scan.botAuthored, &scan), nil } opts.Page = resp.NextPage } } +// digestScan carries state across the pages of one digest-comment scan. +type digestScan struct { + // botAuthored is the newest bot-authored-but-not-provably-ours digest + // comment, kept as a fallback when no exact match turns up. + botAuthored digestComment + // foreign is the newest digest comment this App can never edit (human + // authored), recorded so the resolution can report it ONCE per issue + // together with the action taken (#16439) instead of warning on every + // cycle the comment is seen. + foreign digestComment + foreignAuthor string +} + // scanDigestPage applies the authorship rules to one page of comments. It // returns (comment, true) when it has found the definitive digest to edit and // the scan can stop, or (_, false) to continue to the next page. A -// bot-authored-but-not-provably-ours comment is remembered in botAuthored as a -// fallback rather than ending the scan, so a later page can still yield an -// exact match. -func (c *Client) scanDigestPage(owner, repo string, issueNum int, comments []*gh.IssueComment, botAuthored *digestComment) (digestComment, bool) { +// bot-authored-but-not-provably-ours comment is remembered in scan.botAuthored +// as a fallback rather than ending the scan, so a later page can still yield +// an exact match. A foreign-authored digest comment is NEVER selected: it is +// only noted in scan.foreign so the resolution can log it. +func (c *Client) scanDigestPage(comments []*gh.IssueComment, scan *digestScan) (digestComment, bool) { for _, comment := range comments { if !strings.HasPrefix(comment.GetBody(), advisoryDigestPrefix) { continue @@ -718,8 +769,8 @@ func (c *Client) scanDigestPage(owner, repo string, issueNum int, comments []*gh // a misconfigured slug would create a duplicate every cycle. // "First" is now the NEWEST such comment, since the scan runs // newest-first. - if !botAuthored.found() { - *botAuthored = digestCommentFrom(comment) + if !scan.botAuthored.found() { + scan.botAuthored = digestCommentFrom(comment) } continue } @@ -728,18 +779,56 @@ func (c *Client) scanDigestPage(owner, repo string, issueNum int, comments []*gh // hard-forbids an App from editing a foreign-authored comment (403 // "Resource not accessible by integration") no matter what the // installation grants, so adopting it wedges the digest forever - // (kalantar-msb/soft-reflective#1). Skip it; if no bot-authored digest - // comment exists a fresh App-authored one is created and every later + // (kalantar-msb/soft-reflective#1). Never select it; the scan goes on + // to re-pin to an App-authored digest further down the issue, and if + // there is none a fresh App-authored one is created and every later // cycle edits THAT one, so nothing is duplicated per cycle. - c.logger.Warn("skipping advisory digest comment not authored by this App — an installation token can never edit it", - slog.String("repo", owner+"/"+repo), - slog.Int("issue", issueNum), - slog.Int64("comment_id", comment.GetID()), - slog.String("author", login)) + if !scan.foreign.found() { + scan.foreign = digestCommentFrom(comment) + scan.foreignAuthor = login + } } return digestComment{}, false } +// resolveDigestScan finalizes one scan: it reports a foreign-authored digest +// comment ONCE per issue per process at Warn — with the action taken — and +// at Debug thereafter (#16439). Before the fix the Warn fired on every cycle +// the human comment was listed (~every 5 minutes on +// torch-spyre/spyre-inference#406) and never settled, because the comment +// stays on the issue forever. +func (c *Client) resolveDigestScan(owner, repo string, issueNum int, chosen digestComment, scan *digestScan) digestComment { + if !scan.foreign.found() { + return chosen + } + action := "creating a fresh App-authored digest comment" + if chosen.found() { + action = fmt.Sprintf("re-pinned to App-authored digest comment %d", chosen.id) + } + key := fmt.Sprintf("%s/%s#%d", owner, repo, issueNum) + c.advisoryMu.Lock() + warned := c.advisoryForeignDigestWarned[key] + if c.advisoryForeignDigestWarned == nil { + c.advisoryForeignDigestWarned = make(map[string]bool) + } + c.advisoryForeignDigestWarned[key] = true + c.advisoryMu.Unlock() + attrs := []any{ + slog.String("repo", owner+"/"+repo), + slog.Int("issue", issueNum), + slog.Int("comment_id", scan.foreign.id), + slog.String("author", scan.foreignAuthor), + slog.String("action", action), + } + const msg = "skipping advisory digest comment not authored by this App — an installation token can never edit it" + if warned { + c.logger.Debug(msg, attrs...) + } else { + c.logger.Warn(msg, attrs...) + } + return chosen +} + // digestCommentFrom projects the fields the post path needs out of a forge // comment. UpdatedAt is preferred over CreatedAt: the digest comment is EDITED // in place, so the last write — not the original creation — is what the diff --git a/src/pkg/github/advisory_foreign_anchor_test.go b/src/pkg/github/advisory_foreign_anchor_test.go new file mode 100644 index 0000000000..8aebc45f0b --- /dev/null +++ b/src/pkg/github/advisory_foreign_anchor_test.go @@ -0,0 +1,162 @@ +package github + +import ( + "context" + "encoding/json" + "fmt" + "log/slog" + "net/http" + "net/http/httptest" + "sync" + "testing" +) + +// countingHandler is a slog.Handler that counts records per level. +type countingHandler struct { + mu sync.Mutex + counts map[slog.Level]int + msgs []string +} + +func (h *countingHandler) Enabled(context.Context, slog.Level) bool { return true } +func (h *countingHandler) Handle(_ context.Context, r slog.Record) error { + h.mu.Lock() + defer h.mu.Unlock() + if h.counts == nil { + h.counts = map[slog.Level]int{} + } + h.counts[r.Level]++ + h.msgs = append(h.msgs, r.Message) + return nil +} +func (h *countingHandler) WithAttrs([]slog.Attr) slog.Handler { return h } +func (h *countingHandler) WithGroup(string) slog.Handler { return h } + +// digestAnchorMux is digestCommentMux with a per-PATCH counter and a listed +// body that is served unchanged regardless of PATCHes, modelling the +// eventually-consistent comment list. +func digestAnchorMux(org, repo string, comments []map[string]any, patches map[int64]int, created *int) *http.ServeMux { + mux := http.NewServeMux() + mux.HandleFunc(fmt.Sprintf("/repos/%s/%s/issues/10/comments", org, repo), func(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case "GET": + json.NewEncoder(w).Encode(comments) + case "POST": + *created++ + json.NewEncoder(w).Encode(map[string]any{"id": 9999, "user": map[string]any{"login": "hive-app[bot]"}}) + } + }) + mux.HandleFunc(fmt.Sprintf("/repos/%s/%s/issues/comments/", org, repo), func(w http.ResponseWriter, r *http.Request) { + if r.Method == "PATCH" { + var id int64 + fmt.Sscanf(r.URL.Path, fmt.Sprintf("/repos/%s/%s/issues/comments/%%d", org, repo), &id) + patches[id]++ + json.NewEncoder(w).Encode(map[string]any{"id": id, "user": map[string]any{"login": "hive-app[bot]"}}) + } + }) + return mux +} + +// A human-authored digest comment NEWER than the App's own (the +// torch-spyre/spyre-inference#406 shape in #16439): the App re-pins to its +// own older comment rather than creating a duplicate, and the foreign-anchor +// warning is logged once per issue per process with the action taken — later +// cycles demote it to Debug. +func TestPostAdvisoryDigest_ForeignAnchorRepinsToAppCommentWarnsOnce(t *testing.T) { + org, repo := "torch-spyre", "spyre-inference" + patches := map[int64]int{} + created := 0 + server := httptest.NewServer(digestAnchorMux(org, repo, []map[string]any{ + {"id": 5061265231, "body": advisoryDigestPrefix + " — human", "user": map[string]any{"login": "joerunde", "type": "User"}}, + {"id": 4000, "body": advisoryDigestPrefix + " — ours (stale)", "user": map[string]any{"login": "hive-app[bot]", "type": "Bot"}}, + }, patches, &created)) + defer server.Close() + + c := newAppTestClient(t, server, org, repo, "hive-app[bot]") + h := &countingHandler{} + c.logger = slog.New(h) + + for i := 0; i < 3; i++ { + body := fmt.Sprintf("%s — findings v%d", advisoryDigestPrefix, i) + if err := c.PostAdvisoryDigest(context.Background(), repo, 10, body); err != nil { + t.Fatalf("cycle %d: %v", i, err) + } + } + if patches[4000] != 3 { + t.Errorf("expected the App's own comment to be edited on each changed cycle, got %d PATCHes", patches[4000]) + } + if patches[5061265231] != 0 || created != 0 { + t.Errorf("wrong write path: patchedForeign=%d created=%d", patches[5061265231], created) + } + if got := h.counts[slog.LevelWarn]; got != 1 { + t.Errorf("foreign-anchor warning logged %d times, want exactly once; msgs=%v", got, h.msgs) + } + if c.advisoryDigestAnchors["torch-spyre/spyre-inference#10"] != 4000 { + t.Errorf("anchor not persisted: %v", c.advisoryDigestAnchors) + } +} + +// With no App-authored digest on the issue a fresh one is created, and the +// created comment becomes the persisted anchor. +func TestPostAdvisoryDigest_ForeignAnchorCreatesAndPersistsAnchor(t *testing.T) { + org, repo := "torch-spyre", "spyre-inference" + patches := map[int64]int{} + created := 0 + server := httptest.NewServer(digestAnchorMux(org, repo, []map[string]any{ + {"id": 5061265231, "body": advisoryDigestPrefix + " — human", "user": map[string]any{"login": "joerunde", "type": "User"}}, + }, patches, &created)) + defer server.Close() + + c := newAppTestClient(t, server, org, repo, "hive-app[bot]") + h := &countingHandler{} + c.logger = slog.New(h) + + if err := c.PostAdvisoryDigest(context.Background(), repo, 10, advisoryDigestPrefix+" — new"); err != nil { + t.Fatalf("PostAdvisoryDigest: %v", err) + } + if created != 1 || len(patches) != 0 { + t.Errorf("expected exactly one CREATE and no PATCH, got created=%d patches=%v", created, patches) + } + if c.advisoryDigestAnchors["torch-spyre/spyre-inference#10"] != 9999 { + t.Errorf("created comment not persisted as anchor: %v", c.advisoryDigestAnchors) + } + if h.counts[slog.LevelWarn] != 1 { + t.Errorf("want one Warn, got %d", h.counts[slog.LevelWarn]) + } +} + +// Identical material content re-rendered with a fresh timestamp is not +// PATCHed again, even when the listed body still shows an older digest (the +// comment list lagging the last edit). +func TestPostAdvisoryDigest_UnchangedAnchorBodySkipsEdit(t *testing.T) { + org, repo := "testorg", "testrepo" + patches := map[int64]int{} + created := 0 + server := httptest.NewServer(digestAnchorMux(org, repo, []map[string]any{ + {"id": 222, "body": advisoryDigestPrefix + " — older content", "user": map[string]any{"login": "hive-app[bot]", "type": "Bot"}}, + }, patches, &created)) + defer server.Close() + + c := newAppTestClient(t, server, org, repo, "hive-app[bot]") + first := advisoryDigestPrefix + " — 2026-08-31 04:00 UTC\n**Findings:** 2\n- a\n- b" + second := advisoryDigestPrefix + " — 2026-08-31 04:05 UTC\n**Findings:** 2\n- a\n- b" + if err := c.PostAdvisoryDigest(context.Background(), repo, 10, first); err != nil { + t.Fatalf("first: %v", err) + } + if err := c.PostAdvisoryDigest(context.Background(), repo, 10, second); err != nil { + t.Fatalf("second: %v", err) + } + if patches[222] != 1 { + t.Errorf("expected exactly one PATCH for identical material content, got %d", patches[222]) + } + if created != 0 { + t.Errorf("unexpected CREATE: %d", created) + } + third := advisoryDigestPrefix + " — 2026-08-31 04:10 UTC\n**Findings:** 3\n- a\n- b\n- c" + if err := c.PostAdvisoryDigest(context.Background(), repo, 10, third); err != nil { + t.Fatalf("third: %v", err) + } + if patches[222] != 2 { + t.Errorf("changed material content must PATCH again, got %d", patches[222]) + } +} diff --git a/src/pkg/github/client.go b/src/pkg/github/client.go index 32e5c7c002..92823f7cbf 100644 --- a/src/pkg/github/client.go +++ b/src/pkg/github/client.go @@ -511,6 +511,20 @@ type Client struct { // issues:write) still surface within ~an hour instead of never. Guarded by // advisoryMu. advisoryDigestSkips map[string]int + // advisoryDigestAnchors memoizes, per "owner/repo#issue", the ID of the + // App-authored digest comment the last successful write landed on + // (#16439). It is the pin the digest returns to when a foreign-authored + // digest comment sits newer on the issue, and the key for the per-anchor + // last-written material fingerprint in advisoryDigestAnchorHashes, which + // lets an unchanged re-render skip the PATCH even when the listed body is + // stale. Guarded by advisoryMu. + advisoryDigestAnchors map[string]int + advisoryDigestAnchorHashes map[string]string + // advisoryForeignDigestWarned records, per "owner/repo#issue", that the + // "digest comment not authored by this App" condition has already been + // logged at Warn in this process; later cycles log it at Debug (#16439). + // Guarded by advisoryMu. + advisoryForeignDigestWarned map[string]bool // hiveIdentity records which accounts count as "this hive" for the // self-authorization gate (pr_self_authorization.go, #5117). It is optional // — the gate recognises the App bot without it — and exists so an issue