From 2c3a105be6172d4b80010df889f34c8a686fba22 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Fri, 30 Oct 2026 21:48:26 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20fold=20remaining=20hive?= =?UTF-8?q?=20comments=20into=20the=20status=20comment=20in=20quiet=20mode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In quiet notification mode the evidence pointer, the unblocked note, the review backlog summary, the contribute-hub claim marker and relayed agent status notes (awaiting-ci, parking, already fixed) become sections of the single edited comment. The unblocked note is idempotent per cause in either mode and skipped when the same merge closes the issue; close-on-merge and the issue-stage lane post one closing comment, not both. The client logs created/edited/suppressed counts hourly. Closes #15918 Signed-off-by: Andrew Anderson --- ...tification-spam-fold-remaining-comments.md | 1 + .../sweeps-and-operations.md | 29 ++ src/docs/scale-envelope.md | 2 +- src/pkg/dashboard/contribute_claims.go | 20 +- src/pkg/github/client.go | 27 ++ src/pkg/github/close_on_merge.go | 30 ++- src/pkg/github/gfi_forge.go | 1 + src/pkg/github/issue_blocked.go | 57 +++- src/pkg/github/issue_request_status_notes.go | 42 +++ src/pkg/github/issue_request_watcher.go | 12 +- src/pkg/github/issue_stage_labels.go | 16 +- src/pkg/github/notification_fold_test.go | 251 ++++++++++++++++++ src/pkg/github/notification_tally.go | 96 +++++++ src/pkg/github/pr_status_comment.go | 28 ++ src/pkg/github/review_backlog.go | 3 +- src/pkg/github/review_evidence_merge.go | 13 + src/pkg/github/review_evidence_read_test.go | 17 +- src/pkg/prstatus/prstatus.go | 17 ++ .../testdata/direct-writes-baseline.txt | 2 +- 19 files changed, 632 insertions(+), 32 deletions(-) create mode 100644 changelog.d/fixed-notification-spam-fold-remaining-comments.md create mode 100644 src/pkg/github/issue_request_status_notes.go create mode 100644 src/pkg/github/notification_fold_test.go create mode 100644 src/pkg/github/notification_tally.go diff --git a/changelog.d/fixed-notification-spam-fold-remaining-comments.md b/changelog.d/fixed-notification-spam-fold-remaining-comments.md new file mode 100644 index 0000000000..61d8854805 --- /dev/null +++ b/changelog.d/fixed-notification-spam-fold-remaining-comments.md @@ -0,0 +1 @@ +- Quiet notification mode (`github.notifications.mode: quiet`, the default) now folds the remaining per-event comments the hive posted into the single edited `` comment: the post-merge evidence pointer, the `hive:unblocked` note (now also idempotent per issue and settled PR in either mode, and skipped when the same merge closes the issue), the review backlog summary, the contribute hub's claim marker and an agent's relayed `hive/awaiting-ci`, `Parking on stage/045…` and `Already fixed` status notes. Closing an issue leaves one closing comment: the close-on-merge sweep and the issue-stage lane no longer both post `Fixed by #N`. The GitHub client logs `github notifications created=N edited=M suppressed=K` once an hour so the effect can be measured ([#15918](https://github.com/hivecommons/hive/issues/15918)). diff --git a/src/docs/operator-reference/sweeps-and-operations.md b/src/docs/operator-reference/sweeps-and-operations.md index c0d34ae582..6095e955cd 100644 --- a/src/docs/operator-reference/sweeps-and-operations.md +++ b/src/docs/operator-reference/sweeps-and-operations.md @@ -546,6 +546,35 @@ no longer reported as a stage regression, and a PR the hive itself is holding — queued behind the merge-train front, being rebased by the train, or waiting on a base refresh — is never marked stalled. +The status comment also carries, in quiet mode, the notes the hive used to +post on issues and merged PRs as comments of their own: + +- the post-merge **evidence pointer** (``), as + an `Evidence:` section — the bundle is stored server-side and linked from + the Activity Monitor; +- the **unblocked** note (``) on an issue whose + blocking PR settled; it is also idempotent per cause in either mode (a + second sweep that sees the same issue and the same settled PR posts + nothing), and is skipped altogether when the same merge is closing the + issue; +- the **review backlog summary** (``); +- a contributor's **claim marker** (``) posted by the + contribute hub — claim readers scan every hive comment body for the + marker, so the section is as authoritative as the comment was; +- an agent's relayed **status notes**: `hive/awaiting-ci: …`, `Parking on + stage/045-needs-decision: …` and `Already fixed …`. Any other relayed + comment — a finding, a triage verdict with evidence, a question — is still + posted as a comment. + +Closing an issue leaves exactly one closing comment: when the issue-stage +lane has already said `Fixed by #N` the close-on-merge sweep closes without +repeating it, and vice versa. + +The client counts what it did and logs one INFO line an hour, +`github notifications created=N edited=M suppressed=K` (cumulative since +start: comments created, comments edited in place, notes folded or found +already present), so the effect of the mode can be charted from the logs. + `normal` restores the previous behaviour: every note is its own comment, every perspective's verdict its own review, every new head its own mention. diff --git a/src/docs/scale-envelope.md b/src/docs/scale-envelope.md index 047ba28cc5..8af628446a 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:4969-4979`, descending `AgeMinutes` within each tier). + (`RankActionableIssues`, `src/pkg/github/client.go:4996-5006`, 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/dashboard/contribute_claims.go b/src/pkg/dashboard/contribute_claims.go index 32c8ee335e..424fdc7013 100644 --- a/src/pkg/dashboard/contribute_claims.go +++ b/src/pkg/dashboard/contribute_claims.go @@ -164,7 +164,7 @@ func (h *ContributeWSHub) recordAgentClaim(ctx context.Context, c *ContributorCo postCtx, cancel := context.WithTimeout(ctx, claimCommentTimeout) defer cancel() body := ghpkg.IssueClaimCommentBody(claim.Identity, claim.StartedAt, claim.ExpiresAt) - if err := commenter.CreateIssueComment(postCtx, repoFull, number, body); err != nil { + if err := postClaimComment(postCtx, commenter, repoFull, number, body); err != nil { h.logger.Warn("[contribute-ws] issue claim comment failed; claim recorded on the lease only", "username", identityOf(c), "task", taskID, "repo", repoFull, "number", number, "error", err) } else { @@ -189,3 +189,21 @@ func (h *ContributeWSHub) recordAgentClaim(ctx context.Context, c *ContributorCo h.setLeaseClaim(identityOf(c), taskID, claim, posted) return claim, posted } + +// claimNoteFolder is the optional seam a commenter offers to deliver a note +// as a section of the item's single hive status comment in quiet +// notification mode (*ghpkg.Client.PostClaimNote). Claim readers scan +// every hive comment body for the `` marker, so the +// marker inside a status section is as authoritative as its own comment. +type claimNoteFolder interface { + PostClaimNote(ctx context.Context, repo string, number int, body string) error +} + +// postClaimComment posts the claim marker comment, folding it into the +// status comment when the commenter can and quiet mode asks for it. +func postClaimComment(ctx context.Context, commenter planIssueCommenter, repo string, number int, body string) error { + if folder, ok := commenter.(claimNoteFolder); ok { + return folder.PostClaimNote(ctx, repo, number, body) + } + return commenter.CreateIssueComment(ctx, repo, number, body) +} diff --git a/src/pkg/github/client.go b/src/pkg/github/client.go index c3bc954ef1..9830d103b5 100644 --- a/src/pkg/github/client.go +++ b/src/pkg/github/client.go @@ -274,6 +274,9 @@ type Client struct { // no-findings review, one hive review per head. nil (nothing wired) // reads as normal, so unwired clients behave as before. quietNotificationsFn func() bool + // notifications counts comments created, edited and suppressed; logged + // hourly so the effect of quiet mode is measurable. + notifications notificationTally // reviewRequireLabelsFn returns review.require_labels, read live so a // Features-tab edit gates the next relay request without a rebuild. nil // or empty means the gate is off. @@ -2879,6 +2882,27 @@ func (c *Client) IssueCommentsContain(ctx context.Context, repo string, number i } } +// issueCommentsContainAny reports whether any comment on repo#number +// contains any of needles, in one pass. +func (c *Client) issueCommentsContainAny(ctx context.Context, repo string, number int, needles ...string) (bool, error) { + if c == nil { + return false, ErrNoGitHubClient + } + owner, repoName := c.splitRepo(repo) + comments, err := c.listIssueComments(ctx, owner, repoName, number) + if err != nil { + return false, err + } + for _, comment := range comments { + for _, needle := range needles { + if needle != "" && strings.Contains(comment.GetBody(), needle) { + return true, nil + } + } + } + return false, nil +} + // CreateIssueComment posts a comment on an issue or PR. The signature mirrors // forge.Forge.CreateIssueComment so escalation callers can swap in a neutral // forge adapter on non-GitHub hives without changing call sites. @@ -2918,6 +2942,9 @@ func (c *Client) CreateIssueComment(ctx context.Context, repo string, number int } return effects.Result{Provenance: comment.GetHTMLURL()}, apiErr }) + if err == nil { + c.noteCommentCreated() + } return err } diff --git a/src/pkg/github/close_on_merge.go b/src/pkg/github/close_on_merge.go index 6a26dc6ecc..7c19b57f9b 100644 --- a/src/pkg/github/close_on_merge.go +++ b/src/pkg/github/close_on_merge.go @@ -605,7 +605,7 @@ func (c *Client) closeIssueForMergedPRClaimWithIssue(ctx context.Context, issueR } return res } - closedMarker, reopenedMarker := c.closeOnMergeMarkers(ctx, owner, repoName, issueNumber, pr.GetNumber()) + closedMarker, reopenedMarker, stageNoted := c.closeOnMergeMarkers(ctx, owner, repoName, issueNumber, pr.GetNumber()) if closedMarker { res.Action, res.Reason = CloseOnMergeAlreadyDone, "marker_present" return res @@ -653,7 +653,12 @@ func (c *Client) closeIssueForMergedPRClaimWithIssue(ctx context.Context, issueR res.Action, res.Reason = CloseOnMergeAwaiting, "needs_confirmation_or_human" return res } - if _, _, err := c.client.Issues.CreateComment(WithRESTCaller(ctx, "hive:close_on_merge"), owner, repoName, issueNumber, &gh.IssueComment{Body: gh.Ptr(closeOnMergeCloseComment(pr))}); err != nil { + // The issue-stage lane may already have said "Fixed by #N" for this + // PR (mergedPRClosedStageMarker) and only failed to close; one closing + // comment per issue is enough (hivecommons/hive#15727 vs #15843). + if stageNoted { + c.noteCommentSuppressed() + } else if _, _, err := c.client.Issues.CreateComment(WithRESTCaller(ctx, "hive:close_on_merge"), owner, repoName, issueNumber, &gh.IssueComment{Body: gh.Ptr(closeOnMergeCloseComment(pr))}); err != nil { res.Action, res.Reason = CloseOnMergeNoop, "close_comment_failed" return res } @@ -916,35 +921,40 @@ func closeOnMergeShortSHA(sha string) string { } func (c *Client) hasCloseOnMergeMarker(ctx context.Context, owner, repo string, number int) bool { - closed, _ := c.closeOnMergeMarkers(ctx, owner, repo, number, 0) + closed, _, _ := c.closeOnMergeMarkers(ctx, owner, repo, number, 0) return closed } // closeOnMergeMarkers reports, in one pass over the issue's comments, whether -// Hive's close-on-merge marker is present and whether the reopened-after-merge -// marker for pr is present. -func (c *Client) closeOnMergeMarkers(ctx context.Context, owner, repo string, number, pr int) (closed, reopened bool) { - reopenedMarker := "" +// Hive's close-on-merge marker is present, whether the reopened-after-merge +// marker for pr is present, and whether the issue-stage lane already left +// its own "Fixed by #pr" closing note (mergedPRClosedStageMarker). +func (c *Client) closeOnMergeMarkers(ctx context.Context, owner, repo string, number, pr int) (closed, reopened, stageNoted bool) { + reopenedMarker, stageMarker := "", "" if pr > 0 { reopenedMarker = closeOnMergeReopenedMarker(pr) + stageMarker = mergedPRClosedStageMarkerFor(pr) } opts := &gh.IssueListCommentsOptions{ListOptions: gh.ListOptions{PerPage: closeOnMergeCommentPageSize}} for { comments, resp, err := c.client.Issues.ListComments(ctx, owner, repo, number, opts) if err != nil { - return closed, reopened + return closed, reopened, stageNoted } for _, comment := range comments { body := comment.GetBody() if strings.Contains(body, CloseOnMergeMarker) { - return true, reopened + return true, reopened, stageNoted } if reopenedMarker != "" && strings.Contains(body, reopenedMarker) { reopened = true } + if stageMarker != "" && strings.Contains(body, stageMarker) { + stageNoted = true + } } if resp == nil || resp.NextPage == 0 { - return closed, reopened + return closed, reopened, stageNoted } opts.Page = resp.NextPage } diff --git a/src/pkg/github/gfi_forge.go b/src/pkg/github/gfi_forge.go index 59bcd0a252..5f1c22d387 100644 --- a/src/pkg/github/gfi_forge.go +++ b/src/pkg/github/gfi_forge.go @@ -41,6 +41,7 @@ func (c *Client) UpsertIssueMarkerComment(ctx context.Context, repo string, numb if _, _, err := c.client.Issues.EditComment(ctx, owner, repoName, cm.GetID(), &gh.IssueComment{Body: gh.Ptr(body)}); err != nil { return res, fmt.Errorf("editing marker comment on %s/%s#%d: %w", owner, repoName, number, err) } + c.noteCommentEdited() return res, nil } if !create || body == "" { diff --git a/src/pkg/github/issue_blocked.go b/src/pkg/github/issue_blocked.go index 7e984c847a..c575bd4259 100644 --- a/src/pkg/github/issue_blocked.go +++ b/src/pkg/github/issue_blocked.go @@ -30,6 +30,7 @@ package github import ( "context" "fmt" + "github.com/hivecommons/hive/pkg/prstatus" "log/slog" "regexp" "sort" @@ -468,9 +469,7 @@ func (c *Client) reconcileBlockedTargets(ctx context.Context, targets []blockedI continue } note := blockedUnblockedMarker + fmt.Sprintf("\nUnblocked: %s#%d (superseding #%d) is no longer open. This issue is actionable again.", prRepo, supersededBy, prNumber) - if err := c.CreateIssueCommentBudgeted(ctx, t.repo, t.number, note); err != nil { - logger.Warn("blocked sweep: unblock note not posted", "repo", t.repo, "issue", t.number, "error", err) - } + c.postUnblockedNote(ctx, t.repo, t.number, note, logger) res.Unblocked++ logger.Info("blocked sweep: issue unblocked", "repo", t.repo, "issue", t.number, "pr", prRepo+"#"+strconv.Itoa(supersededBy), "superseded", prNumber) continue @@ -509,9 +508,7 @@ func (c *Client) reconcileBlockedTargets(ctx context.Context, targets []blockedI continue } note := blockedUnblockedMarker + "\nUnblocked: " + prRepo + "#" + strconv.Itoa(prNumber) + " is no longer open. This issue is actionable again." - if err := c.CreateIssueCommentBudgeted(ctx, t.repo, t.number, note); err != nil { - logger.Warn("blocked sweep: unblock note not posted", "repo", t.repo, "issue", t.number, "error", err) - } + c.postUnblockedNote(ctx, t.repo, t.number, note, logger) res.Unblocked++ logger.Info("blocked sweep: issue unblocked", "repo", t.repo, "issue", t.number, "pr", prRepo+"#"+strconv.Itoa(prNumber)) continue @@ -564,9 +561,7 @@ func liftBlockedMarkerless(ctx context.Context, client *Client, t blockedIssueTa logger.Warn("blocked sweep: unblock failed", "repo", t.repo, "issue", t.number, "error", err) return false } - if err := client.CreateIssueCommentBudgeted(ctx, t.repo, t.number, blockedMarkerlessNote); err != nil { - logger.Warn("blocked sweep: unblock note not posted", "repo", t.repo, "issue", t.number, "error", err) - } + client.postUnblockedNote(ctx, t.repo, t.number, blockedMarkerlessNote, logger) res.Unblocked++ logger.Info("blocked sweep: hive-applied marker-less block lifted", "repo", t.repo, "issue", t.number, "actor", actor) return true @@ -634,13 +629,51 @@ func liftBlockedDeadline(ctx context.Context, client *Client, t blockedIssueTarg logger.Warn("blocked sweep: unblock failed", "repo", t.repo, "issue", t.number, "error", err) return } - if err := client.CreateIssueCommentBudgeted(ctx, t.repo, t.number, blockedDeadlineNote(deadline)); err != nil { - logger.Warn("blocked sweep: unblock note not posted", "repo", t.repo, "issue", t.number, "error", err) - } + client.postUnblockedNote(ctx, t.repo, t.number, blockedDeadlineNote(deadline), logger) res.Unblocked++ logger.Info("blocked sweep: issue unblocked", "repo", t.repo, "issue", t.number, "recheck_after", deadline) } +// postUnblockedNote leaves note on the issue once per cause: a comment +// already carrying this exact note — the sweep saw the issue again before +// the label removal was visible, or a second sweep path lifted the same +// block (hivecommons/hive#15536 ×2 within a minute) — means nothing is +// posted. An issue the hive is closing for the same merge (a close-on-merge +// or issue-stage closing note is present) gets no unblocked note either: +// it is done, not actionable again. In quiet notification mode the note is +// a section of the issue's status comment, so only the first note on an +// issue ever creates a comment. +func (c *Client) postUnblockedNote(ctx context.Context, repo string, number int, note string, logger *slog.Logger) { + owner, name := c.splitRepo(repo) + comments, err := c.listIssueComments(ctx, owner, name, number) + if err != nil { + logger.Warn("blocked sweep: comment lookup before unblock note failed; posting anyway", "repo", repo, "issue", number, "error", err) + } + for _, cm := range comments { + body := cm.GetBody() + switch { + case strings.Contains(body, note): + c.noteCommentSuppressed() + return + case strings.Contains(body, CloseOnMergeMarker), strings.Contains(body, mergedPRClosedStageMarker): + logger.Info("blocked sweep: issue is being closed by its merged PR; unblock note not posted", "repo", repo, "issue", number) + c.noteCommentSuppressed() + return + } + } + if c.notificationsQuiet() { + if created, err := c.UpsertPRStatusSection(ctx, repo, number, prstatus.KeyUnblocked, note); err != nil { + logger.Warn("blocked sweep: unblock note not recorded on status comment", "repo", repo, "issue", number, "error", err) + } else if !created { + c.noteCommentSuppressed() + } + return + } + if err := c.CreateIssueCommentBudgeted(ctx, repo, number, note); err != nil { + logger.Warn("blocked sweep: unblock note not posted", "repo", repo, "issue", number, "error", err) + } +} + // blockedDeadlineNote is the note posted when a recheck-after deadline // lifts the block. func blockedDeadlineNote(deadline string) string { diff --git a/src/pkg/github/issue_request_status_notes.go b/src/pkg/github/issue_request_status_notes.go new file mode 100644 index 0000000000..3e81d51c98 --- /dev/null +++ b/src/pkg/github/issue_request_status_notes.go @@ -0,0 +1,42 @@ +package github + +import ( + "strings" + + "github.com/hivecommons/hive/pkg/prstatus" +) + +// Agent status notes the policies tell an agent to leave as a PR/issue +// comment. Each is a one-line state report — the dashboard already shows +// the same state — so in quiet notification mode it is a section of the +// item's status comment rather than an email to every watcher. +const ( + agentNoteAwaitingCI = "awaiting-ci" // "hive/awaiting-ci: CI pending — sweep will merge when green." + agentNoteParking = "parking" // "Parking on stage/045-needs-decision: …" + agentNoteAlreadyFixed = "already-fixed" // "Already fixed by #N …" before a close +) + +// relayStatusNoteKey classifies a relayed comment body as one of the agent +// status notes, by the prefix the policies prescribe, or returns "" for a +// comment that carries content of its own (a finding, a triage verdict +// with evidence, a question) and must stay a comment. +func relayStatusNoteKey(body string) string { + first := strings.ToLower(strings.TrimSpace(firstLine(body))) + switch { + case strings.HasPrefix(first, "hive/awaiting-ci"): + return prstatus.KeyAgentNotePrefix + agentNoteAwaitingCI + case strings.HasPrefix(first, "parking on "), strings.HasPrefix(first, "parked on "): + return prstatus.KeyAgentNotePrefix + agentNoteParking + case strings.HasPrefix(first, "already fixed"): + return prstatus.KeyAgentNotePrefix + agentNoteAlreadyFixed + } + return "" +} + +func firstLine(s string) string { + s = strings.TrimLeft(s, "\r\n\t ") + if i := strings.IndexByte(s, '\n'); i >= 0 { + return s[:i] + } + return s +} diff --git a/src/pkg/github/issue_request_watcher.go b/src/pkg/github/issue_request_watcher.go index 65df5c0fba..56eecbde0b 100644 --- a/src/pkg/github/issue_request_watcher.go +++ b/src/pkg/github/issue_request_watcher.go @@ -659,7 +659,17 @@ func (c *Client) handleOneIssueRequest(ctx context.Context, path string, nowFn f parkBlocked = true } } - err = c.CreateIssueCommentBudgeted(ctx, req.Repo, req.Number, body) + if key := relayStatusNoteKey(body); key != "" && c.notificationsQuiet() { + // A one-line state note (awaiting-ci, parking, already fixed) + // is a section of the item's status comment in quiet mode. + var created bool + created, err = c.UpsertPRStatusSection(ctx, req.Repo, req.Number, key, body) + if err == nil && !created { + c.noteCommentSuppressed() + } + } else { + err = c.CreateIssueCommentBudgeted(ctx, req.Repo, req.Number, body) + } if errors.Is(err, ErrIssueCommentBudget) { c.denyIssueRequest(path, req, "comment refused: "+err.Error()+"; this issue already received its daily quota of hive comments, nothing more is needed on it today", nowFn) return diff --git a/src/pkg/github/issue_stage_labels.go b/src/pkg/github/issue_stage_labels.go index 677ed29b6c..dfd5365b1a 100644 --- a/src/pkg/github/issue_stage_labels.go +++ b/src/pkg/github/issue_stage_labels.go @@ -653,6 +653,11 @@ const mergedPRPartialStageMarker = "", mergedPRClosedStageMarker, pr) +} + func handleMergedPRIssueStage(ctx context.Context, client *Client, in IssueStageInput, logger *slog.Logger) (closed, commented bool) { if client == nil || client.client == nil || in.Claim == nil || !in.Claim.MergedPR || in.Claim.PRNumber <= 0 { return false, false @@ -686,12 +691,17 @@ func handleMergedPRIssueStage(ctx context.Context, client *Client, in IssueStage if !in.Claim.Closing { return false, false } - marker := fmt.Sprintf("%s pr=%d -->", mergedPRClosedStageMarker, in.Claim.PRNumber) - alreadyCommented, err := client.IssueCommentsContain(ctx, in.Repo, in.Number, marker) + marker := mergedPRClosedStageMarkerFor(in.Claim.PRNumber) + // Close-on-merge's own "Fixed by #N" (CloseOnMergeMarker) counts as the + // closing comment too: one per issue, never both (hivecommons/hive#15727 + // vs #15843). + alreadyCommented, err := client.issueCommentsContainAny(ctx, in.Repo, in.Number, marker, CloseOnMergeMarker) if err != nil && logger != nil { logger.Warn("issue stage labels: could not check merged full-fix PR close comment; posting a fresh note", "repo", in.Repo, "issue", in.Number, "pr", in.Claim.PRNumber, "error", err) } - if !alreadyCommented { + if alreadyCommented { + client.noteCommentSuppressed() + } else { body := fmt.Sprintf("%s\nFixed by #%d (merged). Closing because Hive's PR claim metadata says this PR was the full fix.", marker, in.Claim.PRNumber) if strings.TrimSpace(in.Claim.PRURL) != "" { body = fmt.Sprintf("%s\nFixed by #%d (%s, merged). Closing because Hive's PR claim metadata says this PR was the full fix.", marker, in.Claim.PRNumber, in.Claim.PRURL) diff --git a/src/pkg/github/notification_fold_test.go b/src/pkg/github/notification_fold_test.go new file mode 100644 index 0000000000..fc33b4c200 --- /dev/null +++ b/src/pkg/github/notification_fold_test.go @@ -0,0 +1,251 @@ +package github + +import ( + "bytes" + "context" + "encoding/json" + "log/slog" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/prstatus" +) + +// Quiet mode: the post-merge evidence pointer is a section of the PR's +// status comment. With a status comment already there, nothing is created; +// a replayed merge finds the marker inside it and neither edits nor posts. +func TestRecordReviewEvidenceMergeQuietFoldsIntoStatusComment(t *testing.T) { + t0 := time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC) + root := withEvidenceRoot(t) + writeTestEvidence(t, root, "h1", "", t0) + f := evidenceMergeFixture{head: "h1", comments: []string{prstatus.Render(nil)}} + c := evidenceMergeClient(t, &f, &ReviewEvidenceSettings{Enabled: true}) + c.SetNotificationsQuiet(func() bool { return true }) + + c.RecordPRMergedAudit("o/r", 7, "", "m1", PRAuditPathSweep) + if f.posts != 0 || f.edits != 1 { + t.Fatalf("posts=%d edits=%d, want the status comment edited and nothing created", f.posts, f.edits) + } + section, ok := prstatus.Get(f.comments[0], prstatus.KeyEvidence) + if !ok || !strings.Contains(section, ReviewEvidenceMarker) || !strings.Contains(section, "Evidence bundle") { + t.Fatalf("evidence section = %q, %v", section, ok) + } + _, _, suppressed := c.NotificationCounts() + if suppressed != 1 { + t.Fatalf("suppressed=%d, want 1", suppressed) + } + + c.RecordPRMergedAudit("o/r", 7, "", "m1", PRAuditPathSweep) + if f.posts != 0 || f.edits != 1 { + t.Fatalf("replay: posts=%d edits=%d, want no further write", f.posts, f.edits) + } +} + +// Quiet mode without a status comment yet: the one comment created is the +// status comment, carrying the evidence section. Normal mode posts the +// standalone marker comment as before. +func TestRecordReviewEvidenceMergeQuietCreatesStatusCommentOnce(t *testing.T) { + t0 := time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC) + for _, quiet := range []bool{true, false} { + root := withEvidenceRoot(t) + writeTestEvidence(t, root, "h1", "", t0) + f := evidenceMergeFixture{head: "h1"} + c := evidenceMergeClient(t, &f, &ReviewEvidenceSettings{Enabled: true}) + c.SetNotificationsQuiet(func() bool { return quiet }) + c.RecordPRMergedAudit("o/r", 7, "", "m1", PRAuditPathSweep) + if f.posts != 1 || len(f.comments) != 1 { + t.Fatalf("quiet=%v: posts=%d comments=%d, want exactly one", quiet, f.posts, len(f.comments)) + } + if prstatus.IsStatusComment(f.comments[0]) != quiet { + t.Fatalf("quiet=%v: comment %q", quiet, f.comments[0]) + } + if !strings.Contains(f.comments[0], ReviewEvidenceMarker) { + t.Fatalf("quiet=%v: marker missing from %q", quiet, f.comments[0]) + } + } +} + +// An unblocked note is posted once per cause: the second sweep that sees +// the same issue and the same settled PR finds the note and posts nothing +// (hivecommons/hive#15536 ×2 within a minute). In quiet mode the note is a +// section of the issue's status comment, so a second, different cause edits +// that comment rather than creating another. +func TestPostUnblockedNoteIdempotentPerCause(t *testing.T) { + note := blockedUnblockedMarker + "\nUnblocked: o/r#9 is no longer open. This issue is actionable again." + other := blockedUnblockedMarker + "\nUnblocked: o/r#12 is no longer open. This issue is actionable again." + logger := slog.New(slog.NewTextHandler(&bytes.Buffer{}, nil)) + ctx := context.Background() + + t.Run("normal", func(t *testing.T) { + s := &statusCommentServer{} + c := testClient(t, s.start(t).URL) + c.postUnblockedNote(ctx, "o/r", 5, note, logger) + c.postUnblockedNote(ctx, "o/r", 5, note, logger) + if s.creates != 1 || s.edits != 0 { + t.Fatalf("creates=%d edits=%d, want one comment", s.creates, s.edits) + } + if _, _, suppressed := c.NotificationCounts(); suppressed != 1 { + t.Fatalf("suppressed=%d, want 1", suppressed) + } + }) + t.Run("quiet", func(t *testing.T) { + s := &statusCommentServer{} + c := testClient(t, s.start(t).URL) + c.SetNotificationsQuiet(func() bool { return true }) + c.postUnblockedNote(ctx, "o/r", 5, note, logger) + c.postUnblockedNote(ctx, "o/r", 5, note, logger) + c.postUnblockedNote(ctx, "o/r", 5, other, logger) + if s.creates != 1 || s.edits != 1 { + t.Fatalf("creates=%d edits=%d, want one status comment, edited once", s.creates, s.edits) + } + body, _ := s.comments[0]["body"].(string) + section, ok := prstatus.Get(body, prstatus.KeyUnblocked) + if !ok || !strings.Contains(section, "o/r#12") || strings.Contains(section, "o/r#9") { + t.Fatalf("unblocked section = %q, %v", section, ok) + } + }) + t.Run("issue being closed by the merge", func(t *testing.T) { + s := &statusCommentServer{comments: []map[string]any{{"id": 1, "body": CloseOnMergeMarker + "\nFixed by #9 (merged abc). Closing."}}} + c := testClient(t, s.start(t).URL) + c.postUnblockedNote(ctx, "o/r", 5, note, logger) + if s.creates != 0 || s.edits != 0 { + t.Fatalf("creates=%d edits=%d, want nothing on an issue the merge closes", s.creates, s.edits) + } + }) +} + +// Relayed agent status notes fold into the status comment in quiet mode; +// anything with content of its own is still a comment. +func TestRelayStatusNoteKey(t *testing.T) { + tests := map[string]string{ + "hive/awaiting-ci: CI pending — sweep will merge when green.": prstatus.KeyAgentNotePrefix + agentNoteAwaitingCI, + " hive/awaiting-ci: held for human review. Green CI will not merge this.": prstatus.KeyAgentNotePrefix + agentNoteAwaitingCI, + "Parking on stage/045-needs-decision: Risk: schema change\n\nDetails follow.": prstatus.KeyAgentNotePrefix + agentNoteParking, + "Already fixed by #14 (merged). Closing as not planned.": prstatus.KeyAgentNotePrefix + agentNoteAlreadyFixed, + "Verified: the bug reproduces on v5 at abc123.": "", + "": "", + } + for body, want := range tests { + if got := relayStatusNoteKey(body); got != want { + t.Errorf("relayStatusNoteKey(%q) = %q, want %q", body, got, want) + } + } +} + +// The relay comment path in quiet mode: an awaiting-ci note becomes a +// status section; a second one for a new head edits it. +func TestPostNoteOrFoldQuietEditsNormalCreates(t *testing.T) { + ctx := context.Background() + s := &statusCommentServer{} + c := testClient(t, s.start(t).URL) + c.SetNotificationsQuiet(func() bool { return true }) + key := prstatus.KeyAgentNotePrefix + agentNoteAwaitingCI + if created, err := c.PostNoteOrFold(ctx, "o/r", 3, key, "hive/awaiting-ci: CI pending — sweep will merge when green."); err != nil || !created { + t.Fatalf("first: created=%v err=%v", created, err) + } + if created, err := c.PostNoteOrFold(ctx, "o/r", 3, key, "hive/awaiting-ci: held for human review."); err != nil || created { + t.Fatalf("second: created=%v err=%v", created, err) + } + if s.creates != 1 || s.edits != 1 { + t.Fatalf("creates=%d edits=%d", s.creates, s.edits) + } + created, edited, suppressed := c.NotificationCounts() + if created != 1 || edited != 1 || suppressed != 1 { + t.Fatalf("counts created=%d edited=%d suppressed=%d", created, edited, suppressed) + } + + s2 := &statusCommentServer{} + c2 := testClient(t, s2.start(t).URL) + for i := 0; i < 2; i++ { + if _, err := c2.PostNoteOrFold(ctx, "o/r", 3, key, "hive/awaiting-ci: CI pending"); err != nil { + t.Fatal(err) + } + } + if s2.creates != 2 || s2.edits != 0 { + t.Fatalf("normal mode: creates=%d edits=%d, want two comments", s2.creates, s2.edits) + } +} + +// The hourly line: the first bump logs the baseline, bumps within the hour +// stay silent, the first bump after an hour logs again with the totals. +func TestNotificationTallyLogsHourly(t *testing.T) { + now := time.Date(2026, 10, 20, 12, 0, 0, 0, time.UTC) + orig := notificationTallyNow + notificationTallyNow = func() time.Time { return now } + t.Cleanup(func() { notificationTallyNow = orig }) + + var buf bytes.Buffer + c := &Client{logger: slog.New(slog.NewTextHandler(&buf, nil))} + c.noteCommentCreated() + c.noteCommentEdited() + c.noteCommentSuppressed() + if n := strings.Count(buf.String(), "github notifications"); n != 1 { + t.Fatalf("log lines within the hour = %d, want 1:\n%s", n, buf.String()) + } + now = now.Add(notificationTallyLogEvery) + c.noteCommentSuppressed() + out := buf.String() + if n := strings.Count(out, "github notifications"); n != 2 { + t.Fatalf("log lines after an hour = %d, want 2:\n%s", n, out) + } + if !strings.Contains(out, "created=1 edited=1 suppressed=2") { + t.Fatalf("totals missing:\n%s", out) + } +} + +// The issue-stage lane already left its "Fixed by #42" closing note; the +// close-on-merge sweep closes the issue without a second one +// (hivecommons/hive#15727 vs #15843). +func TestCloseOnMergeSkipsSecondClosingComment(t *testing.T) { + defer resetPRDetailCacheForTest(nil, 0)() + var patched bool + var posts int + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch { + case r.Method == "GET" && r.URL.Path == "/repos/o/r/issues": + _ = json.NewEncoder(w).Encode([]map[string]any{ + {"number": 7, "state": "open", "labels": []map[string]string{{"name": CoveredByPRLabel}}}, + }) + case r.Method == "GET" && r.URL.Path == "/repos/o/r/issues/7/comments": + _ = json.NewEncoder(w).Encode([]map[string]string{ + {"body": "covered by PR #42"}, + {"body": mergedPRClosedStageMarkerFor(42) + "\nFixed by #42 (merged). Closing because Hive's PR claim metadata says this PR was the full fix."}, + }) + case r.Method == "GET" && r.URL.Path == "/repos/o/r/issues/7/timeline": + _ = json.NewEncoder(w).Encode([]map[string]any{}) + case r.Method == "GET" && r.URL.Path == "/repos/o/r/issues/7": + _ = json.NewEncoder(w).Encode(map[string]any{"number": 7, "state": "open", "labels": []map[string]string{}}) + case r.Method == "GET" && r.URL.Path == "/repos/o/r/pulls/42": + _ = json.NewEncoder(w).Encode(map[string]any{ + "number": 42, "title": "fix", "body": "unrelated", "merged": true, + "merged_at": "2026-10-09T12:00:00Z", "merge_commit_sha": "abcdef0123456789", + "user": map[string]string{"login": "hive[bot]"}, + "head": map[string]string{"ref": "scanner/other", "sha": "headsha"}, + "base": map[string]any{"repo": map[string]string{"full_name": "o/r"}}, + }) + case r.Method == "POST" && r.URL.Path == "/repos/o/r/issues/7/comments": + posts++ + _ = json.NewEncoder(w).Encode(map[string]any{"id": 1}) + case r.Method == "PATCH" && r.URL.Path == "/repos/o/r/issues/7": + patched = true + _ = json.NewEncoder(w).Encode(map[string]any{"number": 7, "state": "closed"}) + case r.Method == "DELETE" && strings.Contains(r.URL.Path, "/labels/"): + w.WriteHeader(http.StatusNotFound) + default: + t.Fatalf("unexpected request %s %s", r.Method, r.URL.Path) + } + })) + defer srv.Close() + c := newTestClient(t, srv, "o", []string{"r"}) + results := c.CloseOnMergeBackfill(context.Background(), CloseOnMergeOptions{Identity: HiveIdentity{AppLogin: "hive[bot]"}}) + if len(results) != 1 || results[0].Action != CloseOnMergeClosed { + t.Fatalf("results = %+v, want issue closed", results) + } + if !patched || posts != 0 { + t.Fatalf("patched=%v posts=%d, want closed with no second closing comment", patched, posts) + } +} diff --git a/src/pkg/github/notification_tally.go b/src/pkg/github/notification_tally.go new file mode 100644 index 0000000000..e7e700999e --- /dev/null +++ b/src/pkg/github/notification_tally.go @@ -0,0 +1,96 @@ +package github + +import ( + "log/slog" + "sync" + "sync/atomic" + "time" +) + +// notificationTallyLogEvery is how often the client logs its notification +// counters. One INFO line an hour is enough to chart the effect of +// github.notifications.mode without a metrics pipeline. +const notificationTallyLogEvery = time.Hour + +// notificationTally counts what the hive did to GitHub comments: created +// (a new comment — an email to the author and every watcher), edited (an +// in-place rewrite — no email) and suppressed (a note the hive decided not +// to post as its own comment: folded into the status comment, found +// already present, or dropped as noise). Counters are cumulative since +// process start; the hourly log line prints them so the delta between two +// lines is the hour's traffic. +type notificationTally struct { + created atomic.Int64 + edited atomic.Int64 + suppressed atomic.Int64 + + mu sync.Mutex + lastLog time.Time +} + +// notificationTallyNow is the tally's clock, replaced by tests. +var notificationTallyNow = time.Now + +func (t *notificationTally) bump(counter *atomic.Int64, logger *slog.Logger) { + if t == nil { + return + } + counter.Add(1) + t.maybeLog(logger) +} + +// maybeLog writes the hourly line when at least notificationTallyLogEvery +// has passed since the last one. The first bump after start logs at once +// so a fresh process reports its baseline. +func (t *notificationTally) maybeLog(logger *slog.Logger) { + if logger == nil { + return + } + now := notificationTallyNow() + t.mu.Lock() + due := t.lastLog.IsZero() || now.Sub(t.lastLog) >= notificationTallyLogEvery + if due { + t.lastLog = now + } + t.mu.Unlock() + if !due { + return + } + logger.Info("github notifications", + slog.Int64("created", t.created.Load()), + slog.Int64("edited", t.edited.Load()), + slog.Int64("suppressed", t.suppressed.Load())) +} + +// noteCommentCreated records a new issue/PR comment. +func (c *Client) noteCommentCreated() { + if c == nil { + return + } + c.notifications.bump(&c.notifications.created, c.logger) +} + +// noteCommentEdited records an in-place comment edit. +func (c *Client) noteCommentEdited() { + if c == nil { + return + } + c.notifications.bump(&c.notifications.edited, c.logger) +} + +// noteCommentSuppressed records a comment the hive chose not to create. +func (c *Client) noteCommentSuppressed() { + if c == nil { + return + } + c.notifications.bump(&c.notifications.suppressed, c.logger) +} + +// NotificationCounts returns the cumulative created/edited/suppressed +// comment counts, for tests and diagnostics. +func (c *Client) NotificationCounts() (created, edited, suppressed int64) { + if c == nil { + return 0, 0, 0 + } + return c.notifications.created.Load(), c.notifications.edited.Load(), c.notifications.suppressed.Load() +} diff --git a/src/pkg/github/pr_status_comment.go b/src/pkg/github/pr_status_comment.go index 700c555de5..e1ea1587dc 100644 --- a/src/pkg/github/pr_status_comment.go +++ b/src/pkg/github/pr_status_comment.go @@ -34,3 +34,31 @@ func (c *Client) UpsertPRStatusSection(ctx context.Context, repo string, number } return res.Created, nil } + +// PostNoteOrFold delivers a hive note on repo#number. In quiet mode +// (github.notifications.mode) it rewrites section key of the item's status +// comment, so the only comment ever created is the status comment itself; +// in normal mode it posts body as a comment of its own, as the hive always +// did. The returned bool reports whether a new comment was created. +func (c *Client) PostNoteOrFold(ctx context.Context, repo string, number int, key, body string) (bool, error) { + if c == nil { + return false, ErrNoGitHubClient + } + if !c.notificationsQuiet() { + return true, c.CreateIssueComment(ctx, repo, number, body) + } + created, err := c.UpsertPRStatusSection(ctx, repo, number, key, body) + if err == nil && !created { + c.noteCommentSuppressed() + } + return created, err +} + +// PostClaimNote posts a contributor's `` comment body +// through PostNoteOrFold under prstatus.KeyClaim: claim readers scan every +// hive comment body for the marker, so a status section carries it as +// well as a comment of its own did. +func (c *Client) PostClaimNote(ctx context.Context, repo string, number int, body string) error { + _, err := c.PostNoteOrFold(ctx, repo, number, prstatus.KeyClaim, body) + return err +} diff --git a/src/pkg/github/review_backlog.go b/src/pkg/github/review_backlog.go index e44f4043be..50778b1f37 100644 --- a/src/pkg/github/review_backlog.go +++ b/src/pkg/github/review_backlog.go @@ -6,6 +6,7 @@ import ( "encoding/hex" "encoding/json" "fmt" + "github.com/hivecommons/hive/pkg/prstatus" "log/slog" "os" "path/filepath" @@ -516,7 +517,7 @@ func (c *Client) fileReviewBacklog(ctx context.Context, req ReviewRequest, now t return filedIssues[i].key < filedIssues[j].key }) if !ledger.SummaryCommented[prKey] { - if err := c.CreateIssueComment(ctx, req.Repo, req.Number, reviewBacklogSummaryComment(filedIssues, dailyCap)); err != nil { + if _, err := c.PostNoteOrFold(ctx, req.Repo, req.Number, prstatus.KeyReviewBacklog, reviewBacklogSummaryComment(filedIssues, dailyCap)); err != nil { return err } c.recordWriteAudit(AuditActionReviewBacklogSummaryPosted, hiveWriteMeta(), diff --git a/src/pkg/github/review_evidence_merge.go b/src/pkg/github/review_evidence_merge.go index 45d5b951e4..ebb5bac10c 100644 --- a/src/pkg/github/review_evidence_merge.go +++ b/src/pkg/github/review_evidence_merge.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "github.com/hivecommons/hive/pkg/prstatus" "log/slog" "os" "sort" @@ -99,6 +100,18 @@ func (c *Client) recordReviewEvidenceMerge(repo string, number int, method, sha, return } body := ReviewEvidenceComment(full, number, b.ID, b.Hash) + if c.notificationsQuiet() { + // The bundle is stored server-side and linked from the Activity + // Monitor; the pointer is a line of the status comment, not an + // email to every watcher of a PR that just merged. + if created, err := c.UpsertPRStatusSection(ctx, full, number, prstatus.KeyEvidence, body); err != nil { + c.logger.Warn("review evidence: could not record merge pointer on status comment", + slog.String("repo", full), slog.Int("number", number), slog.String("error", err.Error())) + } else if !created { + c.noteCommentSuppressed() + } + return + } if _, _, err := c.client.Issues.CreateComment(ctx, owner, name, number, &gh.IssueComment{Body: gh.Ptr(body)}); err != nil { c.logger.Warn("review evidence: could not post merge comment", slog.String("repo", full), slog.Int("number", number), slog.String("error", err.Error())) diff --git a/src/pkg/github/review_evidence_read_test.go b/src/pkg/github/review_evidence_read_test.go index 4a0bfac23e..4794505f05 100644 --- a/src/pkg/github/review_evidence_read_test.go +++ b/src/pkg/github/review_evidence_read_test.go @@ -13,6 +13,7 @@ import ( "net/http/httptest" "os" "path/filepath" + "strconv" "strings" "sync" "testing" @@ -219,6 +220,7 @@ type evidenceMergeFixture struct { postFail bool comments []string posts int + edits int prFetches int } @@ -252,10 +254,21 @@ func newEvidenceMergeServer(t *testing.T, f *evidenceMergeFixture) *httptest.Ser return } out := []map[string]any{} - for _, c := range f.comments { - out = append(out, map[string]any{"body": c}) + for i, c := range f.comments { + out = append(out, map[string]any{"id": i + 1, "body": c}) } _ = json.NewEncoder(w).Encode(out) + case r.Method == http.MethodPatch && strings.HasPrefix(r.URL.Path, "/repos/o/r/issues/comments/"): + f.edits++ + id, _ := strconv.Atoi(strings.TrimPrefix(r.URL.Path, "/repos/o/r/issues/comments/")) + var in struct { + Body string `json:"body"` + } + _ = json.NewDecoder(r.Body).Decode(&in) + if id >= 1 && id <= len(f.comments) { + f.comments[id-1] = in.Body + } + _ = json.NewEncoder(w).Encode(map[string]any{"id": id}) case r.Method == http.MethodPost && r.URL.Path == "/repos/o/r/issues/7/comments": f.posts++ if f.postFail { diff --git a/src/pkg/prstatus/prstatus.go b/src/pkg/prstatus/prstatus.go index 2dd0cfd4a8..f1d29d1fc3 100644 --- a/src/pkg/prstatus/prstatus.go +++ b/src/pkg/prstatus/prstatus.go @@ -133,4 +133,21 @@ const ( // when the head already carries the hive review it may have, so the // findings are kept without a second review submission. KeyReviewFindingsPrefix = "review-findings:" + // KeyEvidence is the post-merge pointer at the PR's evidence bundle + // (). + KeyEvidence = "evidence" + // KeyUnblocked is the note on a blocked issue that its blocking PR + // settled (). + KeyUnblocked = "unblocked" + // KeyReviewBacklog lists the non-blocking review findings filed as + // backlog issues (). + KeyReviewBacklog = "review-backlog" + // KeyClaim holds a contributor's `` marker; claim + // readers scan every hive comment body for it, so a section carries it + // as well as a comment of its own did. + KeyClaim = "claim" + // KeyAgentNotePrefix + kind holds an agent's relayed status note (the + // `hive/awaiting-ci:` deferral, a stage/045 parking note, an + // "Already fixed" verdict) that used to be its own comment. + KeyAgentNotePrefix = "agent-note:" ) diff --git a/src/pkg/writeback/testdata/direct-writes-baseline.txt b/src/pkg/writeback/testdata/direct-writes-baseline.txt index f9f7edb00b..670a7f79ba 100644 --- a/src/pkg/writeback/testdata/direct-writes-baseline.txt +++ b/src/pkg/writeback/testdata/direct-writes-baseline.txt @@ -106,13 +106,13 @@ wrap pkg/github/pr_duplicate_guard.go 1 wrap pkg/github/pr_level_hold.go 1 wrap pkg/github/pr_reporter_trust_release.go 1 wrap pkg/github/pr_request_watcher.go 3 +wrap pkg/github/pr_status_comment.go 1 wrap pkg/github/pr_supersession_autoclose.go 1 wrap pkg/github/pr_supersession_decision.go 1 wrap pkg/github/pr_supersession_resolve.go 6 wrap pkg/github/prclaims.go 3 wrap pkg/github/prior_art_gate_resume.go 5 wrap pkg/github/reporter_trust_wait.go 4 -wrap pkg/github/review_backlog.go 1 wrap pkg/github/review_backlog_routing.go 1 wrap pkg/github/review_backlog_selfcheck.go 1 wrap pkg/github/review_request_watcher.go 2