From cf9d8921284b2493d99d0bdc7812b13afa7d5c3d Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Fri, 30 Oct 2026 00:19:33 -0400 Subject: [PATCH 1/2] review: one backlog issue per finding; carried-over notes never refile The reviewer swarm filed a backlog issue per perspective per head for the same out-of-scope finding: each pass re-reported the other perspectives' still-open notes as new findings ("Previous style note still present"), so one finding on one PR became three issues, all blocked on the PR, and 16 of 62 open items in the activity monitor were such notes. - Filing: each item is remembered under its PR with file, line and title; a later report from any perspective or head naming the same finding (same file and line, shared symbol, or same title once its carry-over wrapper and advisory qualifiers are stripped) is tracked against the existing item, never filed. A pure carry-over or a finding carrying is a reference and is dropped when it names nothing tracked. One "still present at " comment per item per head, counted against the daily cap. Title dedupe ignores a leading [reviewer] tag. - Prompts: perspectives, the delta section and the heads decision step are told to report only genuinely new findings and to cite prior notes with the refers-to marker. - Block sweep: on merge the hive reads the finding's file on the PR's base; file or named symbol gone closes the item as completed (review_backlog_issue_closed audit), otherwise the block is lifted. - Activity monitor: blocked review notes fold under their open source PR as one muted line (or into the group's lowest-numbered note when the PR is not drawn); the row count counts a group once and the header breakdown names the folded notes. Signed-off-by: Andrew Anderson --- changelog.d/fixed-review-backlog-dedupe.md | 1 + src/docs/dashboard-sections.md | 2 + src/docs/github-write-surface.md | 1 + src/docs/review-swarm.md | 3 +- src/pkg/dashboard/stage_tracker.go | 11 + src/pkg/dashboard/stage_tracker_links.go | 1 + .../stage_tracker_repo_grouping_test.go | 4 + .../dashboard/stage_tracker_review_notes.go | 64 +++++ .../stage_tracker_review_notes_test.go | 120 +++++++++ src/pkg/dashboard/stage_tracker_ui_test.go | 12 +- src/pkg/dashboard/static/index.html | 90 ++++++- src/pkg/github/issue_request_watcher.go | 2 +- src/pkg/github/review_backlog.go | 62 +++++ src/pkg/github/review_backlog_blocks.go | 59 +++- src/pkg/github/review_backlog_dedupe.go | 251 ++++++++++++++++++ src/pkg/github/review_backlog_dedupe_test.go | 175 ++++++++++++ src/pkg/github/review_backlog_routing.go | 16 +- src/pkg/github/review_backlog_selfcheck.go | 111 ++++++++ .../github/review_backlog_selfcheck_test.go | 152 +++++++++++ src/pkg/github/write_surface_internal.go | 6 + src/pkg/review/delta.go | 1 + src/pkg/review/prior_notes_prompt_test.go | 37 +++ src/pkg/review/prompts.go | 12 + src/pkg/review/prompts_decision.go | 2 +- 24 files changed, 1164 insertions(+), 31 deletions(-) create mode 100644 changelog.d/fixed-review-backlog-dedupe.md create mode 100644 src/pkg/dashboard/stage_tracker_review_notes.go create mode 100644 src/pkg/dashboard/stage_tracker_review_notes_test.go create mode 100644 src/pkg/github/review_backlog_dedupe.go create mode 100644 src/pkg/github/review_backlog_dedupe_test.go create mode 100644 src/pkg/github/review_backlog_selfcheck.go create mode 100644 src/pkg/github/review_backlog_selfcheck_test.go create mode 100644 src/pkg/review/prior_notes_prompt_test.go diff --git a/changelog.d/fixed-review-backlog-dedupe.md b/changelog.d/fixed-review-backlog-dedupe.md new file mode 100644 index 0000000000..1e9b436f2b --- /dev/null +++ b/changelog.d/fixed-review-backlog-dedupe.md @@ -0,0 +1 @@ +- review: one backlog issue per finding per PR across perspectives and heads — carried-over "previous/prior … still present" notes and `` citations are references that never refile (at most one "still present at " comment per head); on merge the blocked sweep checks the base itself and closes items whose file or symbol is gone; the activity monitor folds blocked review notes under their source PR as one muted line and counts the group once; dedupe ignores a leading `[reviewer]` title prefix diff --git a/src/docs/dashboard-sections.md b/src/docs/dashboard-sections.md index 36875f28c4..37597c4ea5 100644 --- a/src/docs/dashboard-sections.md +++ b/src/docs/dashboard-sections.md @@ -1038,6 +1038,8 @@ An issue wearing `blocked` because the hive parked it on a pull request — its The state (`open`, `merged`, `closed`, or absent when nothing knows) comes only from what the hive already holds — the status snapshot's open PRs, the ForgeState cache, the recent-merge ring — never a GitHub call from the tracker build; a merged or closed-unmerged blocker says so (`merged; the blocked sweep lifts the label next` / `closed unmerged; the blocked sweep re-triages it next`). The server reads the marker from the issue body when it has one, else from the enumeration's `blocked on owner/repo#N` reason, so sidelined rows (which carry only the reason) name it too. +**Review notes fold under their PR.** A `blocked` review-backlog issue (`Review backlog (): …`, with or without the `[reviewer]` tag) whose source PR is still open is not a row of its own: the payload marks it `review_note_of: ` and lists it in the PR's `review_notes`, and the tracker draws the PR row with one muted line — `3 review notes blocked on this PR · #15627, #15628, #15629`, each number a link — instead of three first-class rows. When the source PR is not drawn in the current view (the Issues tab, or a PR outside the snapshot), the group collapses into its lowest-numbered note, which carries `+2 more review notes blocked on PR #N`. The activity-monitor counter counts rows, so a group costs it one row, and the header's sum names the difference: `… · 4 review notes folded under their PR`; the Overview counter still counts every issue. A note whose PR has merged or closed is about to be lifted or re-triaged by the blocked sweep and stays a row (hivecommons/hive: 16 of 62 open items were such notes). + A `blocked` issue with neither (a human-applied label) keeps the bare pill. This is distinct from `blocked_on`, the open GitHub "blocked by" dependencies of a sequenced sub-issue ([#14765](https://github.com/hivecommons/hive/issues/14765)); when a row has both, the chevron tooltip lists them together. #### Blocked pill — capability gaps diff --git a/src/docs/github-write-surface.md b/src/docs/github-write-surface.md index 9ac9986337..2bb7d3749e 100644 --- a/src/docs/github-write-surface.md +++ b/src/docs/github-write-surface.md @@ -151,6 +151,7 @@ writes no entry. | Review backlog issues | `review_backlog.go` | yes (`review_backlog_issue_filed`, one per GitHub issue, with `pr`, `perspective`, `reused`, `destination`) | yes / backlog issue number | | Review backlog summary comment | `review_backlog.go` | yes (`review_backlog_summary_posted`) | yes / PR number | | Review backlog routing batch | `review_backlog.go` | yes (`review_backlog_batch_routed`, one per filing pass, with `destination`, `filed`, `updated`, `capped`) | yes / PR number | +| Review backlog self-check close | `review_backlog_selfcheck.go` | yes (`review_backlog_issue_closed`, with `pr`, `why`) | yes / backlog issue number | | Human-decision and review-priority labels | `human_decision_label.go` | yes (`hive_label_applied`, with `label`) | yes / PR number | | Recommendations issue | `recommendations.go` | yes (`recommendations_posted`, `outcome=created` or `updated`) | yes / issue number | | Fleet report issue | `fleet_report.go` | yes (`fleet_report_posted`, `outcome=created` or `commented`) | yes / issue number | diff --git a/src/docs/review-swarm.md b/src/docs/review-swarm.md index 5f143f5ad0..ec121ff84c 100644 --- a/src/docs/review-swarm.md +++ b/src/docs/review-swarm.md @@ -267,10 +267,11 @@ When `block_at` is set and `backlog_below` is on, every cited in-scope finding ( - **Destination.** `github_issue` files an issue in the PR's repo. `github_project` also adds that issue to the `governor.work_source.github_projects` project and sets its Status to `project_column_id`. `linear` files an issue on the Linear team mapped to the repo, in `linear_state` when set (an unknown state is an error naming it; label names the team lacks are skipped). `jira` files a Task in the first `work_source.jira.project_keys` project and moves it to `jira_status` when set. All three use the work-source credentials. If the destination's work source is not active or cannot be built, the item is filed as a GitHub issue with the `agent/reviewer` label added, and the hive logs why. - **Item body.** The finding title and summary, the PR link, a `file:line` permalink at the reviewed head SHA, the severity with its P-level, the fingerprint, and the line `filed by Hive review backlog`. `labels` are applied to every item. - **Fingerprint dedup.** Each finding is fingerprinted as `sha256(rule | path | snippet)`: the title, the cleaned file path and the summary, lower-cased with whitespace collapsed. Line numbers are left out, so the same nit at a shifted line still matches. `/data/review-backlog-issues.json` keeps a per-repo map of fingerprint → item. When another PR raises a known fingerprint, the hive comments on the existing item with the new PR link and permalink instead of filing a second one. The same PR raising it again changes nothing. When the destination folds a new finding into an existing open item instead, the item receives the full finding — title, `file:line`, summary, severity, PR link and fingerprint — under an "Additional finding (folded in — same finding area and category)" heading with an unchecked task-list line, still tagged with the `hive:review-backlog-link` marker; the PR summary lists it as `#T (folded into an existing issue) — ` ([#13604](https://github.com/hivecommons/hive/issues/13604)). The `github_issue` sink folds into an open hive-filed issue that cites the same files only when that issue is in the same category (its `Review backlog ()` title or `Perspective:` line matches the finding's perspective) and it either cites the same file within 10 lines of the finding or names a symbol the finding's title names. The same check applies to an open issue whose title differs from the finding's only after its `Review backlog ():` prefix; only an exact title match is reused unconditionally. Sharing a file alone is not enough: the finding is filed as its own issue, which gets a `hive:review-backlog-same-file` comment reading "Same file as #N" ([#13906](https://github.com/hivecommons/hive/issues/13906)). +- **One item per finding per PR.** Each item filed from a PR is also remembered under that PR with its evidence file, line and title (`pr_findings` in the ledger). A later report from any perspective or at any head that names the same finding — same file and the same line, a shared code symbol, or the same title once its wrapping is stripped — is tracked against the existing item, never filed again. A report whose title is only a carry-over wrapper ("Previous style finding still present (non-blocking)", "Prior correctness note still present: …", "Previous security finding (…) unchanged at new head") or that carries the `` marker is a *reference*, not a finding: it is matched against the PR's tracked items and otherwise dropped with a log line, so one finding on one PR can no longer become three issues from three perspectives (hivecommons/hive#15627, #15628, #15629). The hive notes a tracked finding's persistence at most once per head, with one `Still present at ().` comment on the item (counted against the daily cap). Title matching ignores a leading `[reviewer]` role tag, so an existing issue dedupes whichever filing path titled it. - **Split on host close.** A folded finding lives only in its `hive:review-backlog-link` comment, so closing the host used to close it too. Every ten minutes, alongside the closed-issue stage sweep, the hive reads the hive-authored link comments on recently closed backlog hosts (the issues the ledger points findings at) and re-files each still-unchecked `- [ ]` finding as its own `github_issue` backlog item headed `hive:review-backlog-split` and "Split from #N (host closed)", comments the new link on the host under the same marker, and moves the finding's ledger item and fingerprint records to the new issue (or to the open twin the sink folded it into, which then carries the folded comment so its own close splits it again). A checked `- [x]` finding stays with the host. A host closed as not planned or duplicate keeps the findings that match it by the fold rule above and only splits out unrelated ones; the host itself is exempt from the issue filer's rejected-twin gate for that filing (a fold cites the host's own files, so the just-rejected host would otherwise always count as the rejection), while a rejection elsewhere still suppresses the finding and is logged and counted as `skipped`. `/data/review-backlog-issues.json` also keeps `split_keys`, link-marker key → the closed hosts it was split out of, so re-runs are idempotent. One pass files at most 20 findings across all repos; hosts the pass did not reach once that cap was hit are counted and logged as `deferred`. A host that received at least one split is bumped by its cross-link comment and re-surfaces in the next pass, but a deferred host keeps its old `updated_at` and is only revisited while it stays inside the pass window (the last ten-minute interval plus ten minutes of slack), so a backlog of more than 20 unchecked findings closing within one window may leave the trailing hosts unsplit; the `deferred` count in the sweep log is the signal to look for ([#13914](https://github.com/hivecommons/hive/issues/13914)). Hosts closed before the windowed sweep reached them are covered by a backfill pass that runs after each split sweep: it walks the ledger's host index, fetches each host directly and splits every closed one through the same idempotent path. A backfill pass that split, skipped or deferred anything logs `review backlog split backfill pass complete` with those counts. Once a pass visits every host without hitting the per-pass cap (including a host left part-split when the cap ran out) or a transient error, `/data/review-backlog-issues.json` records `split_backfilled` and later passes are no-ops; an incomplete pass logs `review backlog split backfill incomplete, retrying next pass` and the next pass retries. Clearing `split_backfilled` re-runs the walk ([#13947](https://github.com/hivecommons/hive/issues/13947)). - **Cap.** At most `max_per_pr_per_day` items are filed or updated per PR per UTC day. Findings over the cap are retried on the next review. Out-of-scope findings also stay within `review.max_out_of_scope_backlog_issues` per PR. - **Audit.** Each filing pass that wrote anything records one `review_backlog_batch_routed` entry on the PR (`destination`, `filed`, `updated`, `capped`). Each new GitHub issue also gets its own `review_backlog_issue_filed` entry; an issue split out of a closed host carries `split_from=` and `reused`, and the dashboard audit log records one `review-backlog-split` entry per split. The PR receives one summary comment that lists the items filed. -- **Blocked on the PR.** Every item the backlog creates is parked on the reviewed PR with the `blocked` label and the `` marker, in every destination ([#13833](https://github.com/hivecommons/hive/issues/13833)). A below-line or out-of-scope finding is almost always about lines the PR changes, so working it on the base branch first is wrong or conflicts. When the cited file only exists in the PR the note says so ([#13093](https://github.com/hivecommons/hive/issues/13093)); otherwise it says the finding is about code the PR is changing, and tells whoever picks it up to check it still applies after the merge and to close it without a PR if it is already fixed. The ledger records the PR each item waits on, and `ReconcileReviewBacklogBlocks` (every governor cycle) resolves it: the PR **merged** lifts the block with a one-line note, the PR **closed unmerged** also lifts the block, with a `` note ("source PR #N closed unmerged — re-triage against current base"), and leaves the item open for normal triage and dedupe. The hive never closes or cancels a backlog item because its PR closed: a closed PR is often superseded by one that lands the same lines, so the finding may still apply. Closing it as not planned stays a human (or `/fixed`) action. Destinations without a resolver (Jira, GitHub Projects) get the note only. A finding folded into an existing open twin keeps the twin's own state ([#13364](https://github.com/hivecommons/hive/issues/13364)). +- **Blocked on the PR.** Every item the backlog creates is parked on the reviewed PR with the `blocked` label and the `` marker, in every destination ([#13833](https://github.com/hivecommons/hive/issues/13833)). A below-line or out-of-scope finding is almost always about lines the PR changes, so working it on the base branch first is wrong or conflicts. When the cited file only exists in the PR the note says so ([#13093](https://github.com/hivecommons/hive/issues/13093)); otherwise it says the finding is about code the PR is changing, and tells whoever picks it up to check it still applies after the merge and to close it without a PR if it is already fixed. The ledger records the PR each item waits on, and `ReconcileReviewBacklogBlocks` (every governor cycle) resolves it: when the PR **merged** the hive itself checks whether the finding still applies on the PR's base branch — the evidence file gone, or none of the code symbols the finding's title names left in it, closes the item as completed with a one-line note (`review_backlog_issue_closed` audit entry; GitHub-issue destination only); otherwise, or when the check cannot tell, the block is lifted with a one-line note and the item becomes actionable; the PR **closed unmerged** also lifts the block, with a `` note ("source PR #N closed unmerged — re-triage against current base"), and leaves the item open for normal triage and dedupe. The hive never closes or cancels a backlog item because its PR closed: a closed PR is often superseded by one that lands the same lines, so the finding may still apply. Closing it as not planned stays a human (or `/fixed`) action. Destinations without a resolver (Jira, GitHub Projects) get the note only. A finding folded into an existing open twin keeps the twin's own state ([#13364](https://github.com/hivecommons/hive/issues/13364)). - **Heads mode.** With [review heads](#review-heads-several-models-per-perspective), only the decision step's decided reports file backlog items; raw head reports never do, so two heads' wordings of one finding cannot become two items. Instead of the once-per-PR summary comment, the relay appends a **Follow-ups filed** section to the decided review itself (edited in place, the App's own COMMENTED review only), listing every item that head's findings map to — filed, re-raised onto an existing item, or already tracked — so each head commit's review says where its nits went. ### Verdict labels (`review.verdict_labels`) diff --git a/src/pkg/dashboard/stage_tracker.go b/src/pkg/dashboard/stage_tracker.go index c90061e1fa..bd5b5e6e17 100644 --- a/src/pkg/dashboard/stage_tracker.go +++ b/src/pkg/dashboard/stage_tracker.go @@ -522,6 +522,17 @@ type StageTrackerItem struct { // NestedUnder is, on a PR, the issue in the same repo whose row nests // it (0 when the PR has no listed issue and stands on its own). NestedUnder int `json:"nested_under,omitempty"` + // ReviewNoteOf is, on a `blocked` review-backlog issue ("Review backlog + // (): …", with or without the `[reviewer]` prefix), the + // open source PR in the same repo it waits on. The SPA folds such issues + // under the PR's row as one muted "N review notes blocked on this PR" + // line — or, when the PR is not a row, into the lowest-numbered note of + // the group — instead of N first-class rows (hivecommons/hive: 16 of 62 + // open items were blocked review notes). 0 on every other item. + ReviewNoteOf int `json:"review_note_of,omitempty"` + // ReviewNotes lists, on a PR, the blocked review-backlog issues folded + // under it (ReviewNoteOf == this PR), lowest number first. + ReviewNotes []StageTrackerReviewNote `json:"review_notes,omitempty"` // LinkedIssues lists, on a PR, the issues GitHub says it closes // (closingIssuesReferences, i.e. the PR body's Fixes/Closes/Resolves // keywords) in the same repo, so a PR whose issue is not listed — diff --git a/src/pkg/dashboard/stage_tracker_links.go b/src/pkg/dashboard/stage_tracker_links.go index ed602b1b9d..8b3d779cbe 100644 --- a/src/pkg/dashboard/stage_tracker_links.go +++ b/src/pkg/dashboard/stage_tracker_links.go @@ -396,6 +396,7 @@ func stageTrackerResolveLinkedPRs(items []StageTrackerItem, open func(repo strin items[i].ClosedIssue = stageTrackerClosedIssueAnchor(items[i]) } } + stageTrackerFoldReviewNotes(items) } // Link evidence arrives after the issue's stage/new narration, including diff --git a/src/pkg/dashboard/stage_tracker_repo_grouping_test.go b/src/pkg/dashboard/stage_tracker_repo_grouping_test.go index 9e5fe1a2be..8be27c46d6 100644 --- a/src/pkg/dashboard/stage_tracker_repo_grouping_test.go +++ b/src/pkg/dashboard/stage_tracker_repo_grouping_test.go @@ -195,6 +195,10 @@ func stageTrackerRepoGroupingHarness(t *testing.T, html string) string { jsFunc(t, html, "stageTrackerMoveAttrs"), jsFunc(t, html, "stageTrackerNeedsAttention"), jsFunc(t, html, "stageTrackerVisible"), + jsFunc(t, html, "stageTrackerIsReviewNote"), + jsFunc(t, html, "stageTrackerFoldedReviewNotes"), + jsFunc(t, html, "stageTrackerReviewNotesHtml"), + jsFunc(t, html, "stageTrackerFoldedCount"), jsFunc(t, html, "stageTrackerRepoTint"), jsFunc(t, html, "stageTrackerRepoShort"), jsFunc(t, html, "stageTrackerIsPrimary"), diff --git a/src/pkg/dashboard/stage_tracker_review_notes.go b/src/pkg/dashboard/stage_tracker_review_notes.go new file mode 100644 index 0000000000..351ed71406 --- /dev/null +++ b/src/pkg/dashboard/stage_tracker_review_notes.go @@ -0,0 +1,64 @@ +package dashboard + +import ( + "regexp" + "slices" + "strconv" + "strings" +) + +// StageTrackerReviewNote is one blocked review-backlog issue folded under +// its source PR's tracker row. +type StageTrackerReviewNote struct { + Number int `json:"number"` + Title string `json:"title"` + URL string `json:"url,omitempty"` +} + +// stageTrackerReviewBacklogTitleRE matches the titles the review backlog +// files, with or without the `[reviewer]` role prefix. +var stageTrackerReviewBacklogTitleRE = regexp.MustCompile(`^\s*(?:\[[^\]]+\]\s*)?Review backlog\b`) + +// stageTrackerIsBlockedReviewNote reports whether the item is a review- +// backlog issue still wearing `blocked` for a source PR in its own repo. +func stageTrackerIsBlockedReviewNote(it *StageTrackerItem) bool { + if it == nil || it.Kind != "issue" || it.BlockedOnPR == nil || it.BlockedOnPR.Number == 0 { + return false + } + if !slices.Contains(it.Modifiers, modBlocked) || !strings.EqualFold(it.BlockedOnPR.Repo, it.Repo) { + return false + } + return stageTrackerReviewBacklogTitleRE.MatchString(it.Title) +} + +// stageTrackerFoldReviewNotes names, on each blocked review-backlog issue, +// the source PR it is folded under (ReviewNoteOf) and, on each listed source +// PR, the notes folded under it (ReviewNotes). Notes whose PR the hive no +// longer counts open (merged or closed: the blocked sweep lifts them next) +// stay first-class rows. +func stageTrackerFoldReviewNotes(items []StageTrackerItem) { + prs := map[string]*StageTrackerItem{} + for i := range items { + if items[i].Kind == "pr" { + prs[strings.ToLower(items[i].Repo)+"#"+strconv.Itoa(items[i].Number)] = &items[i] + } + } + for i := range items { + it := &items[i] + if !stageTrackerIsBlockedReviewNote(it) { + continue + } + if st := it.BlockedOnPR.State; st == blockedOnPRStateMerged || st == blockedOnPRStateClosed { + continue + } + it.ReviewNoteOf = it.BlockedOnPR.Number + if pr := prs[strings.ToLower(it.Repo)+"#"+strconv.Itoa(it.ReviewNoteOf)]; pr != nil { + pr.ReviewNotes = append(pr.ReviewNotes, StageTrackerReviewNote{Number: it.Number, Title: it.Title, URL: it.URL}) + } + } + for i := range items { + if n := items[i].ReviewNotes; len(n) > 1 { + slices.SortFunc(n, func(a, b StageTrackerReviewNote) int { return a.Number - b.Number }) + } + } +} diff --git a/src/pkg/dashboard/stage_tracker_review_notes_test.go b/src/pkg/dashboard/stage_tracker_review_notes_test.go new file mode 100644 index 0000000000..587086e7f1 --- /dev/null +++ b/src/pkg/dashboard/stage_tracker_review_notes_test.go @@ -0,0 +1,120 @@ +package dashboard + +import ( + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/github" +) + +// A `blocked` review-backlog issue is folded under its open source PR +// (review_note_of / review_notes) instead of standing as a row of its own: +// 16 of the 62 open hivecommons/hive items were such notes. +func TestStageTrackerFoldsBlockedReviewNotesUnderSourcePR(t *testing.T) { + now := time.Date(2026, 10, 30, 1, 0, 0, 0, time.UTC) + s := newTestServer() + filed := now.Add(-5 * time.Hour) + status := minimalPayload() + status.Repos = []FrontendRepo{{ + Name: "hive", Full: "hivecommons/hive", + OpenPrs: []any{FrontendPR{PullRequest: github.PullRequest{Number: 15582, Title: "warn once per PR", Author: "pat", State: "open"}}}, + NonActionableIssues: []any{ + github.RepoWorkIssue{Repo: "hivecommons/hive", Number: 15627, Title: "Review backlog (style): New test re-covers prWarnOnce semantics", Labels: []string{"agent/reviewer", "blocked"}, CreatedAt: filed, Bucket: "filtered", Reason: "blocked on hivecommons/hive#15582"}, + github.RepoWorkIssue{Repo: "hivecommons/hive", Number: 15629, Title: "[reviewer] Review backlog (intent-alignment): Previous style note still present", Labels: []string{"agent/reviewer", "blocked"}, CreatedAt: filed, Bucket: "filtered", Reason: "blocked on hivecommons/hive#15582"}, + github.RepoWorkIssue{Repo: "hivecommons/hive", Number: 15628, Title: "Review backlog (security): Previous style finding still present", Labels: []string{"agent/reviewer", "blocked"}, CreatedAt: filed, Bucket: "filtered", Reason: "blocked on hivecommons/hive#15582"}, + // Source PR not in the snapshot (state unknown): still a note of that PR. + github.RepoWorkIssue{Repo: "hivecommons/hive", Number: 15630, Title: "Review backlog (correctness): Prior correctness note still present", Labels: []string{"agent/reviewer", "blocked"}, CreatedAt: filed, Bucket: "filtered", Reason: "blocked on hivecommons/hive#15621"}, + // Blocked on something that is not a review-backlog note: a row. + github.RepoWorkIssue{Repo: "hivecommons/hive", Number: 15700, Title: "Ship the thing", Labels: []string{"blocked"}, CreatedAt: filed, Bucket: "filtered", Reason: "blocked on hivecommons/hive#15582"}, + // Blocked on a PR in another repo: not folded. + github.RepoWorkIssue{Repo: "hivecommons/hive", Number: 15701, Title: "Review backlog (style): elsewhere", Labels: []string{"agent/reviewer", "blocked"}, CreatedAt: filed, Bucket: "filtered", Reason: "blocked on hivecommons/pluk#7"}, + }, + }} + s.UpdateStatus(status) + tr := s.buildStageTracker(status, nil, now) + + for _, n := range []int{15627, 15628, 15629} { + if it := trackerItem(t, tr, "issue", n); it.ReviewNoteOf != 15582 { + t.Errorf("#%d review_note_of = %d, want 15582 (title %q)", n, it.ReviewNoteOf, it.Title) + } + } + if it := trackerItem(t, tr, "issue", 15630); it.ReviewNoteOf != 15621 { + t.Errorf("#15630 review_note_of = %d, want 15621", it.ReviewNoteOf) + } + for _, n := range []int{15700, 15701} { + if it := trackerItem(t, tr, "issue", n); it.ReviewNoteOf != 0 { + t.Errorf("#%d review_note_of = %d, want 0", n, it.ReviewNoteOf) + } + } + pr := trackerItem(t, tr, "pr", 15582) + got := make([]int, 0, len(pr.ReviewNotes)) + for _, n := range pr.ReviewNotes { + got = append(got, n.Number) + } + if len(got) != 3 || got[0] != 15627 || got[1] != 15628 || got[2] != 15629 { + t.Fatalf("PR 15582 review_notes = %v, want [15627 15628 15629]", got) + } + if pr.ReviewNotes[2].URL != "https://github.com/hivecommons/hive/issues/15629" || !strings.HasPrefix(pr.ReviewNotes[2].Title, "[reviewer] Review backlog") { + t.Fatalf("review note carries url and title: %+v", pr.ReviewNotes[2]) + } +} + +// A note whose source PR already merged or closed is about to be lifted or +// re-triaged by the blocked sweep: it stays a first-class row. +func TestStageTrackerReviewNoteOfResolvedPRStaysARow(t *testing.T) { + items := []StageTrackerItem{ + {Repo: "o/r", Kind: "issue", Number: 2, Title: "Review backlog (style): x", Modifiers: []string{modBlocked}, BlockedOnPR: &StageTrackerBlockedOnPR{Repo: "o/r", Number: 1, State: blockedOnPRStateMerged}}, + {Repo: "o/r", Kind: "issue", Number: 3, Title: "Review backlog (style): y", Modifiers: []string{modBlocked}, BlockedOnPR: &StageTrackerBlockedOnPR{Repo: "o/r", Number: 1, State: blockedOnPRStateClosed}}, + {Repo: "o/r", Kind: "issue", Number: 4, Title: "Review backlog (style): z", Modifiers: []string{modBlocked}, BlockedOnPR: &StageTrackerBlockedOnPR{Repo: "o/r", Number: 1}}, + // Label lifted, marker still in the body: not a blocked note. + {Repo: "o/r", Kind: "issue", Number: 5, Title: "Review backlog (style): w", BlockedOnPR: &StageTrackerBlockedOnPR{Repo: "o/r", Number: 1}}, + } + stageTrackerFoldReviewNotes(items) + for i, want := range []int{0, 0, 1, 0} { + if items[i].ReviewNoteOf != want { + t.Errorf("#%d review_note_of = %d, want %d", items[i].Number, items[i].ReviewNoteOf, want) + } + } +} + +// SPA: folded notes are not rows (so the pill, which counts rows, counts a +// group once), the PR row carries one muted line naming them, a group whose +// PR is not drawn collapses into its lowest-numbered note, and the count +// line's breakdown still adds up to the open totals. +func TestStageTrackerReviewNotesFoldJS(t *testing.T) { + html := indexHTML(t) + runStageTrackerRowActionsScript(t, stageTrackerRowsPreludeJS(t, html)+"\n"+jsFunc(t, html, "stageTrackerRowCount")+"\n"+stageTrackerRowsFixtureJS+` +const since = new Date(now - 3600000).toISOString(); +const ctx2 = Object.assign({}, ctx); +const pr = (n, notes) => ({ repo: 'hivecommons/hive', number: n, kind: 'pr', title: 'PR ' + n, url: 'https://x/' + n, stage: 50, stage_since: since, author: 'pat', author_kind: 'human', modifiers: [], review_notes: notes }); +const note = (n, of) => ({ repo: 'hivecommons/hive', number: n, kind: 'issue', title: 'Review backlog (style): note ' + n, url: 'https://x/i' + n, stage: 0, stage_since: since, modifiers: ['blocked'], blocked_on_pr: { repo: 'hivecommons/hive', number: of, state: 'open' }, review_note_of: of }); +const plain = (n) => ({ repo: 'hivecommons/hive', number: n, kind: 'issue', title: 'Issue ' + n, url: 'https://x/i' + n, stage: 10, stage_since: since, modifiers: [] }); +const n101 = note(101, 100), n102 = note(102, 100), n103 = note(103, 100), n104 = note(104, 200), n105 = note(105, 200); +const items = [pr(100, [n101, n102, n103].map(x => ({ number: x.number, title: x.title, url: x.url }))), n103, n101, n102, n105, n104, plain(106)]; +ctx2.items = items; +const keys = rows => rows.map(r => r.kind + '#' + r.number); +assert.deepStrictEqual(keys(stageTrackerVisible(items, { kind: 'all', repo: '' })), ['pr#100', 'issue#104', 'issue#106'], 'three notes fold under their PR row; the two whose PR is not drawn collapse into #104'); +assert.strictEqual(stageTrackerRowCount(items, ctx2), 3, 'the pill counts a group once'); +assert.deepStrictEqual(keys(stageTrackerVisible(items, { kind: 'issue', repo: '' })), ['issue#101', 'issue#104', 'issue#106'], 'Issues view: no PR rows, so each group shows its lowest-numbered note'); +assert.deepStrictEqual(keys(stageTrackerVisible(items, { kind: 'pr', repo: '' })), ['pr#100'], 'Pull requests view unchanged'); + +const prRow = stageTrackerRowHtml(items[0], stages, ctx2); +assert.ok(prRow.includes('
3 review notes blocked on this PR · '), 'one muted line on the PR row: ' + prRow); +for (const n of [101, 102, 103]) assert.ok(prRow.includes('href="https://x/i' + n + '"'), 'note #' + n + ' stays reachable'); +const leadRow = stageTrackerRowHtml(n104, stages, ctx2); +assert.ok(leadRow.includes('data-ham-review-notes="1">+1 more review note blocked on PR #200 · '), 'the lead note names the rest of its group: ' + leadRow); +assert.ok(leadRow.includes('href="https://x/i105"')); +assert.ok(!stageTrackerRowHtml(plain(106), stages, ctx2).includes('ham-review-notes'), 'a plain issue has no notes line'); +assert.ok(!stageTrackerRowHtml(pr(300, []), stages, ctx2).includes('ham-review-notes'), 'a PR without notes has no notes line'); + +const folded = stageTrackerFoldedCount(items, ctx2); +assert.strictEqual(folded, 4, 'four of six issues are folded'); +const counts = stageTrackerCountsText({ total: 3, from: 1, to: 3 }, 3, 0, { issues: 6, prs: 1 }, 0, folded); +assert.strictEqual(counts.badge, 3); +assert.ok(counts.badgeTitle.includes('3 rows (6 issues + 1 PR on its own row)'), counts.badgeTitle); +assert.ok(counts.badgeTitle.includes('4 review notes folded under their PR'), counts.badgeTitle); +assert.ok(!counts.badgeTitle.includes("issue's row"), 'no PR shares an issue row here: ' + counts.badgeTitle); +assert.deepStrictEqual(stageTrackerRowBreakdown(7, { issues: 6, prs: 1 }), { known: true, rows: 7, issues: 6, prs: 1, standalonePRs: 1, shared: 0, folded: 0 }, 'without folding the breakdown is as before'); +`) +} diff --git a/src/pkg/dashboard/stage_tracker_ui_test.go b/src/pkg/dashboard/stage_tracker_ui_test.go index a8c7850482..b3c53270c4 100644 --- a/src/pkg/dashboard/stage_tracker_ui_test.go +++ b/src/pkg/dashboard/stage_tracker_ui_test.go @@ -232,6 +232,10 @@ func stageTrackerRowsPreludeJS(t *testing.T, html string) string { jsFunc(t, html, "stageTrackerMoveAttrs"), jsFunc(t, html, "stageTrackerNeedsAttention"), jsFunc(t, html, "stageTrackerVisible"), + jsFunc(t, html, "stageTrackerIsReviewNote"), + jsFunc(t, html, "stageTrackerFoldedReviewNotes"), + jsFunc(t, html, "stageTrackerReviewNotesHtml"), + jsFunc(t, html, "stageTrackerFoldedCount"), stageTrackerCuesJS(t, html), }, "\n") } @@ -980,6 +984,10 @@ func TestStageTrackerFilterSortPage(t *testing.T) { jsFunc(t, html, "stageTrackerFilterActive"), jsFunc(t, html, "stageTrackerMatches"), jsFunc(t, html, "stageTrackerVisible"), + jsFunc(t, html, "stageTrackerIsReviewNote"), + jsFunc(t, html, "stageTrackerFoldedReviewNotes"), + jsFunc(t, html, "stageTrackerReviewNotesHtml"), + jsFunc(t, html, "stageTrackerFoldedCount"), jsFunc(t, html, "stageTrackerRowCount"), jsFunc(t, html, "stageTrackerSort"), jsFunc(t, html, "stageTrackerPage"), @@ -1111,8 +1119,8 @@ assert.strictEqual(c.breakdown, '100 issues + 50 PRs on their own row'); c = stageTrackerCountsText(stageTrackerPage(many.slice(0, 37), F({ size: 50 })), 37, 3, { issues: 33, prs: 13 }, 3); assert.strictEqual(c.header, "37 rows (33 issues + 4 PRs on their own row) · 3 need attention · 9 PRs shown on their issue's row"); assert.strictEqual(c.badgeTitle, "37 rows (33 issues + 4 PRs on their own row) · 3 need attention · 9 PRs shown on their issue's row"); -assert.deepStrictEqual(stageTrackerRowBreakdown(37, { issues: 33, prs: 13 }), { known: true, rows: 37, issues: 33, prs: 13, standalonePRs: 4, shared: 9 }); -assert.deepStrictEqual(stageTrackerRowBreakdown(34, { issues: 33, prs: 1 }), { known: true, rows: 34, issues: 33, prs: 1, standalonePRs: 1, shared: 0 }); +assert.deepStrictEqual(stageTrackerRowBreakdown(37, { issues: 33, prs: 13 }), { known: true, rows: 37, issues: 33, prs: 13, standalonePRs: 4, shared: 9, folded: 0 }); +assert.deepStrictEqual(stageTrackerRowBreakdown(34, { issues: 33, prs: 1 }), { known: true, rows: 34, issues: 33, prs: 1, standalonePRs: 1, shared: 0, folded: 0 }); assert.strictEqual(stageTrackerCountsText(stageTrackerPage(many.slice(0, 34), F({ size: 50 })), 34, 0, { issues: 33, prs: 1 }).breakdown, '33 issues + 1 PR on its own row', 'singular PR'); assert.strictEqual(stageTrackerRowBreakdown(40, { issues: 33, prs: 5 }).standalonePRs, 5, 'standalone PRs never exceed open PRs'); assert.strictEqual(stageTrackerRowBreakdown(10, null).known, false); diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index dac2b4f48c..50b70b39e5 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -1718,6 +1718,8 @@ .ham-sub-superseded { opacity: .55; } .ham-sub-superseded .ham-row-head { min-height: 0; } .ham-superseded-note { color: var(--muted); font-size: var(--fs-xs); flex: 0 0 auto; white-space: nowrap; } + .ham-review-notes { color: var(--muted); font-size: var(--fs-xs); margin-top: var(--sp-1); } + .ham-review-notes a { color: inherit; } .ham-adopted-from { color: var(--muted); text-decoration: none; } /* The Let hive decide preview. One decision, readable in three seconds (operator feedback on the dialog, issue 15397): subject line, @@ -17441,7 +17443,7 @@

Where to get help

    ${stageTrackerStripItems(shown, stages, range, nowMs, ctx)}
${stageTrackerSinceHtml(shown, nowMs)} -
${stageTrackerCurrentStageHtml(shown, stages, range, nowMs)}${typeof stageTrackerEventHtml === 'function' ? stageTrackerEventHtml(stageTrackerItemKey(item), ctx) : ''}${subs} +
${stageTrackerCurrentStageHtml(shown, stages, range, nowMs)}${typeof stageTrackerEventHtml === 'function' ? stageTrackerEventHtml(stageTrackerItemKey(item), ctx) : ''}${typeof stageTrackerReviewNotesHtml === 'function' ? stageTrackerReviewNotesHtml(item, ctx) : ''}${subs} `; } @@ -18369,12 +18371,57 @@

Where to get help

// Rows for the current filter. Outside the Pull requests view a PR that // is nested under a listed issue is drawn as that issue's sub-row only. + // A `blocked` review-backlog issue (review_note_of = its source PR) is + // never a row of its own while that PR is open: it folds under the PR's + // row (one muted "N review notes blocked on this PR" line), or, when the + // PR is not drawn in this view, into the lowest-numbered note of its + // group, so a PR with eight notes costs the count one row, not eight. function stageTrackerVisible(items, filter, ctx) { const f = stageTrackerNormalizeFilter(filter || _stageTrackerFilter); const rows = (items || []).filter(it => stageTrackerMatches(it, f, ctx)); if (f.kind === 'pr') return rows; const issues = new Set(rows.filter(it => it.kind === 'issue').map(it => it.repo + '#' + it.number)); - return rows.filter(it => !(it.kind === 'pr' && it.nested_under && issues.has(it.repo + '#' + it.nested_under))); + const out = rows.filter(it => !(it.kind === 'pr' && it.nested_under && issues.has(it.repo + '#' + it.nested_under))); + const drawnPRs = new Set(rows.filter(it => it.kind === 'pr').map(it => it.repo + '#' + it.number)); + const lead = {}; + for (const it of out) { + if (!stageTrackerIsReviewNote(it)) continue; + const k = it.repo + '#' + it.review_note_of; + if (!(k in lead) || Number(it.number) < lead[k]) lead[k] = Number(it.number); + } + return out.filter(it => { + if (!stageTrackerIsReviewNote(it)) return true; + const k = it.repo + '#' + it.review_note_of; + return !drawnPRs.has(k) && lead[k] === Number(it.number); + }); + } + + // Pure: an issue the tracker folds as a review note (see stageTrackerVisible). + function stageTrackerIsReviewNote(it) { + return !!(it && it.kind === 'issue' && Number(it.review_note_of) > 0); + } + + // Pure: the review notes folded into this row. On a PR row, its + // review_notes; on the lead note of a group whose PR is not drawn, the + // other open notes of that group (from the full item list). + function stageTrackerFoldedReviewNotes(item, items) { + if (!item) return []; + if (item.kind === 'pr') return Array.isArray(item.review_notes) ? item.review_notes : []; + if (!stageTrackerIsReviewNote(item)) return []; + return (items || []).filter(it => stageTrackerIsReviewNote(it) && it.repo === item.repo && it.review_note_of === item.review_note_of && it.number !== item.number) + .sort((a, b) => a.number - b.number); + } + + // The muted one-line summary of a row's folded review notes, with each + // note's number as a link so nothing becomes unreachable. + function stageTrackerReviewNotesHtml(item, ctx) { + const notes = stageTrackerFoldedReviewNotes(item, ctx && ctx.items); + if (!notes.length) return ''; + const links = notes.map(n => n.url ? `#${Number(n.number)}` : `#${Number(n.number)}`).join(', '); + const lead = item.kind === 'pr' + ? `${notes.length} review note${notes.length === 1 ? '' : 's'} blocked on this PR` + : `+${notes.length} more review note${notes.length === 1 ? '' : 's'} blocked on PR #${Number(item.review_note_of)}`; + return `
${esc(lead)} · ${links}
`; } // The canonical tracker count (owner): rows of the default view — every @@ -18466,15 +18513,18 @@

Where to get help

// spells out how the row count is made ("33 issues + 4 PRs on their // own row") in the header and the pill tooltip, so the two pills read // as different views of one population rather than a disagreement. - function stageTrackerCountsText(page, allCount, attention, open, attentionAll) { + function stageTrackerCountsText(page, allCount, attention, open, attentionAll, folded) { const filteredOut = Math.max(0, allCount - page.total); const showing = page.total ? `showing ${page.from}–${page.to} of ${page.total}` : 'showing 0 of 0'; const attnText = n => n ? n + ' need' + (n === 1 ? 's' : '') + ' attention' : 'nothing needs attention'; const attn = attnText(attention); const attnAll = attnText(Number.isFinite(Number(attentionAll)) ? Number(attentionAll) : attention); const rowsWord = n => n === 1 ? 'row' : 'rows'; - const parts = stageTrackerRowBreakdown(allCount, open); - const note = parts.shared ? `${parts.shared} ${parts.shared === 1 ? 'PR' : 'PRs'} shown on ${parts.shared === 1 ? 'its' : 'their'} issue's row` : ''; + const parts = stageTrackerRowBreakdown(allCount, open, folded); + const noteBits = []; + if (parts.shared) noteBits.push(`${parts.shared} ${parts.shared === 1 ? 'PR' : 'PRs'} shown on ${parts.shared === 1 ? 'its' : 'their'} issue's row`); + if (parts.folded) noteBits.push(`${parts.folded} review ${parts.folded === 1 ? 'note' : 'notes'} folded under ${parts.folded === 1 ? 'its' : 'their'} PR`); + const note = noteBits.join(' · '); const breakdown = parts.known ? `${parts.issues} ${parts.issues === 1 ? 'issue' : 'issues'} + ${parts.standalonePRs} ${parts.standalonePRs === 1 ? 'PR' : 'PRs'} on ${parts.standalonePRs === 1 ? 'its' : 'their'} own row` : ''; return { showing: showing + (filteredOut ? ` (${filteredOut} filtered out)` : ''), @@ -18487,15 +18537,28 @@

Where to get help

} // Pure: how the default view's row count decomposes against the open - // item totals. Every open issue is a row; a PR is a row only when it is - // not drawn under a listed issue, so standalonePRs = rows − issues and - // shared = (issues + PRs) − rows. `known` is false without totals. - function stageTrackerRowBreakdown(rowCount, open) { + // item totals. Every open issue is a row except a review note folded + // under its PR (`folded`, see stageTrackerVisible); a PR is a row only + // when it is not drawn under a listed issue, so standalonePRs = rows − + // (issues − folded) and shared = (issues − folded + PRs) − rows. `known` + // is false without totals. `issues` stays the open-issue total so the + // breakdown still adds up to the Overview tile. + function stageTrackerRowBreakdown(rowCount, open, folded) { const rows = Math.max(0, Number(rowCount) || 0); - if (!open || !Number.isFinite(Number(open.issues)) || !Number.isFinite(Number(open.prs))) return { known: false, rows, issues: 0, prs: 0, standalonePRs: 0, shared: 0 }; + if (!open || !Number.isFinite(Number(open.issues)) || !Number.isFinite(Number(open.prs))) return { known: false, rows, issues: 0, prs: 0, standalonePRs: 0, shared: 0, folded: 0 }; const issues = Math.max(0, Number(open.issues)), prs = Math.max(0, Number(open.prs)); - const standalonePRs = Math.min(prs, Math.max(0, rows - issues)); - return { known: true, rows, issues, prs, standalonePRs, shared: Math.max(0, issues + prs - rows) }; + const fold = Math.min(issues, Math.max(0, Number(folded) || 0)); + const standalonePRs = Math.min(prs, Math.max(0, rows - (issues - fold))); + return { known: true, rows, issues, prs, standalonePRs, shared: Math.max(0, issues - fold + prs - rows), folded: fold }; + } + + // Pure: how many issues of the default view are folded review notes + // rather than rows (stageTrackerVisible drops them; the count line + // explains the difference). + function stageTrackerFoldedCount(items, ctx) { + const f = stageTrackerDefaultFilter(); + const shown = new Set(stageTrackerVisible(items, f, ctx).map(it => it.repo + '#' + it.number)); + return (items || []).filter(it => stageTrackerIsReviewNote(it) && stageTrackerMatches(it, f, ctx) && !shown.has(it.repo + '#' + it.number)).length; } // The tracker's open issue / PR counts: the payload's open_issues/open_prs @@ -18794,6 +18857,7 @@

All labels

ctx.events = _stageTrackerEvents; ctx.noteTuck = stageTrackerNoteTuckSetting(); ctx.sharedPRs = stageTrackerSharedPRs(d.items); + ctx.items = d.items; // Repo chips ride the setting against how many repos the payload spans. if (stageTrackerRepoChipOn(stageTrackerRepoChipSetting(), repos.length)) ctx.repoChips = { primary: stageTrackerPrimaryRepo(), active: f.repo }; const visible = stageTrackerVisible(d.items, f, ctx); @@ -18818,7 +18882,7 @@

All labels

// is the warn-coloured header suffix and the pill's tooltip, counted // over every item regardless of filter. const attentionAll = d.items.filter(stageTrackerNeedsAttention).length; - const counts = stageTrackerCountsText(page, allRows, attention, stageTrackerOpenCounts(d), attentionAll); + const counts = stageTrackerCountsText(page, allRows, attention, stageTrackerOpenCounts(d), attentionAll, stageTrackerFoldedCount(d.items, ctx)); setSectionSummary('activity-monitor-section', counts.header.replace(/(\d+) needs? attention/, m => `${m}`)); const badge = document.getElementById('activity-monitor-sidebar-badge'); if (badge) { badge.textContent = String(counts.badge); badge.title = counts.badgeTitle; if (counts.badge) badge.removeAttribute('data-zero'); else badge.setAttribute('data-zero', ''); } diff --git a/src/pkg/github/issue_request_watcher.go b/src/pkg/github/issue_request_watcher.go index bd46a6545b..67eb89f274 100644 --- a/src/pkg/github/issue_request_watcher.go +++ b/src/pkg/github/issue_request_watcher.go @@ -1778,7 +1778,7 @@ func (c *Client) scanOpenIssues(ctx context.Context, owner, repo, title, body st continue } candidate := strings.TrimSpace(is.GetTitle()) - if candidate == title { + if candidate == title || reviewBacklogTitlesEqual(candidate, title) { return openIssueMatches{subject: is}, nil } // Pages arrive newest-first, so overwriting leaves the oldest diff --git a/src/pkg/github/review_backlog.go b/src/pkg/github/review_backlog.go index 77e8150f66..aea6db96a6 100644 --- a/src/pkg/github/review_backlog.go +++ b/src/pkg/github/review_backlog.go @@ -52,6 +52,14 @@ type reviewBacklogLedger struct { // host in the ledger once (hivecommons/hive#13947), so later passes // skip it and leave closed hosts to the windowed split sweep. SplitBackfilled bool `json:"split_backfilled,omitempty"` + // PRFindings maps "#" → the findings filed or tracked from + // that PR, so a later perspective or head reporting the same finding is + // tracked against the existing item instead of filed again + // (hivecommons/hive#15627). + PRFindings map[string][]reviewBacklogPRFinding `json:"pr_findings,omitempty"` + // StillPresentHeads maps ":" → the last head a "still + // present" note was posted for, so each item gets at most one per head. + StillPresentHeads map[string]string `json:"still_present_heads,omitempty"` } // reviewBacklogLedgerMu serialises the load→mutate→save cycles of the @@ -125,6 +133,8 @@ func newReviewBacklogLedger() *reviewBacklogLedger { Fingerprints: map[string]map[string]reviewBacklogFingerprintRecord{}, PRDailyCount: map[string]int{}, SplitKeys: map[string][]int{}, + PRFindings: map[string][]reviewBacklogPRFinding{}, + StillPresentHeads: map[string]string{}, } } @@ -161,6 +171,12 @@ func loadReviewBacklog(path string) (*reviewBacklogLedger, error) { if l.SplitKeys == nil { l.SplitKeys = map[string][]int{} } + if l.PRFindings == nil { + l.PRFindings = map[string][]reviewBacklogPRFinding{} + } + if l.StillPresentHeads == nil { + l.StillPresentHeads = map[string]string{} + } return l, nil } @@ -263,6 +279,15 @@ func (c *Client) fileReviewBacklog(ctx context.Context, req ReviewRequest, now t key := reviewBacklogKey(req.Repo, req.Number, report.Perspective, finding) if prev, ok := ledger.Items[key]; ok { listed = append(listed, reviewBacklogFiled{number: prev.IssueNumber, key: prev.ItemKey, url: prev.IssueURL, title: reviewBacklogIssueTitle(report.Perspective, finding), status: reviewBacklogListedTracked}) + // The same perspective reporting it again at a new head + // leaves the once-per-head "still present" note. + if tracked, isTracked := ledger.prFinding(req.Repo, req.Number, finding); isTracked && dailyRemaining > 0 && ledger.stillPresentDue(req.Repo, tracked.ref(), headSHA) { + if err := sink.AppendToItem(ctx, req.Repo, tracked.ref(), reviewBacklogStillPresentComment(headSHA, []review.Perspective{report.Perspective})); err != nil { + return err + } + ledger.PRDailyCount[dayKey]++ + dailyRemaining-- + } continue } fp := ReviewFindingFingerprint(finding) @@ -277,6 +302,39 @@ func (c *Client) fileReviewBacklog(ctx context.Context, req ReviewRequest, now t RecordedAt: now.UTC(), } known, ok := ledger.fingerprint(req.Repo, fp) + // One item per finding per PR (hivecommons/hive#15627): a + // report that names a finding already tracked for this PR — + // another perspective's, or an earlier head's carried over — + // is a reference, not a filing. A carry-over that refers to + // nothing tracked here is dropped outright. + carryOver := reviewBacklogCarryOver(finding) + tracked, isTracked := ledger.prFinding(req.Repo, req.Number, finding) + if !isTracked { + for _, n := range reviewRefersTo(finding) { + if tracked, isTracked = ledger.prFindingByItem(req.Repo, req.Number, n); isTracked { + break + } + } + } + if isTracked { + record.IssueNumber, record.IssueURL, record.ItemKey = tracked.ItemNumber, tracked.ItemURL, tracked.ItemKey + ledger.Items[key] = record + listed = append(listed, reviewBacklogFiled{number: tracked.ItemNumber, key: tracked.ItemKey, url: tracked.ItemURL, title: reviewBacklogIssueTitle(report.Perspective, finding), status: reviewBacklogListedTracked}) + if dailyRemaining > 0 && ledger.stillPresentDue(req.Repo, tracked.ref(), headSHA) { + if err := sink.AppendToItem(ctx, req.Repo, tracked.ref(), reviewBacklogStillPresentComment(headSHA, []review.Perspective{report.Perspective})); err != nil { + return err + } + ledger.PRDailyCount[dayKey]++ + dailyRemaining-- + } + continue + } + if carryOver { + c.logger.Info("review backlog: carried-over note is a reference, not filed", + slog.String("repo", req.Repo), slog.Int("pr", req.Number), + slog.String("perspective", string(report.Perspective)), slog.String("title", strings.TrimSpace(finding.Title))) + continue + } if ok && known.Destination == destination && known.ItemID != "" { // A closed item must not collect new re-raises; drop the // ledger entry so the finding is re-filed (the create path @@ -307,6 +365,7 @@ func (c *Client) fileReviewBacklog(ctx context.Context, req ReviewRequest, now t known.UpdatedAt = now.UTC() ledger.setFingerprint(req.Repo, fp, known) ledger.Items[key] = record + ledger.addPRFinding(req.Repo, req.Number, finding, known.ref(), headSHA) ledger.PRDailyCount[dayKey]++ dailyRemaining-- updated++ @@ -381,6 +440,7 @@ func (c *Client) fileReviewBacklog(ctx context.Context, req ReviewRequest, now t } record.IssueNumber, record.IssueURL, record.ItemKey = ref.Number, ref.URL, ref.Key ledger.Items[key] = record + ledger.addPRFinding(req.Repo, req.Number, finding, ref, headSHA) fpRecord := reviewBacklogFingerprintRecord{ Destination: destination, ItemID: ref.ID, @@ -390,6 +450,8 @@ func (c *Client) fileReviewBacklog(ctx context.Context, req ReviewRequest, now t PRs: []int{req.Number}, FiledAt: now.UTC(), UpdatedAt: now.UTC(), + File: normaliseFingerprintPath(finding.File), + Title: strings.TrimSpace(finding.Title), } if !ref.Reused { // A filing folded into an open twin keeps the twin's own diff --git a/src/pkg/github/review_backlog_blocks.go b/src/pkg/github/review_backlog_blocks.go index 2387a39afd..e50e6cd3b6 100644 --- a/src/pkg/github/review_backlog_blocks.go +++ b/src/pkg/github/review_backlog_blocks.go @@ -18,9 +18,11 @@ import ( // the block. ReconcileReviewBacklogBlocks resolves parked items in their own // destination (GitHub issue, Linear, Jira, GitHub Projects): // -// - the PR merged: the block is lifted with a one-line note telling the -// worker to check the finding still applies and to close the item without -// a PR if it is already fixed; +// - the PR merged: the hive checks the finding itself first +// (reviewBacklogSelfVerify): if its file is gone from the base, or none +// of the symbols its title names is in that file any more, the item is +// closed as completed with a one-line note; otherwise the block is +// lifted with a note that the item is actionable now; // - the PR closed without merging: the block is dropped and a note asks for // re-triage against the current base. The item is never closed or // cancelled automatically: a closed PR is often superseded by one that @@ -37,6 +39,9 @@ const ( reviewBacklogBlockBlocked = "blocked" reviewBacklogBlockLifted = "lifted" reviewBacklogBlockRetriage = "retriage" + // reviewBacklogBlockClosed: the PR merged and the self-check found the + // finding gone from the base, so the item was closed as completed. + reviewBacklogBlockClosed = "closed" ) // reviewBacklogRetriageMarker tags the note posted when an item is unblocked @@ -64,12 +69,12 @@ type ReviewBacklogBlockResolver interface { func blockedOnReviewedPRNote(repo string, pr int) string { var b strings.Builder b.WriteString(BlockedOnMarker(repo, pr)) - fmt.Fprintf(&b, "\n**Blocked on PR #%d.** This finding came from reviewing PR #%d and is about code that PR is changing. Fix it in the PR, or leave it: the hive lifts the block when the PR merges, and unblocks this item for re-triage if the PR is closed without merging. Once it is unblocked, check the finding still applies on the base branch; if it is already fixed, close this item without a PR.\n\n", pr, pr) + fmt.Fprintf(&b, "\n**Blocked on PR #%d.** This finding came from reviewing PR #%d and is about code that PR is changing. Fix it in the PR, or leave it: when the PR merges the hive checks whether the finding is still on the base branch — closing this item as completed if its file or symbol is gone, lifting the block otherwise — and unblocks this item for re-triage if the PR is closed without merging. Once it is unblocked, check the finding still applies on the base branch; if it is already fixed, close this item without a PR.\n\n", pr, pr) return b.String() } func reviewBacklogLiftNote(repo string, pr int) string { - return fmt.Sprintf("%s\nUnblocked: %s#%d merged. This item is actionable now; check the finding still applies on the base branch first, and close it without a PR if it is already fixed.", blockedUnblockedMarker, repo, pr) + return fmt.Sprintf("%s\nUnblocked: %s#%d merged and the finding's file is still on the base branch. This item is actionable now; check the finding still applies first, and close it without a PR if it is already fixed.", blockedUnblockedMarker, repo, pr) } func reviewBacklogRetriageNote(repo string, pr int) string { @@ -92,7 +97,10 @@ type ReviewBacklogBlockResult struct { Checked int Lifted int Retriaged int - Errors int + // Closed counts items closed as completed because the self-check found + // the finding no longer applies on the base once its PR merged. + Closed int + Errors int } // reviewBacklogPROutcome is a tracked PR's state: still open, merged, or @@ -106,12 +114,19 @@ const ( ) func (c *Client) reviewBacklogPROutcome(ctx context.Context, repo string, number int) (reviewBacklogPROutcome, error) { + o, _, err := c.reviewBacklogPROutcomeBase(ctx, repo, number) + return o, err +} + +// reviewBacklogPROutcomeBase is reviewBacklogPROutcome plus the PR's base +// branch, which the self-check reads the finding's file from. +func (c *Client) reviewBacklogPROutcomeBase(ctx context.Context, repo string, number int) (reviewBacklogPROutcome, string, error) { owner, name := c.splitRepo(repo) pr, _, err := c.client.PullRequests.Get(ctx, owner, name, number) if err != nil { - return reviewBacklogPROpen, err + return reviewBacklogPROpen, "", err } - return prOutcome(pr), nil + return prOutcome(pr), strings.TrimSpace(pr.GetBase().GetRef()), nil } func prOutcome(pr *gh.PullRequest) reviewBacklogPROutcome { @@ -154,6 +169,7 @@ func ReconcileReviewBacklogBlocks(ctx context.Context, c *Client, logger *slog.L } sort.Strings(repos) outcomes := map[string]reviewBacklogPROutcome{} + bases := map[string]string{} lookups := 0 changed := false for _, repo := range repos { @@ -178,13 +194,14 @@ func ReconcileReviewBacklogBlocks(ctx context.Context, c *Client, logger *slog.L continue } lookups++ - o, err := c.reviewBacklogPROutcome(ctx, repo, rec.BlockedOnPR) + o, base, err := c.reviewBacklogPROutcomeBase(ctx, repo, rec.BlockedOnPR) if err != nil { res.Errors++ logger.Warn("review backlog block sweep: PR lookup failed", "pr", prKey, "error", err) continue } outcomes[prKey] = o + bases[prKey] = base outcome = o } if outcome == reviewBacklogPROpen { @@ -200,6 +217,16 @@ func ReconcileReviewBacklogBlocks(ctx context.Context, c *Client, logger *slog.L var actErr error switch outcome { case reviewBacklogPRMerged: + var closed bool + closed, actErr = c.reviewBacklogSelfVerify(ctx, sink, repo, rec, bases[prKey], logger) + if actErr == nil && closed { + rec.BlockState = reviewBacklogBlockClosed + res.Closed++ + break + } + if actErr != nil { + break + } note := reviewBacklogLiftNote(repo, rec.BlockedOnPR) if canResolve { actErr = resolver.LiftBlock(ctx, repo, ref, note) @@ -212,10 +239,21 @@ func ReconcileReviewBacklogBlocks(ctx context.Context, c *Client, logger *slog.L } case reviewBacklogPRClosed: if supersededBy, isSuperseded := c.pullRequestSupersededBy(ctx, repo, rec.BlockedOnPR); isSuperseded && supersededBy > 0 && supersededBy != rec.BlockedOnPR { - yOutcome, yErr := c.reviewBacklogPROutcome(ctx, repo, supersededBy) + yOutcome, yBase, yErr := c.reviewBacklogPROutcomeBase(ctx, repo, supersededBy) if yErr == nil { switch yOutcome { case reviewBacklogPRMerged: + rec.BlockedOnPR = supersededBy + var closed bool + closed, actErr = c.reviewBacklogSelfVerify(ctx, sink, repo, rec, yBase, logger) + if actErr == nil && closed { + rec.BlockState = reviewBacklogBlockClosed + res.Closed++ + break + } + if actErr != nil { + break + } note := reviewBacklogLiftNote(repo, supersededBy) if canResolve { actErr = resolver.LiftBlock(ctx, repo, ref, note) @@ -224,7 +262,6 @@ func ReconcileReviewBacklogBlocks(ctx context.Context, c *Client, logger *slog.L } if actErr == nil { rec.BlockState = reviewBacklogBlockLifted - rec.BlockedOnPR = supersededBy res.Lifted++ } case reviewBacklogPRClosed: diff --git a/src/pkg/github/review_backlog_dedupe.go b/src/pkg/github/review_backlog_dedupe.go new file mode 100644 index 0000000000..97662104c4 --- /dev/null +++ b/src/pkg/github/review_backlog_dedupe.go @@ -0,0 +1,251 @@ +package github + +import ( + "fmt" + "regexp" + "sort" + "strconv" + "strings" + + "github.com/hivecommons/hive/pkg/outputschema" + "github.com/hivecommons/hive/pkg/review" +) + +// One backlog item per underlying finding per PR, across perspectives and +// heads (hivecommons/hive#15627, #15628, #15629 were three issues for one +// finding on one PR). Two mechanisms: +// +// - a perspective that reports an earlier head's or another perspective's +// note ("Previous style finding still present (non-blocking)") is making +// a REFERENCE, not a finding. Such a report never files; at most it +// leaves one short "still present at " comment on the item it +// refers to, once per head; +// - a genuinely new report that cites the same file as an item already +// filed from the same PR, and the same line, a shared symbol or the same +// (normalised) title, is the same finding seen from another perspective: +// it is tracked against that item instead of filed again. +// +// Reviewers that want to point at a prior note cite it with the +// ReviewRefersToMarker; the hive reads it as a reference too. + +// ReviewRefersToMarker is the comment a reviewer puts in a finding to say it +// restates an existing backlog item rather than raising a new one: +// ``. +const ReviewRefersToMarker = review.ReviewRefersToMarker + +var ( + reviewRefersToRE = regexp.MustCompile(``) + // A role tag such as "[reviewer]" ahead of the backlog stem (another + // filing path adds it; dedupe must read both forms). + reviewBacklogRolePrefixRE = regexp.MustCompile(`^\s*\[[^\]\s]+\]\s*`) + reviewBacklogStemRE = regexp.MustCompile(`(?i)^\s*review backlog\s*\([^)]*\)\s*:\s*`) + // Carry-over wrappers: "Previous style note still present (non-blocking):", + // "Prior finding still present:", "Earlier … unchanged at new head", + // "Previous intent-alignment note (…) still structurally true". + reviewBacklogCarryOverRE = regexp.MustCompile(`(?i)^\s*(?:re-?check(?:\s+of)?\s+(?:the\s+)?)?(?:previous|prior|earlier)\b[^:]*?\b(?:finding|note|concern|item|issue|observation)s?\b`) + reviewBacklogStillRE = regexp.MustCompile(`(?i)\b(?:still\s+(?:present|true|structurally\s+true|applies|holds|stands|open|unaddressed|unchanged|not\s+(?:addressed|met|fixed))|unchanged|carried\s+over|not\s+yet\s+(?:addressed|met|fixed)|persists|remains)\b`) + // Trailing advisory qualifiers that vary per pass: "(non-blocking)", + // "(still present, advisory)", "(advisory, unchanged)". + reviewBacklogQualifierRE = regexp.MustCompile(`(?i)\s*\((?:[\s,;/-]*(?:non-?blocking|advisory|still\s+present|unchanged|info(?:rmational)?|nit|minor|low|carried\s+over|at\s+new\s+head|new\s+head))+[\s,;/-]*\)\s*`) + reviewBacklogWrapperTail = regexp.MustCompile(`(?i)\s*(?:at\s+(?:the\s+)?new\s+head|at\s+head\s+[0-9a-f]{7,40}|since\s+(?:head\s+)?[0-9a-f]{7,40})\s*$`) +) + +// reviewRefersTo lists the backlog item numbers a finding cites with the +// ReviewRefersToMarker in its title or summary. +func reviewRefersTo(f outputschema.Finding) []int { + var out []int + for _, m := range reviewRefersToRE.FindAllStringSubmatch(f.Title+"\n"+f.Summary, -1) { + if n, err := strconv.Atoi(m[1]); err == nil && n > 0 { + out = append(out, n) + } + } + return out +} + +// reviewBacklogCarryOver reports whether a finding is a carry-over +// reference — a restatement of an earlier head's or another perspective's +// note — rather than a finding of its own: its title is wrapped in +// "Previous/Prior/Earlier … still present/unchanged", or it cites an item +// with the refers-to marker. +func reviewBacklogCarryOver(f outputschema.Finding) bool { + if len(reviewRefersTo(f)) > 0 { + return true + } + title := reviewBacklogBareTitle(f.Title) + if !reviewBacklogCarryOverRE.MatchString(title) { + return false + } + head := reviewBacklogCarryOverRE.FindString(title) + rest := title[len(head):] + // "Previous … note" alone, or followed by a still/unchanged clause + // before the first colon, is the wrapper; "Previous finding leaks the + // token" (a new finding about earlier code) is not. + clause := rest + if i := strings.Index(rest, ":"); i >= 0 { + clause = rest[:i] + } + return strings.TrimSpace(strings.Trim(clause, " .-—–()")) == "" || reviewBacklogStillRE.MatchString(clause) +} + +// reviewBacklogBareTitle drops a leading role tag and the "Review backlog +// ():" stem, so titles from either filing path compare equal. +func reviewBacklogBareTitle(title string) string { + t := reviewBacklogRolePrefixRE.ReplaceAllString(strings.TrimSpace(title), "") + t = reviewBacklogStemRE.ReplaceAllString(t, "") + return strings.TrimSpace(t) +} + +// reviewBacklogNormalisedTitle is the finding a title names once its +// per-pass wrapping is stripped: role tag, backlog stem, carry-over wrapper +// ("Previous style note still present (non-blocking):"), trailing advisory +// qualifiers and "at new head" tails, lower-cased with whitespace collapsed. +// Titles of the same finding reported by different perspectives or at +// different heads normalise to the same string; a pure wrapper with no +// finding of its own normalises to "". +func reviewBacklogNormalisedTitle(title string) string { + t := reviewBacklogQualifierRE.ReplaceAllString(reviewBacklogBareTitle(title), " ") + if m := reviewBacklogCarryOverRE.FindStringIndex(t); m != nil { + rest := t[m[1]:] + if i := strings.Index(rest, ":"); i >= 0 && (strings.TrimSpace(strings.Trim(rest[:i], " .-—–()")) == "" || reviewBacklogStillRE.MatchString(rest[:i])) { + t = rest[i+1:] + } else if strings.TrimSpace(strings.Trim(rest, " .-—–()")) == "" || reviewBacklogStillRE.MatchString(rest) { + // "Previous security finding (merge/trust policy expansion) + // unchanged at new head": the parenthetical is the finding. + t = "" + if open := strings.Index(rest, "("); open >= 0 { + if close := strings.Index(rest[open:], ")"); close > 0 { + t = rest[open+1 : open+close] + } + } + } + } + t = reviewBacklogQualifierRE.ReplaceAllString(t, " ") + t = reviewBacklogWrapperTail.ReplaceAllString(t, "") + t = strings.Trim(t, " \t:.-—–") + return normaliseFingerprintText(t) +} + +// reviewBacklogPRFinding is one finding filed (or tracked) from a PR: enough +// of it to tell a later report of the same finding from a new one. +type reviewBacklogPRFinding struct { + File string `json:"file"` + Line int `json:"line,omitempty"` + Title string `json:"title"` + ItemID string `json:"item_id,omitempty"` + ItemKey string `json:"item_key,omitempty"` + ItemNumber int `json:"item_number,omitempty"` + ItemURL string `json:"item_url,omitempty"` +} + +func (r reviewBacklogPRFinding) ref() ReviewBacklogRef { + return ReviewBacklogRef{ID: r.ItemID, Key: r.ItemKey, Number: r.ItemNumber, URL: r.ItemURL} +} + +// reviewBacklogSameFinding reports whether a report is the finding an +// item already tracks for the same PR: same file, and the same line, a +// shared code symbol, or the same normalised title. +func reviewBacklogSameFinding(have reviewBacklogPRFinding, f outputschema.Finding) bool { + file := normaliseFingerprintPath(f.File) + if file == "" || file != normaliseFingerprintPath(have.File) { + return false + } + if have.Line > 0 && f.Line == have.Line { + return true + } + want, got := reviewBacklogNormalisedTitle(have.Title), reviewBacklogNormalisedTitle(f.Title) + if want != "" && want == got { + return true + } + return reviewBacklogSharesSymbol(have.Title, f.Title) +} + +// prFinding returns the item already filed from pr for the same finding. +func (l *reviewBacklogLedger) prFinding(repo string, pr int, f outputschema.Finding) (reviewBacklogPRFinding, bool) { + for _, have := range l.PRFindings[reviewBacklogPRKey(repo, pr)] { + if reviewBacklogSameFinding(have, f) { + return have, true + } + } + return reviewBacklogPRFinding{}, false +} + +// prFindingByItem returns the finding pr filed as item number n, for a +// refers-to citation. +func (l *reviewBacklogLedger) prFindingByItem(repo string, pr, n int) (reviewBacklogPRFinding, bool) { + for _, have := range l.PRFindings[reviewBacklogPRKey(repo, pr)] { + if have.ItemNumber == n && n > 0 { + return have, true + } + } + return reviewBacklogPRFinding{}, false +} + +func (l *reviewBacklogLedger) addPRFinding(repo string, pr int, f outputschema.Finding, ref ReviewBacklogRef, head string) { + key := reviewBacklogPRKey(repo, pr) + // The head the finding was filed at needs no "still present" note. + if head = strings.TrimSpace(head); head != "" && ref.ID != "" { + l.StillPresentHeads[reviewBacklogRepoKey(repo)+":"+ref.ID] = head + } + for _, have := range l.PRFindings[key] { + if have.ItemID == ref.ID && normaliseFingerprintPath(have.File) == normaliseFingerprintPath(f.File) && have.Line == f.Line { + return + } + } + l.PRFindings[key] = append(l.PRFindings[key], reviewBacklogPRFinding{ + File: normaliseFingerprintPath(f.File), Line: f.Line, Title: strings.TrimSpace(f.Title), + ItemID: ref.ID, ItemKey: ref.Key, ItemNumber: ref.Number, ItemURL: ref.URL, + }) +} + +func reviewBacklogPRKey(repo string, pr int) string { + return reviewBacklogRepoKey(repo) + "#" + strconv.Itoa(pr) +} + +// reviewBacklogTitlesEqual reports whether two review-backlog titles name +// the same item once a leading role tag ("[reviewer]") is ignored, so a +// filing dedupes against an existing title with or without the prefix. +// Titles outside the backlog stem never match this way. +func reviewBacklogTitlesEqual(a, b string) bool { + a, b = strings.TrimSpace(a), strings.TrimSpace(b) + if a == b || !reviewBacklogTitleCategoryRE.MatchString(a) || !reviewBacklogTitleCategoryRE.MatchString(b) { + return a == b + } + return reviewBacklogRolePrefixRE.ReplaceAllString(a, "") == reviewBacklogRolePrefixRE.ReplaceAllString(b, "") +} + +// stillPresentDue reports whether a "still present" note for item at head +// has not been posted yet; it records the head when it has not. Rate-limited +// to one note per item per head, so three perspectives re-reporting the same +// note at one head leave one line, not three. +func (l *reviewBacklogLedger) stillPresentDue(repo string, ref ReviewBacklogRef, head string) bool { + head = strings.TrimSpace(head) + if head == "" || ref.ID == "" { + return false + } + key := reviewBacklogRepoKey(repo) + ":" + ref.ID + if l.StillPresentHeads[key] == head { + return false + } + l.StillPresentHeads[key] = head + return true +} + +// reviewBacklogStillPresentMarker tags the once-per-head note. +const reviewBacklogStillPresentMarker = "\nStill present at %s (%s).", reviewBacklogStillPresentMarker, head, short, strings.Join(names, ", ")) +} diff --git a/src/pkg/github/review_backlog_dedupe_test.go b/src/pkg/github/review_backlog_dedupe_test.go new file mode 100644 index 0000000000..8fb05a62f7 --- /dev/null +++ b/src/pkg/github/review_backlog_dedupe_test.go @@ -0,0 +1,175 @@ +package github + +import ( + "context" + "encoding/json" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/outputschema" + "github.com/hivecommons/hive/pkg/review" +) + +// The titles hivecommons/hive filed on one night: every carry-over wrapper +// strips to the finding it restates, so one finding keys to one string +// whichever perspective or head reported it. +func TestReviewBacklogNormalisedTitleCollapsesRealTitles(t *testing.T) { + cases := []struct { + title string + want string + carryOver bool + }{ + // #15627 / #15628 / #15629: one finding on PR #15582. + {"Review backlog (style): New test re-covers prWarnOnce semantics rather than the dispatcher wiring", "new test re-covers prwarnonce semantics rather than the dispatcher wiring", false}, + {"Review backlog (security): Previous style finding still present (non-blocking)", "", true}, + {"Review backlog (intent-alignment): Previous style note still present (non-blocking): TestReviewPromotedWarnOncePerPR exercises prWarnOnce, not planReviewDispatchWith wiring", "testreviewpromotedwarnonceperpr exercises prwarnonce, not planreviewdispatchwith wiring", true}, + // #15630 / #15631 on PR #15621. + {"Review backlog (style): Prior correctness note still present: head-commit fetch precedes the compare", "head-commit fetch precedes the compare", true}, + {"Review backlog (security): Previous correctness note still present: headCommitTime runs before the compare proves the diff empty", "headcommittime runs before the compare proves the diff empty", true}, + // #15659 / #15660: the same finding wrapped two ways. + {"Review backlog (docs-currency): Prior finding still present: waiver will recur per squash", "waiver will recur per squash", true}, + {"Review backlog (security): Previous intent-alignment note (waiver will recur per squash) still structurally true", "waiver will recur per squash", true}, + // #15602, #15182: wrapper with the finding inside. + {"Review backlog (security): Previous intent-alignment finding still present: mobile-shots retry tuning in refactor PR", "mobile-shots retry tuning in refactor pr", true}, + {"Review backlog (correctness): Previous security finding (merge/trust policy expansion) unchanged at new head", "merge/trust policy expansion", true}, + // #15576, #15540: per-pass qualifiers vary; the finding does not. + {"Review backlog (correctness): Retained 'drum cell per digit' clause still contradicts the newer odometer fragment (still present, advisory)", "retained 'drum cell per digit' clause still contradicts the newer odometer fragment", false}, + {"Review backlog (docs-currency): Dashboard help marks still land on index stub page (advisory, unchanged)", "dashboard help marks still land on index stub page", false}, + {"[reviewer] Review backlog (docs-currency): Dashboard help marks still land on index stub page", "dashboard help marks still land on index stub page", false}, + // #15548: "Earlier …" describing the code, not a prior note. + {"Review backlog (docs-currency): Earlier unreleased changelog fragment still advertises 18 px digits and a grown pill", "earlier unreleased changelog fragment still advertises 18 px digits and a grown pill", false}, + {"Previous finding leaks the token: foo", "previous finding leaks the token: foo", false}, + } + for _, tc := range cases { + if got := reviewBacklogNormalisedTitle(tc.title); got != tc.want { + t.Errorf("normalised(%q)\n got %q\nwant %q", tc.title, got, tc.want) + } + if got := reviewBacklogCarryOver(outputschema.Finding{Title: tc.title}); got != tc.carryOver { + t.Errorf("carryOver(%q) = %v, want %v", tc.title, got, tc.carryOver) + } + } + if !reviewBacklogCarryOver(outputschema.Finding{Title: "Retry tuning", Summary: "See for the earlier note."}) { + t.Error("a refers-to marker makes the finding a reference") + } + if got := reviewRefersTo(outputschema.Finding{Summary: " and "}); len(got) != 2 || got[0] != 15600 || got[1] != 7 { + t.Errorf("refersTo = %v", got) + } +} + +func TestReviewBacklogTitlesEqualIgnoresRolePrefix(t *testing.T) { + a := "Review backlog (style): Dashboard help marks still land on index stub page" + if !reviewBacklogTitlesEqual("[reviewer] "+a, a) || !reviewBacklogTitlesEqual(a, "[reviewer] "+a) || !reviewBacklogTitlesEqual(a, a) { + t.Error("the [reviewer] prefix must not defeat the title match") + } + if reviewBacklogTitlesEqual("[reviewer] Fix the thing", "Fix the thing") { + t.Error("only backlog titles are prefix-insensitive") + } + if reviewBacklogTitlesEqual("Review backlog (style): a", "Review backlog (style): b") { + t.Error("different findings never match") + } +} + +func perspectiveReport(t *testing.T, p review.Perspective, number int, head string, findings ...map[string]any) string { + t.Helper() + raw, err := json.Marshal(map[string]any{ + "lane": "review-swarm", "kind": "review", "perspective": string(p), "verdict": "approve", + "repo": "o/r", "number": number, "head_sha": head, "summary": "review", "findings": findings, + "prs_opened": []any{}, "beads_filed": []any{}, + }) + if err != nil { + t.Fatal(err) + } + return string(raw) +} + +func oos(title, file string, line int, summary string) map[string]any { + return map[string]any{"title": title, "severity": "low", "summary": summary, "file": file, "line": line, "review_scope": "out-of-scope"} +} + +// Three perspectives across two heads report one finding of PR 5: one item +// is filed; the other perspectives' "previous … still present" reports are +// references and file nothing; a new head leaves one "still present" note, +// not one per perspective; a carry-over that names nothing tracked is +// dropped instead of filed. +func TestReviewBacklogOneItemPerFindingAcrossPerspectivesAndHeads(t *testing.T) { + withReviewBacklogState(t) + var pending []review.PendingReview + for _, head := range []string{"abc", "bbb"} { + for _, p := range []review.Perspective{review.PerspectiveStyle, review.PerspectiveSecurity, review.PerspectiveIntentAlignment, review.PerspectiveCorrectness} { + pending = append(pending, review.PendingReview{Repo: "o/r", Number: 5, HeadSHA: head, Perspective: p, Agent: "reviewer"}) + } + } + withVerdictDispatchState(t, review.DispatchState{Pending: pending}) + comments := map[int][]string{} + srv := prCommentServer(t, comments) + c := reviewTestClient(t, srv.URL) + c.SetReviewBacklog(func() (bool, int) { return true, 10 }) + sink := &fakeBacklogSink{dest: config.ReviewBacklogLinear} + c.SetReviewBacklogRouting(func() ReviewBacklogRouting { + return ReviewBacklogRouting{ + Severity: config.ReviewSeverityConfig{BlockAt: "P1"}, + Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear, MaxPerPRPerDay: 10}, + Sink: sink, + } + }) + ctx := context.Background() + now := time.Date(2026, 10, 29, 22, 0, 0, 0, time.UTC) + file := "src/cmd/hive/warn_once_pr_test.go" + file5 := func(p review.Perspective, head string, findings ...map[string]any) { + t.Helper() + if err := c.fileOutOfScopeReviewBacklog(ctx, ReviewRequest{Repo: "o/r", Number: 5, Agent: "reviewer", Report: perspectiveReport(t, p, 5, head, findings...)}, now); err != nil { + t.Fatalf("%s@%s: %v", p, head, err) + } + } + + // Head abc: style raises the finding; security and intent-alignment + // restate it (same file, same line / shared symbol). + file5(review.PerspectiveStyle, "abc", oos("New test re-covers prWarnOnce semantics rather than the dispatcher wiring", file, 27, "the test exercises prWarnOnce")) + file5(review.PerspectiveSecurity, "abc", oos("Previous style finding still present (non-blocking)", file, 27, "unchanged")) + file5(review.PerspectiveIntentAlignment, "abc", oos("Previous style note still present (non-blocking): TestReviewPromotedWarnOncePerPR exercises prWarnOnce, not planReviewDispatchWith wiring", file, 30, "unchanged")) + if len(sink.created) != 1 || sink.created[0].Title != "Review backlog (style): New test re-covers prWarnOnce semantics rather than the dispatcher wiring" { + t.Fatalf("created = %+v, want the one style item", sink.created) + } + if len(sink.appends) != 0 { + t.Fatalf("appends at the filing head = %+v, want none", sink.appends) + } + + // Head bbb: all three report it again. One "still present" note. + file5(review.PerspectiveStyle, "bbb", oos("New test re-covers prWarnOnce semantics rather than the dispatcher wiring", file, 27, "still there")) + file5(review.PerspectiveSecurity, "bbb", oos("Previous style finding still present (non-blocking)", file, 27, "unchanged")) + file5(review.PerspectiveIntentAlignment, "bbb", oos("Prior style note unchanged at new head", file, 27, "unchanged")) + if len(sink.created) != 1 { + t.Fatalf("a later head re-filed: %+v", sink.created) + } + if len(sink.appends) != 1 || sink.appends[0].ref.ID != "id-1" { + t.Fatalf("appends = %+v, want one still-present note on id-1", sink.appends) + } + if note := sink.appends[0].comment; !strings.Contains(note, "Still present at bbb") || !strings.Contains(note, "(style)") || !strings.Contains(note, reviewBacklogStillPresentMarker) { + t.Fatalf("still-present note = %q", note) + } + + // A carry-over citing nothing tracked for this PR is a reference to + // nowhere: dropped, never filed. + file5(review.PerspectiveCorrectness, "bbb", oos("Prior security note still present: token leaks in the log line", "pkg/other.go", 4, "unchanged")) + if len(sink.created) != 1 { + t.Fatalf("an unanchored carry-over was filed: %+v", sink.created) + } + // A genuinely new finding elsewhere still files. + file5(review.PerspectiveCorrectness, "bbb", oos("Dispatcher wiring untested", "pkg/other.go", 9, "nothing covers planReviewDispatchWith")) + if len(sink.created) != 2 || sink.created[1].Title != "Review backlog (correctness): Dispatcher wiring untested" { + t.Fatalf("created = %+v, want the new correctness finding filed second", sink.created) + } + + ledger, err := loadReviewBacklog("") + if err != nil { + t.Fatal(err) + } + if got := len(ledger.PRFindings[reviewBacklogPRKey("o/r", 5)]); got != 2 { + t.Fatalf("PR findings tracked = %d, want 2 (one per item)", got) + } + if len(comments[5]) != 2 { + t.Fatalf("PR 5 summary comments = %d, want one per pass that filed", len(comments[5])) + } +} diff --git a/src/pkg/github/review_backlog_routing.go b/src/pkg/github/review_backlog_routing.go index 8f64f5c9a3..5cbffca83a 100644 --- a/src/pkg/github/review_backlog_routing.go +++ b/src/pkg/github/review_backlog_routing.go @@ -158,7 +158,7 @@ func (s githubIssueBacklogSink) CreateItem(ctx context.Context, item ReviewBackl } var ( - reviewBacklogTitleCategoryRE = regexp.MustCompile(`^\s*Review backlog \(([^)]+)\):`) + reviewBacklogTitleCategoryRE = regexp.MustCompile(`^\s*(?:\[[^\]\s]+\]\s*)?Review backlog \(([^)]+)\):`) reviewBacklogPerspectiveRE = regexp.MustCompile(`(?m)^\s*Perspective:\s*(\S+)`) reviewBacklogFileLineRE = regexp.MustCompile(`([A-Za-z0-9_./-]+):(\d+)\b`) reviewBacklogFileNameRE = regexp.MustCompile(`[A-Za-z0-9_./-]*[A-Za-z0-9_-]\.[a-z]{1,5}\b`) @@ -183,9 +183,16 @@ var reviewBacklogNonSymbols = map[string]bool{ // An issue with no recognisable category never matches. func reviewBacklogFoldMatches(p review.Perspective, f outputschema.Finding, title, body string) bool { want := strings.ToLower(strings.TrimSpace(string(p))) - if want == "" || want != reviewBacklogItemCategory(title, body) { + if want == "" { return false } + if cat := reviewBacklogItemCategory(title, body); cat != want { + // Another perspective's item folds only when it is the same + // finding verbatim: same title once per-pass wrapping is stripped, + // cited at the same place (hivecommons/hive#15627). + n := reviewBacklogNormalisedTitle(f.Title) + return cat != "" && n != "" && n == reviewBacklogNormalisedTitle(title) && reviewBacklogNearbyCitation(f, title+"\n"+body) + } return reviewBacklogNearbyCitation(f, title+"\n"+body) || reviewBacklogSharesSymbol(f.Title, title) } @@ -434,6 +441,11 @@ type reviewBacklogFingerprintRecord struct { // BlockState is blocked, lifted or retriage; ReconcileReviewBacklogBlocks // moves it on when BlockedOnPR merges or closes. BlockState string `json:"block_state,omitempty"` + // File and Title are the finding's evidence file (repo-relative) and + // title, kept so the block sweep can check whether the finding still + // applies on the base once BlockedOnPR merges (hivecommons/hive#15627). + File string `json:"file,omitempty"` + Title string `json:"title,omitempty"` } func (r reviewBacklogFingerprintRecord) ref() ReviewBacklogRef { diff --git a/src/pkg/github/review_backlog_selfcheck.go b/src/pkg/github/review_backlog_selfcheck.go new file mode 100644 index 0000000000..7ed383abb6 --- /dev/null +++ b/src/pkg/github/review_backlog_selfcheck.go @@ -0,0 +1,111 @@ +package github + +import ( + "context" + "fmt" + "log/slog" + "sort" + "strconv" + "strings" +) + +// ReviewBacklogCloser is an optional sink capability: a destination that can +// close an item as completed, with a note saying why. Sinks without it keep +// the lift-and-ask behaviour. +type ReviewBacklogCloser interface { + CloseItem(ctx context.Context, repo string, ref ReviewBacklogRef, note string) error +} + +// CloseItem posts note and closes the GitHub issue as completed. +func (s githubIssueBacklogSink) CloseItem(ctx context.Context, repo string, ref ReviewBacklogRef, note string) error { + if ref.Number <= 0 { + return fmt.Errorf("review backlog: GitHub item %q has no issue number", ref.ID) + } + if err := s.c.CreateIssueCommentBudgeted(ctx, repo, ref.Number, note); err != nil { + return err + } + return s.c.CloseIssue(ctx, repo, ref.Number, IssueCloseOptions{StateReason: IssueStateReasonCompleted}) +} + +// reviewBacklogBaseVerdict is what the base-branch check could tell. +type reviewBacklogBaseVerdict int + +const ( + reviewBacklogBaseUnknown reviewBacklogBaseVerdict = iota + reviewBacklogBaseApplies + reviewBacklogBaseGone +) + +// reviewBacklogSelfVerify is the "does it still apply on base?" check the +// lift note used to ask the worker for (hivecommons/hive#15627). Once the +// source PR has merged it reads the finding's file on the base branch: a +// file that no longer exists, or that no longer contains any symbol the +// finding's title names, means the finding is gone and the item is closed as +// completed with a one-line note. Anything less certain — no file recorded, +// a title naming no symbol, a lookup error, or a sink that cannot close — +// reports not closed, and the caller lifts the block as before. +func (c *Client) reviewBacklogSelfVerify(ctx context.Context, sink ReviewBacklogSink, repo string, rec reviewBacklogFingerprintRecord, baseRef string, logger *slog.Logger) (bool, error) { + closer, can := sink.(ReviewBacklogCloser) + if !can || strings.TrimSpace(rec.File) == "" { + return false, nil + } + verdict, why := c.reviewBacklogFindingOnBase(ctx, repo, baseRef, rec.File, rec.Title) + if verdict != reviewBacklogBaseGone { + return false, nil + } + note := fmt.Sprintf("%s\nClosed as completed: %s#%d merged and %s, so this finding no longer applies. Reopen if it does.", blockedUnblockedMarker, repo, rec.BlockedOnPR, why) + if err := closer.CloseItem(ctx, repo, rec.ref(), note); err != nil { + return false, err + } + if rec.ItemNumber > 0 { + c.recordWriteAudit(AuditActionReviewBacklogIssueClosed, hiveWriteMeta(), + WriteTarget{Repo: repo, Number: rec.ItemNumber}, + "pr", strconv.Itoa(rec.BlockedOnPR), + "why", why) + } + if logger != nil { + logger.Info("review backlog block sweep: finding gone from base, item closed", "item", rec.ItemKey, "pr", repo+"#"+strconv.Itoa(rec.BlockedOnPR), "why", why) + } + return true, nil +} + +// reviewBacklogFindingOnBase reads file at baseRef ("" for the default +// branch) and reports whether the finding titled title can still be there. +func (c *Client) reviewBacklogFindingOnBase(ctx context.Context, repo, baseRef, file, title string) (reviewBacklogBaseVerdict, string) { + if c == nil || c.client == nil { + return reviewBacklogBaseUnknown, "" + } + file = normaliseFingerprintPath(file) + if file == "" { + return reviewBacklogBaseUnknown, "" + } + owner, name := c.splitRepo(repo) + base := strings.TrimSpace(baseRef) + if base == "" { + base = "the default branch" + } + exists, err := c.PathExistsAtRef(ctx, owner, name, file, baseRef) + if err != nil { + return reviewBacklogBaseUnknown, "" + } + if !exists { + return reviewBacklogBaseGone, fmt.Sprintf("`%s` no longer exists on %s", file, base) + } + symbols := reviewBacklogSymbols(reviewBacklogBareTitle(title)) + if len(symbols) == 0 { + return reviewBacklogBaseUnknown, "" + } + content, err := c.GetFileContentRef(ctx, owner, name, file, baseRef) + if err != nil { + return reviewBacklogBaseUnknown, "" + } + names := make([]string, 0, len(symbols)) + for s := range symbols { + if strings.Contains(content, s) { + return reviewBacklogBaseApplies, "" + } + names = append(names, "`"+s+"`") + } + sort.Strings(names) + return reviewBacklogBaseGone, fmt.Sprintf("%s is no longer in `%s` on %s", strings.Join(names, ", "), file, base) +} diff --git a/src/pkg/github/review_backlog_selfcheck_test.go b/src/pkg/github/review_backlog_selfcheck_test.go new file mode 100644 index 0000000000..a2b6b4dad1 --- /dev/null +++ b/src/pkg/github/review_backlog_selfcheck_test.go @@ -0,0 +1,152 @@ +package github + +import ( + "context" + "encoding/base64" + "fmt" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/config" +) + +type fakeCloserSink struct { + fakeResolverSink + closed []string + notes map[string]string +} + +func (f *fakeCloserSink) CloseItem(_ context.Context, _ string, ref ReviewBacklogRef, note string) error { + f.closed = append(f.closed, ref.Key) + if f.notes == nil { + f.notes = map[string]string{} + } + f.notes[ref.Key] = note + return nil +} + +// When the source PR merges the hive answers "does it still apply on base?" +// itself: the finding's file gone, or none of the symbols its title names +// left in it, closes the item as completed; a symbol still there lifts the +// block as before; no evidence file (legacy record) lifts; a sink that +// cannot close lifts. +func TestReconcileReviewBacklogBlocksSelfVerifiesOnMerge(t *testing.T) { + withReviewBacklogState(t) + contents := map[string]string{ + "pkg/keep.go": "func prWarnOnce() {}\n", + "pkg/renamed.go": "func warnOncePerPR() {}\n", + } + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch { + case r.URL.Path == "/repos/o/r/pulls/5": + _, _ = io.WriteString(w, `{"number":5,"state":"closed","merged":true,"base":{"ref":"v5"}}`) + case strings.HasPrefix(r.URL.Path, "/repos/o/r/contents/"): + if r.URL.Query().Get("ref") != "v5" { + t.Errorf("contents read at ref %q, want the PR's base v5", r.URL.Query().Get("ref")) + } + p := strings.TrimPrefix(r.URL.Path, "/repos/o/r/contents/") + body, ok := contents[p] + if !ok { + w.WriteHeader(http.StatusNotFound) + _, _ = io.WriteString(w, `{"message":"Not Found"}`) + return + } + _, _ = fmt.Fprintf(w, `{"type":"file","encoding":"base64","name":%q,"path":%q,"content":%q}`, p, p, base64.StdEncoding.EncodeToString([]byte(body))) + default: + w.WriteHeader(http.StatusNotFound) + } + })) + defer srv.Close() + sink := &fakeCloserSink{fakeResolverSink: fakeResolverSink{fakeBacklogSink: fakeBacklogSink{dest: config.ReviewBacklogLinear}}} + c := reviewTestClient(t, srv.URL) + c.SetReviewBacklogRouting(func() ReviewBacklogRouting { + return ReviewBacklogRouting{Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear}, Sink: sink} + }) + ledger := newReviewBacklogLedger() + rec := func(key, file, title string) reviewBacklogFingerprintRecord { + return reviewBacklogFingerprintRecord{Destination: config.ReviewBacklogLinear, ItemID: "id-" + key, ItemKey: key, BlockedOnPR: 5, BlockState: reviewBacklogBlockBlocked, File: file, Title: title} + } + for fp, r := range map[string]reviewBacklogFingerprintRecord{ + "fp-gone-file": rec("GONE-FILE", "pkg/deleted.go", "Review backlog (style): helperThing re-covers prWarnOnce semantics"), + "fp-gone-symbol": rec("GONE-SYM", "pkg/renamed.go", "Review backlog (style): prWarnOnce re-covers the dispatcher"), + "fp-applies": rec("APPLIES", "pkg/keep.go", "Review backlog (style): prWarnOnce re-covers the dispatcher"), + "fp-no-symbol": rec("NOSYM", "pkg/keep.go", "Review backlog (docs-currency): the comment is stale"), + "fp-legacy": rec("LEGACY", "", ""), + } { + ledger.setFingerprint("o/r", fp, r) + } + if err := ledger.save("", time.Now().UTC()); err != nil { + t.Fatal(err) + } + + res := ReconcileReviewBacklogBlocks(context.Background(), c, nil) + if res.Checked != 5 || res.Closed != 2 || res.Lifted != 3 || res.Errors != 0 { + t.Fatalf("result = %+v, want checked 5, closed 2, lifted 3", res) + } + closed := strings.Join(sink.closed, ",") + if !strings.Contains(closed, "GONE-FILE") || !strings.Contains(closed, "GONE-SYM") || len(sink.closed) != 2 { + t.Fatalf("closed = %v", sink.closed) + } + lifted := strings.Join(sink.lifted, ",") + for _, want := range []string{"APPLIES", "NOSYM", "LEGACY"} { + if !strings.Contains(lifted, want) { + t.Errorf("lifted %v lacks %s", sink.lifted, want) + } + } + if n := sink.notes["GONE-FILE"]; !strings.Contains(n, blockedUnblockedMarker) || !strings.Contains(n, "o/r#5 merged and `pkg/deleted.go` no longer exists on v5") { + t.Errorf("gone-file note = %q", n) + } + if n := sink.notes["GONE-SYM"]; !strings.Contains(n, "`prWarnOnce` is no longer in `pkg/renamed.go` on v5") { + t.Errorf("gone-symbol note = %q", n) + } + saved, err := loadReviewBacklog("") + if err != nil { + t.Fatal(err) + } + states := map[string]string{} + for fp, r := range saved.Fingerprints["o/r"] { + states[fp] = r.BlockState + } + if states["fp-gone-file"] != reviewBacklogBlockClosed || states["fp-gone-symbol"] != reviewBacklogBlockClosed || states["fp-applies"] != reviewBacklogBlockLifted { + t.Fatalf("states = %v", states) + } + + // A closed item is never looked at again. + sink.closed, sink.lifted = nil, nil + if res = ReconcileReviewBacklogBlocks(context.Background(), c, nil); res.Checked != 0 || res.Closed+res.Lifted != 0 { + t.Fatalf("second pass = %+v, want nothing to do", res) + } +} + +// A sink without CloseItem keeps the lift-and-ask behaviour even when the +// file is gone. +func TestReconcileReviewBacklogBlocksLiftsWhenSinkCannotClose(t *testing.T) { + withReviewBacklogState(t) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + if r.URL.Path == "/repos/o/r/pulls/5" { + _, _ = io.WriteString(w, `{"number":5,"state":"closed","merged":true,"base":{"ref":"v5"}}`) + return + } + w.WriteHeader(http.StatusNotFound) + })) + defer srv.Close() + sink := &fakeResolverSink{fakeBacklogSink: fakeBacklogSink{dest: config.ReviewBacklogLinear}} + c := reviewTestClient(t, srv.URL) + c.SetReviewBacklogRouting(func() ReviewBacklogRouting { + return ReviewBacklogRouting{Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear}, Sink: sink} + }) + ledger := newReviewBacklogLedger() + ledger.setFingerprint("o/r", "fp", reviewBacklogFingerprintRecord{Destination: config.ReviewBacklogLinear, ItemID: "id", ItemKey: "ENG-1", BlockedOnPR: 5, BlockState: reviewBacklogBlockBlocked, File: "pkg/deleted.go", Title: "Review backlog (style): prWarnOnce"}) + if err := ledger.save("", time.Now().UTC()); err != nil { + t.Fatal(err) + } + if res := ReconcileReviewBacklogBlocks(context.Background(), c, nil); res.Closed != 0 || res.Lifted != 1 || strings.Join(sink.lifted, ",") != "ENG-1" { + t.Fatalf("result = %+v lifted=%v", res, sink.lifted) + } +} diff --git a/src/pkg/github/write_surface_internal.go b/src/pkg/github/write_surface_internal.go index a6e90023f7..ed558ee9f2 100644 --- a/src/pkg/github/write_surface_internal.go +++ b/src/pkg/github/write_surface_internal.go @@ -28,6 +28,11 @@ const ( // filing pass that filed or updated at least one item, in any // destination (GitHub issue, Projects, Linear, Jira). Target is the PR. AuditActionReviewBacklogBatchRouted = "review_backlog_batch_routed" + // AuditActionReviewBacklogIssueClosed is recorded when the block sweep + // closes a backlog issue as completed because its source PR merged and + // the finding's file or symbol is gone from the base + // (review_backlog_selfcheck.go). Target is the backlog issue. + AuditActionReviewBacklogIssueClosed = "review_backlog_issue_closed" // AuditActionHiveLabelApplied is recorded when the hive applies an // existing repo label to a PR: the human-decision label and the review // priority labels (human_decision_label.go). @@ -91,6 +96,7 @@ func InternalWriteAuditActions() []string { AuditActionReviewBacklogIssueFiled, AuditActionReviewBacklogSummaryPosted, AuditActionReviewBacklogBatchRouted, + AuditActionReviewBacklogIssueClosed, AuditActionHiveLabelApplied, AuditActionRecommendationsPosted, AuditActionFleetReportPosted, diff --git a/src/pkg/review/delta.go b/src/pkg/review/delta.go index fa34e8c4d8..f8a6ef26a9 100644 --- a/src/pkg/review/delta.go +++ b/src/pkg/review/delta.go @@ -338,6 +338,7 @@ func deltaSection(pr PullRequest, ctx *DeltaContext) string { b.WriteString("The previous review recorded no findings, so there is nothing to re-check beyond the delta itself.\n") } b.WriteString("Your verdict is for the WHOLE PR at the new head: a previous finding that is still present keeps its verdict; a clean delta on a previously approved PR is approve. Record it with the usual head_sha above.\n") + b.WriteString("Say fixed / still present / no longer applies in your summary. Do not list a still-present out-of-scope finding again under findings: it is already in the backlog, and a re-report (\"Previous … still present\") is discarded, not filed. Cite its backlog issue with `` if you need to point at it. Only in-scope findings that still bear on the verdict, and genuinely new findings in the delta, go under findings.\n") return b.String() } diff --git a/src/pkg/review/prior_notes_prompt_test.go b/src/pkg/review/prior_notes_prompt_test.go new file mode 100644 index 0000000000..8225fe341f --- /dev/null +++ b/src/pkg/review/prior_notes_prompt_test.go @@ -0,0 +1,37 @@ +package review + +import ( + "strings" + "testing" + "time" +) + +// Every prompt that can raise findings tells the reviewer not to re-report +// another perspective's or an earlier head's note as a finding, and how to +// cite one instead (hivecommons/hive#15627, #15628, #15629 were one finding +// reported by three perspectives). +func TestPromptsForbidReReportingPriorNotes(t *testing.T) { + pr := dispatchPR("sha2") + pr.Repo = "acme/hive" + delta := &DeltaContext{PreviousHead: "sha1", PreviousVerdict: VerdictApprove, PreviousRecordedAt: deltaNow.Add(-time.Hour), DiffKnown: true, Files: []string{"a.go"}, ChangedLines: 25} + prompts := map[string]string{ + "perspective": BuildPerspectivePromptWith(PerspectiveStyle, pr, PromptOptions{}), + "combined": BuildCombinedPrompt(pr, DefaultPerspectives, PromptOptions{}), + "delta": deltaSection(pr, delta), + "decision": BuildDecisionPrompt(pr, []Perspective{PerspectiveCorrectness, PerspectiveSecurity}, Aggregate{}, PromptOptions{}), + } + for name, p := range prompts { + if !strings.Contains(p, ReviewRefersToMarker+" #N -->") { + t.Errorf("%s prompt does not name the refers-to marker", name) + } + if !strings.Contains(p, "Previous style note still present") && !strings.Contains(p, "Prior finding unchanged") && !strings.Contains(p, "Previous … still present") { + t.Errorf("%s prompt does not show what a carry-over looks like", name) + } + } + if p := prompts["perspective"]; !strings.Contains(p, "Do NOT re-report another perspective's note") { + t.Errorf("perspective prompt lacks the rule:\n%s", p) + } + if p := prompts["decision"]; !strings.Contains(p, "Each UNDERLYING finding appears once") { + t.Errorf("decision prompt lacks the once-per-finding rule") + } +} diff --git a/src/pkg/review/prompts.go b/src/pkg/review/prompts.go index c2960f391e..4f4f3f77f9 100644 --- a/src/pkg/review/prompts.go +++ b/src/pkg/review/prompts.go @@ -535,6 +535,7 @@ func scopeContractSection(pr PullRequest) string { } b.WriteString("- Classify every finding with review_scope: \"in-scope\" when it affects whether this PR satisfies that contract, or \"out-of-scope\" when it is a real adjacent/pre-existing concern for later work.\n") b.WriteString("- Only in-scope findings may affect your verdict. Out-of-scope findings need file:line evidence too, but they do not block this PR; the hive will file them as backlog issues.\n") + b.WriteString(priorNotesInstruction) if isLinear && linearText != "" { b.WriteString("- Mark each acceptance criterion in that issue delivered, missing, or deferred in your summary.\n\n") return b.String() @@ -547,6 +548,17 @@ func scopeContractSection(pr PullRequest) string { return b.String() } +// priorNotesInstruction keeps a perspective from re-reporting another +// perspective's or an earlier head's note as a finding of its own: each such +// re-report used to become one more backlog issue per perspective per head +// (hivecommons/hive#15627, #15628, #15629 were one finding). The hive drops +// carry-over wrappers and treats ReviewRefersToMarker as a reference. +const priorNotesInstruction = "- Report only findings that are NEW from this perspective at this head. Do NOT re-report another perspective's note, or a note already raised at an earlier head, as a finding of your own: a finding titled \"Previous style note still present\" or \"Prior finding unchanged\" is a reference, not a finding, and the hive discards it instead of filing it. If you must mention a prior note, say so in your summary, or cite its backlog issue inside the finding's summary with ``; the hive treats that as a reference to issue N, never as a new filing.\n" + +// ReviewRefersToMarker is the marker a finding carries to cite an existing +// backlog item instead of raising a new one; pkg/github reads it. +const ReviewRefersToMarker = "` in the summary instead.\n") b.WriteString("dispositions: one object per head finding for this perspective: {\"head\": \"\", \"title\": \"\", \"file\": \"\", \"line\": , \"disposition\": \"confirmed|not_reproducible|out_of_scope\", \"reason\": \"\", \"severity\": \"\"}.\n") b.WriteString(findingSchemaInstruction) b.WriteString("A perspective you could not meaningfully decide is requires_human, not approve.\n") From a226234943d72d6141ad27cdfdadd1bef1509988 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Fri, 30 Oct 2026 00:51:26 -0400 Subject: [PATCH 2/2] review backlog: CI follow-ups - static-src carries the tracker fold (index.html is generated from it) - still-present notes are tracked per PR and item, so a cross-PR re-raise does not re-arm the note on the filing PR - CloseItem consults the write-back gate; ratchet baseline names the file - node harnesses past ARG_MAX read the script from stdin - drop the unused reviewBacklogPROutcome wrapper Signed-off-by: Andrew Anderson --- .../dashboard/stage_tracker_gutter_test.go | 2 +- .../stage_tracker_repo_grouping_test.go | 12 ++- .../stage_tracker_sentinel_lane_test.go | 2 +- src/pkg/dashboard/static-src/css/tracker.css | 2 + .../dashboard/static-src/js/tracker-filter.js | 86 ++++++++++++++++--- .../dashboard/static-src/js/tracker-row.js | 2 +- src/pkg/github/review_backlog.go | 6 +- src/pkg/github/review_backlog_blocks.go | 7 +- src/pkg/github/review_backlog_dedupe.go | 12 ++- src/pkg/github/review_backlog_dedupe_test.go | 6 +- src/pkg/github/review_backlog_selfcheck.go | 8 ++ .../testdata/direct-writes-baseline.txt | 1 + 12 files changed, 115 insertions(+), 31 deletions(-) diff --git a/src/pkg/dashboard/stage_tracker_gutter_test.go b/src/pkg/dashboard/stage_tracker_gutter_test.go index 41842c0bbd..5ee7729856 100644 --- a/src/pkg/dashboard/stage_tracker_gutter_test.go +++ b/src/pkg/dashboard/stage_tracker_gutter_test.go @@ -251,7 +251,7 @@ stageTrackerSetMenuOpen(el, true); assert.ok(el.classList.contains('ham-menu-ope stageTrackerSetMenuOpen(el, false); assert.ok(!el.classList.contains('ham-menu-open')); stageTrackerSetMenuOpen(null, true); // tolerant of a missing panel ` - if out, err := exec.Command(node, "-e", script).CombinedOutput(); err != nil { + if out, err := runNodeStdin(node, script); err != nil { t.Fatalf("node gutter invariant fixture failed: %v\n%s", err, out) } } diff --git a/src/pkg/dashboard/stage_tracker_repo_grouping_test.go b/src/pkg/dashboard/stage_tracker_repo_grouping_test.go index 8be27c46d6..536707c1ff 100644 --- a/src/pkg/dashboard/stage_tracker_repo_grouping_test.go +++ b/src/pkg/dashboard/stage_tracker_repo_grouping_test.go @@ -69,6 +69,14 @@ func TestStageTrackerRepoGroupingWiring(t *testing.T) { } } +// runNodeStdin feeds the script to node over stdin: the lifted harnesses +// are past Linux's per-argument limit for -e. +func runNodeStdin(node, script string) ([]byte, error) { + cmd := exec.Command(node, "-") + cmd.Stdin = strings.NewReader(script) + return cmd.CombinedOutput() +} + func stageTrackerRepoGroupingHarness(t *testing.T, html string) string { t.Helper() return strings.Join([]string{ @@ -321,7 +329,7 @@ const section = stageTrackerRepoSectionHtml('always', 'repo'); assert.ok(section.includes('id="sts-repos"') && /value="always" checked/.test(section) && !/value="multi" checked/.test(section), section); assert.ok(/