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-advisory-digest-foreign-anchor.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion src/docs/scale-envelope.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
131 changes: 110 additions & 21 deletions src/pkg/github/advisory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -659,43 +696,57 @@ 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)
if err != nil {
// 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",
slog.String("repo", owner+"/"+repo),
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
Expand All @@ -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
}
Expand All @@ -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
Expand Down
162 changes: 162 additions & 0 deletions src/pkg/github/advisory_foreign_anchor_test.go
Original file line number Diff line number Diff line change
@@ -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])
}
}
Loading
Loading