From aab696f71d7efbdf83f6115d02b0276dd5cdc568 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Sat, 7 Nov 2026 14:01:56 -0500 Subject: [PATCH 1/3] review backlog: min_severity floor, per-PR lifetime cap, collapse same-family findings Findings below review.backlog.min_severity (default P2) are no longer filed as backlog issues; they are appended to one upserted hive:review-followups comment on the PR. The issue cap is now review.backlog.max_per_pr (default 3) over the PR's lifetime, persisted in the review-backlog ledger, instead of per UTC day (max_per_pr_per_day is read as a retired alias). New findings from one pass that share a perspective and scope are filed as a single checklist item. Operators who want every finding filed as before set min_severity: P3. Fixes #17065 Signed-off-by: Andrew Anderson --- .../changed-review-backlog-min-severity.md | 1 + src/docs/operator-reference/README.md | 3 +- src/docs/review-swarm.md | 10 +- src/docs/work-sources.md | 2 +- src/pkg/config/review_severity.go | 76 +++- src/pkg/config/review_severity_test.go | 36 ++ src/pkg/github/issue_blocked_test.go | 16 +- src/pkg/github/review_backlog.go | 204 ++++------- src/pkg/github/review_backlog_collapse.go | 334 ++++++++++++++++++ .../github/review_backlog_collapse_test.go | 297 ++++++++++++++++ src/pkg/github/review_backlog_dedupe_test.go | 2 +- src/pkg/github/review_backlog_heads_test.go | 6 +- src/pkg/github/review_backlog_routing.go | 43 ++- src/pkg/github/review_backlog_routing_test.go | 137 ++++--- src/pkg/github/review_backlog_scope_test.go | 4 +- .../github/review_backlog_selfcheck_test.go | 4 +- 16 files changed, 945 insertions(+), 230 deletions(-) create mode 100644 changelog.d/changed-review-backlog-min-severity.md create mode 100644 src/pkg/github/review_backlog_collapse.go create mode 100644 src/pkg/github/review_backlog_collapse_test.go diff --git a/changelog.d/changed-review-backlog-min-severity.md b/changelog.d/changed-review-backlog-min-severity.md new file mode 100644 index 0000000000..5cc630a683 --- /dev/null +++ b/changelog.d/changed-review-backlog-min-severity.md @@ -0,0 +1 @@ +- Review backlog: findings below the new `review.backlog.min_severity` (default `P2`) are no longer filed as issues but collected in one upserted `hive:review-followups` comment on the PR; the issue cap `review.backlog.max_per_pr` (default 3) now applies over the PR's lifetime instead of per day (`max_per_pr_per_day` is a retired alias); and new findings sharing a perspective are filed as one checklist item. Set `min_severity: P3` to file every finding as before (hivecommons/hive#17065). diff --git a/src/docs/operator-reference/README.md b/src/docs/operator-reference/README.md index 4528369000..cbe54dd213 100644 --- a/src/docs/operator-reference/README.md +++ b/src/docs/operator-reference/README.md @@ -269,7 +269,8 @@ Go callers keep reading `cfg.Agents`, `cfg.Governor.Gateways` and so on through | `review.severity.block_at` | Unset; owner-only. | Lowest finding priority that blocks merge: `P1` (ship fast, P0–P1 block), `P2` (strict, P0–P2 block) or `P3` (everything blocks). Also drives `classification.review_bots.min_priority` when that key is not set in `hive.yaml` or `hive-project.yaml`. Any other value fails load naming the field. Features -> Review gate -> Severity & backlog. See [review-swarm.md](../review-swarm.md#blocking-line-reviewseverity-reviewbacklog). | | `review.severity.comment_below` / `backlog_below` | `true` / `true`; owner-only. | Whether findings below the blocking line are posted as non-blocking comments and filed to `review.backlog`. | | `review.backlog.destination` | `github_issue`; owner-only. | `github_issue`, `github_project`, `linear` or `jira`. The last three need the matching `governor.work_source.type` (`github_projects`, `linear`, `jira`); on a mismatch the hive falls back to `github_issue`. Destination fields: `project_column_id` (required for `github_project`), `linear_state`, `jira_status`. | -| `review.backlog.labels` / `max_per_pr_per_day` | `[agent/reviewer]` / `10`; owner-only. `from-review`, `origin/review`, and `origin/reviewer` are accepted and mapped to `agent/reviewer`. | Labels on every backlog item and the per-PR-per-day cap (negative fails load; 0 = default). Reviewer is an agent lane, so `agent/reviewer` is the routing/ownership/provenance label. Non-lane hive issue filers continue to use `origin/selfreport`, `origin/governor`, `origin/dashboard`, `origin/forge`, or `origin/convergence`; the one-time startup migration (`/data/origin-label-migration-v4.done`) removes redundant lane-origin labels and backfills only non-lane origins. | +| `review.backlog.labels` / `max_per_pr` | `[agent/reviewer]` / `3`; owner-only. `from-review`, `origin/review`, and `origin/reviewer` are accepted and mapped to `agent/reviewer`. | Labels on every backlog item and the per-PR lifetime cap on items filed (negative fails load; 0 = default; `max_per_pr_per_day` is a retired alias for the size). New findings from one pass sharing a perspective collapse into one checklist item. Reviewer is an agent lane, so `agent/reviewer` is the routing/ownership/provenance label. Non-lane hive issue filers continue to use `origin/selfreport`, `origin/governor`, `origin/dashboard`, `origin/forge`, or `origin/convergence`; the one-time startup migration (`/data/origin-label-migration-v4.done`) removes redundant lane-origin labels and backfills only non-lane origins. | +| `review.backlog.min_severity` | `P2`; owner-only. `P0`–`P3`; invalid fails load. | Lowest finding priority that is filed as a backlog item. Findings below it (P3 `low`/`info` by default, every docs-currency nit included) go to one upserted `hive:review-followups` comment on the PR instead. `P3` files every finding, as before [#17065](https://github.com/hivecommons/hive/issues/17065). | | `review.reviewers.autoscale.*` | `enabled: false`; when on: `min: 1`, `max: 4` (≤ the replicas cap 5), `target_backlog_per_reviewer: 2`, `scale_up_cycles: 2`, `scale_down_cycles: 5`, `cooldown: 5m`, `cpu_per_reviewer: 1.0`, `mem_per_reviewer_mb: 2048`, `agent` unset (first `role: reviewer` agent, else `reviewer_agents[0]`); owner-only. | Sizes the reviewer lane to its backlog ([#14685](https://github.com/hivecommons/hive/issues/14685)): adds a `reviewer-N` replica after `scale_up_cycles` cycles with more PRs waiting for a free reviewer than `target_backlog_per_reviewer` per reviewer (or a wait older than the review-pending stage dwell), drains the top replica after `scale_down_cycles` idle cycles and retires it only when its turn ends. Scale-ups are refused by named guards (`at-max`, `cooldown`, `saturated` kicks, `provider-budget`, `provider-headroom`, `max-parallel-reviews`, cgroup `cpu` / `memory`). Decisions log as `reviewer autoscale: 2→3 reason=…`; state is in `/api/status.reviewerAutoscale`. See [review-swarm.md](../review-swarm.md#reviewer-autoscale-reviewreviewersautoscale). | | `review.contributor_prs.base_sync` | Off by default; owner-only. | Lets PR follow-up use GitHub's update-branch API on hive-authored lane PRs whose head is in a contributor fork, whose `mergeable_state` is `behind`/`dirty`, and whose author enabled "allow edits by maintainers". `behind` PRs get a GitHub-authored merge commit; `dirty`/422 falls back to the `hive-base-moved` repair note and kick. This requires an agent/proxy mode with `ISSUES_PRS_MERGE` because `PUT /pulls/{n}/update-branch` is merge-scoped. It does not rewrite history and does not add a human/bot-authored DCO commit; the repository's "Sign-off survives the squash" check examines the PR's own commits, not GitHub's update-branch merge commit. | | `governor.advisory.post_retry_backoff_s` | `3600` seconds. `HIVE_ADVISORY_POST_RETRY_BACKOFF_S` overrides the default when the key is unset; a negative value disables the pause. | How long the advisory digest write is paused after GitHub refuses it with `403 Resource not accessible by integration`. That refusal is structural (foreign-authored comment, repo dropped from the installation, or repo transferred to another owner), so per-cycle retries only burn API quota. The failed edit first falls back to creating a fresh App-authored comment; if the create is refused too, the error is recorded for the hub health row — naming the new `owner/repo` when the repository has moved — and attempts pause for this window. See [advisory.md](../advisory.md#when-the-write-is-refused). | diff --git a/src/docs/review-swarm.md b/src/docs/review-swarm.md index 7b47bc4a78..b200d5424a 100644 --- a/src/docs/review-swarm.md +++ b/src/docs/review-swarm.md @@ -208,7 +208,8 @@ review: backlog: destination: github_issue # github_issue | github_project | linear | jira labels: [agent/reviewer] # default [agent/reviewer]; from-review, origin/review, and origin/reviewer are mapped to it - max_per_pr_per_day: 10 # default 10 + min_severity: P2 # default P2; P0 | P1 | P2 | P3 — findings below it go to the PR's Follow-ups comment, not the tracker + max_per_pr: 3 # default 3; items per PR over the PR's lifetime (max_per_pr_per_day is a retired alias) ``` ### Blocking line (`review.severity`, `review.backlog`) @@ -264,7 +265,8 @@ Semantics: | `linear_state` | team default | Linear workflow state for new items. | | `jira_status` | project initial status | Jira status for new tickets. | | `labels` | `[agent/reviewer]` | Applied to every item. The retired `from-review`, `origin/review`, and `origin/reviewer` are accepted and mapped to `agent/reviewer`. | -| `max_per_pr_per_day` | `10` | Cap per PR per day. | +| `min_severity` | `P2` | Lowest finding priority that still becomes a backlog item. Findings below it (P3 `low`/`info` by default — every docs-currency nit, for example) are appended to one upserted `hive:review-followups` comment on the PR instead, so nothing is lost and nothing is filed. `P3` restores filing every finding ([#17065](https://github.com/hivecommons/hive/issues/17065)). | +| `max_per_pr` | `3` | Items filed per PR over the PR's lifetime, not per day. `max_per_pr_per_day` is accepted as a retired alias for the size when `max_per_pr` is unset. | **Precedence with `classification.review_bots.min_priority`.** One blocking line governs both the hive reviewer and external review bots. The bot threshold resolves in this order, first non-empty wins: `hive.yaml` `classification.review_bots.min_priority` (including an explicit `all`), then `hive-project.yaml` `classification.review_bots.min_priority`, then `review.severity.block_at`. With none set, every bot finding is routed. `block_at` is never copied into `min_priority`; clearing the explicit override hands control back to `block_at`. Invalid values fail config load with an error naming the field (for example `review.severity.block_at`, `review.backlog.project_column_id`). @@ -277,7 +279,9 @@ When `block_at` is set and `backlog_below` is on, every cited in-scope finding ( - **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. +- **Severity floor.** Findings below `review.backlog.min_severity` (default `P2`, so `low`/`info`) are never filed. They are listed as `- [ ]` task lines in one `` comment on the PR, edited in place on every pass: ticked lines stay ticked, a re-reported nit is not listed twice, and new nits are appended. Operators who want every finding filed set `min_severity: P3` ([#17065](https://github.com/hivecommons/hive/issues/17065)). +- **Same-family collapse.** New findings from one pass that share a perspective and scope (for example three docs-currency nits from one PR) become one item titled `[reviewer] Review backlog (): # — N follow-ups` whose body is a checklist, one `- [ ]` per finding with `file:line`, severity and summary. Each finding's fingerprint is recorded against that one item, so another PR raising any of them comments on it instead of filing again. A single new finding keeps the per-finding title. +- **Cap.** At most `max_per_pr` items (default 3) are filed per PR over the PR's lifetime; the count is persisted in `/data/review-backlog-issues.json` (`pr_item_count`) and never resets. A collapsed item counts once. Findings over the cap are retried on the next review and may land after a human closes an item. 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: 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. diff --git a/src/docs/work-sources.md b/src/docs/work-sources.md index 30027bb5d4..f3e88a3ef6 100644 --- a/src/docs/work-sources.md +++ b/src/docs/work-sources.md @@ -483,7 +483,7 @@ Review findings below the blocking line (`review.severity.block_at`), and cited | `linear` | `linear` | An issue on the team mapped to the PR's repo (`teams[].repo`), in `linear_state` when set. Labels that already exist on the team are applied. | | `jira` | `jira` | A Task in the first `project_keys` project, moved to `jira_status` when set. Labels have their whitespace replaced with `-`. | -If the destination's work source is not active, or cannot be built (for example a missing `api_key`), the hive files a GitHub issue with the `agent/reviewer` label and logs why. Items are de-duplicated per repo by finding fingerprint. When another PR raises the same finding, the hive comments on the existing item instead of filing a new one. At most `max_per_pr_per_day` items are filed or updated per PR per day. See [review-swarm.md](review-swarm.md#backlog-routing) for the fingerprint, item body and audit entries. +If the destination's work source is not active, or cannot be built (for example a missing `api_key`), the hive files a GitHub issue with the `agent/reviewer` label and logs why. Items are de-duplicated per repo by finding fingerprint. When another PR raises the same finding, the hive comments on the existing item instead of filing a new one. Findings below `review.backlog.min_severity` (default `P2`) are held in one Follow-ups comment on the PR instead of filed; new findings sharing a perspective are filed as one checklist item; and at most `max_per_pr` items (default 3) are filed per PR over its lifetime. See [review-swarm.md](review-swarm.md#backlog-routing) for the fingerprint, item body and audit entries. ## Open questions diff --git a/src/pkg/config/review_severity.go b/src/pkg/config/review_severity.go index c2ae07ef06..2e92efdd68 100644 --- a/src/pkg/config/review_severity.go +++ b/src/pkg/config/review_severity.go @@ -75,10 +75,41 @@ const ( // ValidReviewBacklogDestinations lists the accepted destinations in UI order. var ValidReviewBacklogDestinations = []string{ReviewBacklogGitHubIssue, ReviewBacklogGitHubProject, ReviewBacklogLinear, ReviewBacklogJira} -// DefaultReviewBacklogMaxPerPRPerDay caps backlog items filed per PR per day -// when review.backlog.max_per_pr_per_day is unset. +// DefaultReviewBacklogMaxPerPRPerDay is the retired per-day cap default, +// kept so a config that still sets max_per_pr_per_day keeps loading; the +// live cap is EffectiveMaxPerPR. const DefaultReviewBacklogMaxPerPRPerDay = 10 +// DefaultReviewBacklogMaxPerPR caps backlog items filed or re-raised from one +// PR over its whole lifetime when review.backlog.max_per_pr is unset. +const DefaultReviewBacklogMaxPerPR = 3 + +// DefaultReviewBacklogMinSeverity is the lowest priority still filed as a +// backlog item when review.backlog.min_severity is unset: P0–P2 become +// items, P3 (low/info, e.g. docs-currency) is listed in the PR's Follow-ups +// comment instead. +const DefaultReviewBacklogMinSeverity = "P2" + +// ValidReviewBacklogMinSeverity lists the accepted review.backlog.min_severity +// values; P3 files every finding (the pre-hivecommons/hive#17065 behaviour). +var ValidReviewBacklogMinSeverity = []string{"P0", "P1", "P2", "P3"} + +// NormalizeReviewBacklogMinSeverity canonicalises min_severity ("" means the +// default, case and surrounding whitespace ignored). ok is false for +// anything else. +func NormalizeReviewBacklogMinSeverity(v string) (string, bool) { + v = strings.ToUpper(strings.TrimSpace(v)) + if v == "" { + return DefaultReviewBacklogMinSeverity, true + } + for _, p := range ValidReviewBacklogMinSeverity { + if v == p { + return v, true + } + } + return "", false +} + // ReviewBacklogConfig is `review.backlog` (hivecommons/hive#11088): where // below-the-line findings are filed. The review backlog filer in pkg/github // routes them there (hivecommons/hive#11089). @@ -101,9 +132,17 @@ type ReviewBacklogConfig struct { // the retired from-review, origin/review and origin/reviewer names are // accepted and mapped to agent/reviewer. Labels []string `yaml:"labels,omitempty" json:"labels,omitempty"` - // MaxPerPRPerDay caps backlog items filed per PR per day. 0 means - // DefaultReviewBacklogMaxPerPRPerDay. + // MaxPerPRPerDay is the retired per-day cap. Still read as MaxPerPR when + // that is unset, so an existing config keeps a cap of the same size. MaxPerPRPerDay int `yaml:"max_per_pr_per_day,omitempty" json:"max_per_pr_per_day,omitempty"` + // MaxPerPR caps backlog items filed or re-raised from one PR over its + // lifetime. 0 means MaxPerPRPerDay if set, else + // DefaultReviewBacklogMaxPerPR. + MaxPerPR int `yaml:"max_per_pr,omitempty" json:"max_per_pr,omitempty"` + // MinSeverity is the lowest priority (P0–P3) still filed as a backlog + // item; findings below it are listed in the PR's Follow-ups comment + // instead. Empty means DefaultReviewBacklogMinSeverity. + MinSeverity string `yaml:"min_severity,omitempty" json:"min_severity,omitempty"` } // NormalizeReviewBacklogDestination canonicalises a destination ("" means @@ -181,6 +220,29 @@ func (b ReviewBacklogConfig) EffectiveMaxPerPRPerDay() int { return b.MaxPerPRPerDay } +// EffectiveMaxPerPR returns the per-PR lifetime cap: max_per_pr, else the +// retired max_per_pr_per_day when set, else DefaultReviewBacklogMaxPerPR. +func (b ReviewBacklogConfig) EffectiveMaxPerPR() int { + if b.MaxPerPR > 0 { + return b.MaxPerPR + } + if b.MaxPerPRPerDay > 0 { + return b.MaxPerPRPerDay + } + return DefaultReviewBacklogMaxPerPR +} + +// EffectiveMinSeverity returns min_severity with the default applied; an +// invalid value also yields the default so a bad live edit never files +// every finding. +func (b ReviewBacklogConfig) EffectiveMinSeverity() string { + v, ok := NormalizeReviewBacklogMinSeverity(b.MinSeverity) + if !ok { + return DefaultReviewBacklogMinSeverity + } + return v +} + // Validate checks the backlog block, naming the offending field. A mismatch // between destination and the active work source is deliberately not an // error here: switching work source must never stop the hive from loading, @@ -201,5 +263,11 @@ func (b ReviewBacklogConfig) Validate() error { if b.MaxPerPRPerDay < 0 { return fmt.Errorf("review.backlog.max_per_pr_per_day: must not be negative, got %d", b.MaxPerPRPerDay) } + if b.MaxPerPR < 0 { + return fmt.Errorf("review.backlog.max_per_pr: must not be negative, got %d", b.MaxPerPR) + } + if _, ok := NormalizeReviewBacklogMinSeverity(b.MinSeverity); !ok { + return fmt.Errorf("review.backlog.min_severity: %q is not valid (must be %s, or empty)", b.MinSeverity, strings.Join(ValidReviewBacklogMinSeverity, ", ")) + } return nil } diff --git a/src/pkg/config/review_severity_test.go b/src/pkg/config/review_severity_test.go index d9fb0a34b6..d0718bc632 100644 --- a/src/pkg/config/review_severity_test.go +++ b/src/pkg/config/review_severity_test.go @@ -24,6 +24,12 @@ func TestReviewSeverityBacklogDefaults(t *testing.T) { if got := rc.Backlog.EffectiveMaxPerPRPerDay(); got != 10 { t.Fatalf("max_per_pr_per_day default = %d", got) } + if got := rc.Backlog.EffectiveMaxPerPR(); got != DefaultReviewBacklogMaxPerPR { + t.Fatalf("max_per_pr default = %d, want %d", got, DefaultReviewBacklogMaxPerPR) + } + if got := rc.Backlog.EffectiveMinSeverity(); got != "P2" { + t.Fatalf("min_severity default = %q, want P2", got) + } if err := rc.Severity.Validate(); err != nil { t.Fatalf("zero severity must validate: %v", err) } @@ -58,6 +64,11 @@ func TestReviewSeverityBacklogValidate(t *testing.T) { {name: "unknown destination", backlog: ReviewBacklogConfig{Destination: "trello"}, wantErr: "review.backlog.destination"}, {name: "blank label", backlog: ReviewBacklogConfig{Labels: []string{"from-review", " "}}, wantErr: "review.backlog.labels[1]"}, {name: "negative cap", backlog: ReviewBacklogConfig{MaxPerPRPerDay: -1}, wantErr: "review.backlog.max_per_pr_per_day"}, + {name: "negative lifetime cap", backlog: ReviewBacklogConfig{MaxPerPR: -1}, wantErr: "review.backlog.max_per_pr"}, + {name: "min severity lower-case", backlog: ReviewBacklogConfig{MinSeverity: " p3 "}}, + {name: "min severity P0", backlog: ReviewBacklogConfig{MinSeverity: "P0"}}, + {name: "min severity P4", backlog: ReviewBacklogConfig{MinSeverity: "P4"}, wantErr: "review.backlog.min_severity"}, + {name: "min severity word", backlog: ReviewBacklogConfig{MinSeverity: "low"}, wantErr: "review.backlog.min_severity"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { @@ -78,6 +89,31 @@ func TestReviewSeverityBacklogValidate(t *testing.T) { } } +func TestReviewBacklogEffectiveMaxPerPRAndMinSeverity(t *testing.T) { + for _, tc := range []struct { + name string + backlog ReviewBacklogConfig + wantCap int + wantMin string + }{ + {name: "defaults", wantCap: DefaultReviewBacklogMaxPerPR, wantMin: DefaultReviewBacklogMinSeverity}, + {name: "max_per_pr", backlog: ReviewBacklogConfig{MaxPerPR: 5}, wantCap: 5, wantMin: "P2"}, + {name: "retired per-day alias keeps its size", backlog: ReviewBacklogConfig{MaxPerPRPerDay: 8}, wantCap: 8, wantMin: "P2"}, + {name: "max_per_pr over alias", backlog: ReviewBacklogConfig{MaxPerPR: 2, MaxPerPRPerDay: 8}, wantCap: 2, wantMin: "P2"}, + {name: "file everything", backlog: ReviewBacklogConfig{MinSeverity: "p3"}, wantCap: 3, wantMin: "P3"}, + {name: "invalid falls back to default", backlog: ReviewBacklogConfig{MinSeverity: "P9"}, wantCap: 3, wantMin: "P2"}, + } { + t.Run(tc.name, func(t *testing.T) { + if got := tc.backlog.EffectiveMaxPerPR(); got != tc.wantCap { + t.Fatalf("EffectiveMaxPerPR = %d, want %d", got, tc.wantCap) + } + if got := tc.backlog.EffectiveMinSeverity(); got != tc.wantMin { + t.Fatalf("EffectiveMinSeverity = %q, want %q", got, tc.wantMin) + } + }) + } +} + func TestReviewBacklogEffectiveDestinationFollowsWorkSource(t *testing.T) { cases := []struct { dest, ws, want string diff --git a/src/pkg/github/issue_blocked_test.go b/src/pkg/github/issue_blocked_test.go index 9371cd9a6e..286de61a50 100644 --- a/src/pkg/github/issue_blocked_test.go +++ b/src/pkg/github/issue_blocked_test.go @@ -336,15 +336,21 @@ func TestReviewBacklogFilesIntroducedAndTouchedItemsBlockedOnItsPR(t *testing.T) c.SetReviewBacklogRouting(func() ReviewBacklogRouting { return ReviewBacklogRouting{ Severity: config.ReviewSeverityConfig{BlockAt: "P2"}, - Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogGitHubIssue, Labels: []string{"nit", "help wanted"}}, + Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogGitHubIssue, Labels: []string{"nit", "help wanted"}, MinSeverity: "P3"}, } }) recs := captureAudit(c) - report := routingReport(t, 5, "abc", + // Two passes: findings from one pass that share a perspective collapse + // into one item (hivecommons/hive#17065), and this test wants each + // finding's own blocked note. + for _, finding := range []map[string]any{ nit("only-in-pr", "low", "pkg/new.go", 3, "introduced by the PR"), - nit("on-base", "low", "pkg/old.go", 4, "already there")) - if err := c.fileOutOfScopeReviewBacklog(context.Background(), ReviewRequest{Repo: "o/r", Number: 5, Agent: "reviewer", Report: report}, time.Now()); err != nil { - t.Fatal(err) + nit("on-base", "low", "pkg/old.go", 4, "already there"), + } { + report := routingReport(t, 5, "abc", finding) + if err := c.fileOutOfScopeReviewBacklog(context.Background(), ReviewRequest{Repo: "o/r", Number: 5, Agent: "reviewer", Report: report}, time.Now()); err != nil { + t.Fatal(err) + } } if len(issues) != 2 { t.Fatalf("issues = %d, want 2", len(issues)) diff --git a/src/pkg/github/review_backlog.go b/src/pkg/github/review_backlog.go index 50778b1f37..77f2531a74 100644 --- a/src/pkg/github/review_backlog.go +++ b/src/pkg/github/review_backlog.go @@ -42,8 +42,10 @@ type reviewBacklogLedger struct { // Fingerprints maps repo → finding fingerprint → filed item, so a // finding re-raised on any PR updates its existing item. Fingerprints map[string]map[string]reviewBacklogFingerprintRecord `json:"fingerprints,omitempty"` - // PRDailyCount counts items filed or updated per "@". - PRDailyCount map[string]int `json:"pr_daily_count,omitempty"` + // PRItemCount counts items filed or re-raised per "#" over the + // PR's lifetime (hivecommons/hive#17065); the per-day counter it replaces + // let a long-lived PR file max_per_pr_per_day items every UTC day. + PRItemCount map[string]int `json:"pr_item_count,omitempty"` // SplitKeys maps a folded finding's link-marker key to every closed // host issue it has been split out of (hivecommons/hive#13914). A // finding can be split more than once when the split lands in an open @@ -71,7 +73,7 @@ type reviewBacklogLedger struct { // the issue each call creates, so releasing it between the call and the // save would reopen the lost-update window. The cost is that the run // loop's split sweep waits while a review's backlog filing is in flight, -// bounded by the review's per-PR daily cap and the sweep's per-pass cap. +// bounded by the review's per-PR cap and the sweep's per-pass cap. var reviewBacklogLedgerMu sync.Mutex // splitFrom reports whether key was already split out of host. @@ -133,7 +135,7 @@ func newReviewBacklogLedger() *reviewBacklogLedger { SummaryCommented: map[string]bool{}, PRFiledIssueCount: map[string]int{}, Fingerprints: map[string]map[string]reviewBacklogFingerprintRecord{}, - PRDailyCount: map[string]int{}, + PRItemCount: map[string]int{}, SplitKeys: map[string][]int{}, PRFindings: map[string][]reviewBacklogPRFinding{}, StillPresentHeads: map[string]string{}, @@ -167,8 +169,8 @@ func loadReviewBacklog(path string) (*reviewBacklogLedger, error) { if l.Fingerprints == nil { l.Fingerprints = map[string]map[string]reviewBacklogFingerprintRecord{} } - if l.PRDailyCount == nil { - l.PRDailyCount = map[string]int{} + if l.PRItemCount == nil { + l.PRItemCount = map[string]int{} } if l.SplitKeys == nil { l.SplitKeys = map[string][]int{} @@ -208,8 +210,13 @@ func (l *reviewBacklogLedger) save(path string, now time.Time) error { // review.backlog destination (hivecommons/hive#11089) and are de-duplicated // per repo by ReviewFindingFingerprint: a finding another PR already filed is // appended to the existing item instead of filed again. Every item filed or -// updated counts toward the per-PR daily cap, and each pass that wrote -// anything leaves one batch audit entry. +// re-raised counts toward the per-PR lifetime cap (review.backlog.max_per_pr), +// and each pass that wrote anything leaves one batch audit entry. +// +// Findings below review.backlog.min_severity (hivecommons/hive#17065) are +// never items: they are listed in one upserted Follow-ups comment on the PR. +// New findings from one pass that share a perspective and scope are +// collapsed into a single item with a checklist. // // Findings in files changed by the PR or missing on its base are parked on // that PR; findings in untouched files can be worked immediately (#14473). @@ -249,10 +256,10 @@ func (c *Client) fileReviewBacklog(ctx context.Context, req ReviewRequest, now t return err } prKey := ReviewLinkKey(req.Repo, req.Number) - dayKey := reviewBacklogDayKey(prKey, now) - dailyCap := reviewBacklogDailyCap(routing, routed, outOfScopeCap) - dailyRemaining := dailyCap - ledger.PRDailyCount[dayKey] + prCap := reviewBacklogPRCap(routing, routed, outOfScopeCap) + remaining := prCap - ledger.PRItemCount[prKey] outOfScopeRemaining := outOfScopeCap - ledger.PRFiledIssueCount[prKey] + minSeverity := routing.Backlog.EffectiveMinSeverity() sink, labels := c.reviewBacklogTarget(routing) destination := sink.Destination() headSHA := reqHeadSHAFromReports(reports) @@ -265,6 +272,12 @@ func (c *Client) fileReviewBacklog(ctx context.Context, req ReviewRequest, now t // onto an existing item, or already tracked for this PR — for the // decided review's Follow-ups section. var listed []reviewBacklogFiled + // held is every finding below review.backlog.min_severity: listed in + // the PR's Follow-ups comment, never an item. + var held []reviewBacklogHeldFinding + // pending is every finding that needs a new item; filed after the loop + // so findings sharing a perspective collapse into one item. + var pending []reviewBacklogPending var updated, capped int for _, report := range reports { for _, finding := range report.Findings { @@ -278,17 +291,20 @@ func (c *Client) fileReviewBacklog(ctx context.Context, req ReviewRequest, now t if !outOfScope && (!belowLine || !ReviewFindingBelowLine(finding, routing.Severity.BlockAt)) { continue } + if ReviewFindingBelowMinSeverity(finding, minSeverity) { + held = append(held, reviewBacklogHeldFinding{Perspective: report.Perspective, Finding: finding}) + continue + } 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, req.Number, tracked.ref(), headSHA) { + // leaves the once-per-head "still present" note; bounded + // per head, it does not spend the per-PR cap. + if tracked, isTracked := ledger.prFinding(req.Repo, req.Number, finding); isTracked && ledger.stillPresentDue(req.Repo, req.Number, 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 } @@ -322,12 +338,10 @@ func (c *Client) fileReviewBacklog(ctx context.Context, req ReviewRequest, now t 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, req.Number, tracked.ref(), headSHA) { + if ledger.stillPresentDue(req.Repo, req.Number, 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 } @@ -356,7 +370,7 @@ func (c *Client) fileReviewBacklog(ctx context.Context, req ReviewRequest, now t listed = append(listed, reviewBacklogFiled{number: known.ItemNumber, key: known.ItemKey, url: known.ItemURL, title: reviewBacklogIssueTitle(report.Perspective, finding), status: reviewBacklogListedTracked}) continue } - if dailyRemaining <= 0 { + if remaining <= 0 { capped++ continue } @@ -368,123 +382,52 @@ func (c *Client) fileReviewBacklog(ctx context.Context, req ReviewRequest, now t ledger.setFingerprint(req.Repo, fp, known) ledger.Items[key] = record ledger.addPRFinding(req.Repo, req.Number, finding, known.ref(), headSHA) - ledger.PRDailyCount[dayKey]++ - dailyRemaining-- + ledger.PRItemCount[prKey]++ + remaining-- updated++ listed = append(listed, reviewBacklogFiled{number: known.ItemNumber, key: known.ItemKey, url: known.ItemURL, title: reviewBacklogIssueTitle(report.Perspective, finding), status: reviewBacklogListedUpdated}) continue } - if dailyRemaining <= 0 || (outOfScope && outOfScopeRemaining <= 0) { - capped++ - continue - } - title := reviewBacklogIssueTitle(report.Perspective, finding) - missing := baseProbe.FileMissingOnBase(ctx, finding.File) - touched, filesKnown := false, true - if !missing { - touched, filesKnown = changedFiles(ctx, finding.File) - } - blocked := missing || touched || !filesKnown - itemLabels := labels - if blocked { - itemLabels = blockedBacklogLabels(labels) - } - body := c.reviewBacklogIssueBody(req, report.Perspective, finding, headSHA, routing.Severity.BlockAt, fp) - if missing { - body = blockedBacklogNote(req.Repo, req.Number, finding.File, baseProbe.BaseRef(ctx)) + body - } else if blocked { - note := blockedOnReviewedPRNote(req.Repo, req.Number) - if !filesKnown { - note = strings.Replace(note, "is about code that PR is changing", "could not be checked against the PR's changed files", 1) - } - body = note + body - } - ref, err := sink.CreateItem(ctx, ReviewBacklogItem{ - Repo: req.Repo, - Title: title, - Body: body, - Labels: itemLabels, - Perspective: report.Perspective, - Finding: finding, - }) - if err != nil && ref.ID == "" { + pending = append(pending, reviewBacklogPending{perspective: report.Perspective, finding: finding, key: key, fp: fp, record: record, outOfScope: outOfScope}) + } + } + for _, group := range groupReviewBacklogPending(pending) { + if remaining <= 0 || (group[0].outOfScope && outOfScopeRemaining <= 0) { + capped += len(group) + continue + } + filed, ok, createErr := c.createReviewBacklogGroup(ctx, reviewBacklogGroupInput{ + req: req, ledger: ledger, sink: sink, labels: labels, destination: destination, + headSHA: headSHA, blockAt: routing.Severity.BlockAt, baseProbe: baseProbe, changedFiles: changedFiles, + group: group, now: now, + }) + if createErr != nil && !ok { + return createErr + } + if !ok { + continue + } + if group[0].outOfScope { + ledger.PRFiledIssueCount[prKey]++ + outOfScopeRemaining-- + } + ledger.PRItemCount[prKey]++ + remaining-- + filedIssues = append(filedIssues, filed) + listed = append(listed, filed) + if createErr != nil { + // The item exists but a follow-up step (Projects column, + // Jira status) failed: keep the ledger entry so a retry + // does not file it twice. + if err := ledger.save("", now); err != nil { return err } - createErr := err - if ref.ID == "" { - continue - } - if ref.Reused && createErr == nil { - // hivecommons/hive#13604: the comment must carry the whole - // finding or it is lost when the twin closes. - if err := sink.AppendToItem(ctx, req.Repo, ref, c.reviewBacklogFoldedComment(req, report.Perspective, finding, headSHA, fp, key)); err != nil { - return err - } - } - if !ref.Reused && ref.SameFileAs > 0 && createErr == nil { - // hivecommons/hive#13906: the item exists, so a failed - // cross-link must not fail the pass and re-file it. - if err := sink.AppendToItem(ctx, req.Repo, ref, reviewBacklogSameFileComment(finding, ref.SameFileAs)); err != nil { - c.logger.Warn("review backlog: same-file cross-link comment failed", - slog.String("repo", req.Repo), slog.String("item", ref.Key), - slog.Int("same_file_as", ref.SameFileAs), slog.String("error", err.Error())) - } - } - if ref.Number > 0 { - c.recordWriteAudit(AuditActionReviewBacklogIssueFiled, hiveWriteMeta(), - WriteTarget{Repo: req.Repo, Number: ref.Number}, - "pr", strconv.Itoa(req.Number), - "perspective", string(report.Perspective), - "reused", strconv.FormatBool(ref.Reused), - "blocked", strconv.FormatBool(blocked), - "destination", destination, - "review_agent", req.Agent) - } - 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, - ItemKey: ref.Key, - ItemNumber: ref.Number, - ItemURL: ref.URL, - 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 - // state (#13364). Keep source provenance for new items, - // but only park findings related to the PR. - fpRecord.BlockedOnPR = req.Number - if blocked { - fpRecord.BlockState = reviewBacklogBlockBlocked - } - } - ledger.setFingerprint(req.Repo, fp, fpRecord) - if outOfScope { - ledger.PRFiledIssueCount[prKey]++ - outOfScopeRemaining-- - } - ledger.PRDailyCount[dayKey]++ - dailyRemaining-- - filed := reviewBacklogFiled{number: ref.Number, key: ref.Key, url: ref.URL, title: title, folded: ref.Reused, status: reviewBacklogListedFiled} - filedIssues = append(filedIssues, filed) - listed = append(listed, filed) - if createErr != nil { - // The item exists but a follow-up step (Projects column, - // Jira status) failed: keep the ledger entry so a retry - // does not file it twice. - if err := ledger.save("", now); err != nil { - return err - } - return createErr - } + return createErr } } + if len(held) > 0 { + c.upsertReviewFollowUpsComment(ctx, req, held, minSeverity) + } if len(filedIssues) > 0 || updated > 0 { c.recordWriteAudit(AuditActionReviewBacklogBatchRouted, hiveWriteMeta(), WriteTarget{Repo: req.Repo, Number: req.Number}, @@ -494,7 +437,6 @@ func (c *Client) fileReviewBacklog(ctx context.Context, req ReviewRequest, now t "capped", strconv.Itoa(capped), "review_agent", req.Agent) } - ledger.pruneDailyCounts(now) if decided { // Heads mode (#13833): list what this head's findings map to in the // decided review itself, every head, instead of a once-per-PR @@ -517,7 +459,7 @@ func (c *Client) fileReviewBacklog(ctx context.Context, req ReviewRequest, now t return filedIssues[i].key < filedIssues[j].key }) if !ledger.SummaryCommented[prKey] { - if _, err := c.PostNoteOrFold(ctx, req.Repo, req.Number, prstatus.KeyReviewBacklog, reviewBacklogSummaryComment(filedIssues, dailyCap)); err != nil { + if _, err := c.PostNoteOrFold(ctx, req.Repo, req.Number, prstatus.KeyReviewBacklog, reviewBacklogSummaryComment(filedIssues, prCap)); err != nil { return err } c.recordWriteAudit(AuditActionReviewBacklogSummaryPosted, hiveWriteMeta(), @@ -620,7 +562,7 @@ func reviewBacklogSummaryComment(issues []reviewBacklogFiled, cap int) string { fmt.Fprintf(&b, "- %s — %s\n", ref, issue.title) } } - fmt.Fprintf(&b, "\nCap: %d backlog item(s) per PR per day.", cap) + fmt.Fprintf(&b, "\nCap: %d backlog item(s) per PR.", cap) return b.String() } diff --git a/src/pkg/github/review_backlog_collapse.go b/src/pkg/github/review_backlog_collapse.go new file mode 100644 index 0000000000..5dda3ae569 --- /dev/null +++ b/src/pkg/github/review_backlog_collapse.go @@ -0,0 +1,334 @@ +package github + +import ( + "context" + "fmt" + "log/slog" + "sort" + "strconv" + "strings" + "time" + + "github.com/hivecommons/hive/pkg/outputschema" + "github.com/hivecommons/hive/pkg/review" +) + +// reviewFollowUpsCommentMarker tags the one PR comment that lists findings +// held below review.backlog.min_severity (hivecommons/hive#17065). It is +// edited in place on every pass, like the claim ledger, so a PR carries one +// such comment however many reviews it gets. +const reviewFollowUpsCommentMarker = "" + +// reviewFollowUpsSummaryMaxLen bounds the one-line summary quoted per held +// finding in the Follow-ups comment. +const reviewFollowUpsSummaryMaxLen = 200 + +// reviewBacklogHeldFinding is a finding below review.backlog.min_severity: +// listed in the PR's Follow-ups comment, never filed as an item. +type reviewBacklogHeldFinding struct { + Perspective review.Perspective + Finding outputschema.Finding +} + +// reviewBacklogPending is a finding that needs a new backlog item, held +// back until the whole report is read so findings sharing a perspective +// and scope are filed as one item. +type reviewBacklogPending struct { + perspective review.Perspective + finding outputschema.Finding + key string + fp string + record reviewBacklogRecord + outOfScope bool +} + +// groupReviewBacklogPending groups pending findings by family — perspective +// and scope — in first-seen order, keeping each group's findings in report +// order. +func groupReviewBacklogPending(pending []reviewBacklogPending) [][]reviewBacklogPending { + var order []string + groups := map[string][]reviewBacklogPending{} + for _, p := range pending { + k := string(p.perspective) + "\x00" + strconv.FormatBool(p.outOfScope) + if _, ok := groups[k]; !ok { + order = append(order, k) + } + groups[k] = append(groups[k], p) + } + out := make([][]reviewBacklogPending, 0, len(order)) + for _, k := range order { + out = append(out, groups[k]) + } + return out +} + +type reviewBacklogGroupInput struct { + req ReviewRequest + ledger *reviewBacklogLedger + sink ReviewBacklogSink + labels []string + destination string + headSHA string + blockAt string + baseProbe *reviewBacklogBaseProbe + changedFiles func(context.Context, string) (bool, bool) + group []reviewBacklogPending + now time.Time +} + +// createReviewBacklogGroup files one backlog item for group: the finding's +// own item when the group has one, else one collapsed item with a checklist +// (hivecommons/hive#17065). Every finding in the group is recorded in the +// ledger against the item, so a later head or PR raising any of them is +// de-duplicated as before. ok is false when nothing was filed; a non-nil +// error with ok true means the item exists but a follow-up step failed. +func (c *Client) createReviewBacklogGroup(ctx context.Context, in reviewBacklogGroupInput) (reviewBacklogFiled, bool, error) { + req, ledger, sink, group := in.req, in.ledger, in.sink, in.group + if len(group) == 0 { + return reviewBacklogFiled{}, false, nil + } + first := group[0] + perspective := first.perspective + var missingFile string + blocked, filesKnown := false, true + for _, p := range group { + if in.baseProbe.FileMissingOnBase(ctx, p.finding.File) { + blocked = true + if missingFile == "" { + missingFile = p.finding.File + } + continue + } + touched, known := in.changedFiles(ctx, p.finding.File) + if touched || !known { + blocked = true + } + if !known { + filesKnown = false + } + } + itemLabels := in.labels + if blocked { + itemLabels = blockedBacklogLabels(in.labels) + } + var title, body string + if len(group) == 1 { + title = reviewBacklogIssueTitle(perspective, first.finding) + body = c.reviewBacklogIssueBody(req, perspective, first.finding, in.headSHA, in.blockAt, first.fp) + } else { + title = reviewBacklogGroupTitle(req, perspective, len(group)) + body = c.reviewBacklogGroupBody(req, perspective, group, in.headSHA, in.blockAt) + } + switch { + case missingFile != "": + body = blockedBacklogNote(req.Repo, req.Number, missingFile, in.baseProbe.BaseRef(ctx)) + body + case blocked: + note := blockedOnReviewedPRNote(req.Repo, req.Number) + if !filesKnown { + note = strings.Replace(note, "is about code that PR is changing", "could not be checked against the PR's changed files", 1) + } + body = note + body + } + ref, err := sink.CreateItem(ctx, ReviewBacklogItem{ + Repo: req.Repo, + Title: title, + Body: body, + Labels: itemLabels, + Perspective: perspective, + Finding: first.finding, + }) + if err != nil && ref.ID == "" { + return reviewBacklogFiled{}, false, err + } + createErr := err + if ref.ID == "" { + return reviewBacklogFiled{}, false, nil + } + if ref.Reused && createErr == nil { + // hivecommons/hive#13604: the comment must carry the whole + // finding or it is lost when the twin closes; one per finding so + // the split sweep reads each back. + for _, p := range group { + if err := sink.AppendToItem(ctx, req.Repo, ref, c.reviewBacklogFoldedComment(req, perspective, p.finding, in.headSHA, p.fp, p.key)); err != nil { + return reviewBacklogFiled{}, false, err + } + } + } + if !ref.Reused && ref.SameFileAs > 0 && createErr == nil { + // hivecommons/hive#13906: the item exists, so a failed + // cross-link must not fail the pass and re-file it. + if err := sink.AppendToItem(ctx, req.Repo, ref, reviewBacklogSameFileComment(first.finding, ref.SameFileAs)); err != nil { + c.logger.Warn("review backlog: same-file cross-link comment failed", + slog.String("repo", req.Repo), slog.String("item", ref.Key), + slog.Int("same_file_as", ref.SameFileAs), slog.String("error", err.Error())) + } + } + if ref.Number > 0 { + c.recordWriteAudit(AuditActionReviewBacklogIssueFiled, hiveWriteMeta(), + WriteTarget{Repo: req.Repo, Number: ref.Number}, + "pr", strconv.Itoa(req.Number), + "perspective", string(perspective), + "findings", strconv.Itoa(len(group)), + "reused", strconv.FormatBool(ref.Reused), + "blocked", strconv.FormatBool(blocked), + "destination", in.destination, + "review_agent", req.Agent) + } + for _, p := range group { + record := p.record + record.IssueNumber, record.IssueURL, record.ItemKey = ref.Number, ref.URL, ref.Key + ledger.Items[p.key] = record + ledger.addPRFinding(req.Repo, req.Number, p.finding, ref, in.headSHA) + fpRecord := reviewBacklogFingerprintRecord{ + Destination: in.destination, + ItemID: ref.ID, + ItemKey: ref.Key, + ItemNumber: ref.Number, + ItemURL: ref.URL, + PRs: []int{req.Number}, + FiledAt: in.now.UTC(), + UpdatedAt: in.now.UTC(), + File: normaliseFingerprintPath(p.finding.File), + Title: strings.TrimSpace(p.finding.Title), + } + if !ref.Reused { + // A filing folded into an open twin keeps the twin's own + // state (#13364). Keep source provenance for new items, + // but only park findings related to the PR. + fpRecord.BlockedOnPR = req.Number + if blocked { + fpRecord.BlockState = reviewBacklogBlockBlocked + } + } + ledger.setFingerprint(req.Repo, p.fp, fpRecord) + } + return reviewBacklogFiled{number: ref.Number, key: ref.Key, url: ref.URL, title: title, folded: ref.Reused, status: reviewBacklogListedFiled}, true, createErr +} + +// reviewBacklogGroupTitle names a collapsed item: `[reviewer] Review backlog +// (): # — N follow-ups`. It keeps the `Review backlog +// ():` lead every backlog item has, so title canonicalisation +// and the fold matcher treat it like any other item. +func reviewBacklogGroupTitle(req ReviewRequest, p review.Perspective, n int) string { + return reviewBacklogIssueTitle(p, outputschema.Finding{Title: fmt.Sprintf("%s#%d — %d follow-ups", strings.TrimSpace(req.Repo), req.Number, n)}) +} + +// reviewBacklogGroupBody is the body of a collapsed item: one checklist +// entry per finding, each with its severity, evidence, summary and +// fingerprint so nothing a per-finding item would have carried is lost. +func (c *Client) reviewBacklogGroupBody(req ReviewRequest, p review.Perspective, group []reviewBacklogPending, headSHA, blockAt string) string { + owner, repoName := c.splitRepo(req.Repo) + var b strings.Builder + fmt.Fprintf(&b, "Filed from %s in the Hive review of PR #%d: %d %s findings collapsed into one item. Tick each off as it is addressed.\n\n", reviewBacklogReason(group[0].finding, blockAt), req.Number, len(group), p) + fmt.Fprintf(&b, "PR: #%d (%s)\n", req.Number, reviewPRURL(owner, repoName, req.Number)) + fmt.Fprintf(&b, "Perspective: %s\n\n", p) + b.WriteString("### Findings\n\n") + for _, g := range group { + f := g.finding + file := strings.TrimSpace(f.File) + fmt.Fprintf(&b, "- [ ] **%s** — %s (`%s:%d`)\n", strings.TrimSpace(f.Title), reviewFindingSeverityLine(f), file, f.Line) + if link := reviewFindingPermalink(owner, repoName, headSHA, f); link != "" { + fmt.Fprintf(&b, " %s\n", link) + } + if summary := strings.TrimSpace(f.Summary); summary != "" { + fmt.Fprintf(&b, " %s\n", summary) + } + fmt.Fprintf(&b, " Fingerprint: `%s`\n", g.fp) + } + fmt.Fprintf(&b, "\n%s\n", reviewBacklogFooter) + return b.String() +} + +// upsertReviewFollowUpsComment lists the findings held below +// review.backlog.min_severity in the PR's single Follow-ups comment, edited +// in place; entries already there (including ones a human ticked) are kept +// and new ones appended. Best effort: a failure is logged, the pass is not +// failed, and the findings return on the next review of the PR. +func (c *Client) upsertReviewFollowUpsComment(ctx context.Context, req ReviewRequest, held []reviewBacklogHeldFinding, minSeverity string) { + res, err := c.EditIssueMarkerComment(ctx, req.Repo, req.Number, reviewFollowUpsCommentMarker, func(previous string) string { + return renderReviewFollowUpsComment(previous, held, minSeverity) + }) + if err != nil { + c.logger.Warn("review backlog: follow-ups comment not updated", + slog.String("repo", req.Repo), slog.Int("number", req.Number), + slog.Int("held", len(held)), slog.String("error", err.Error())) + return + } + if res.Created || res.Edited { + c.recordWriteAudit(AuditActionReviewBacklogSummaryPosted, hiveWriteMeta(), + WriteTarget{Repo: req.Repo, Number: req.Number}, + "held", strconv.Itoa(len(held)), + "min_severity", minSeverity, + "review_agent", req.Agent) + } +} + +// renderReviewFollowUpsComment merges held into previous (the comment's +// prior body, "" when none) and returns the full comment. Checklist lines +// are keyed on their text after the checkbox so a ticked entry stays ticked +// and a re-reported finding is not listed twice. +func renderReviewFollowUpsComment(previous string, held []reviewBacklogHeldFinding, minSeverity string) string { + type entry struct { + checked bool + text string + } + var entries []entry + seen := map[string]bool{} + for _, line := range strings.Split(previous, "\n") { + trimmed := strings.TrimSpace(line) + var checked bool + var text string + switch { + case strings.HasPrefix(trimmed, "- [ ] "): + text = strings.TrimSpace(strings.TrimPrefix(trimmed, "- [ ] ")) + case strings.HasPrefix(trimmed, "- [x] "), strings.HasPrefix(trimmed, "- [X] "): + checked, text = true, strings.TrimSpace(trimmed[len("- [x] "):]) + default: + continue + } + if text == "" || seen[text] { + continue + } + seen[text] = true + entries = append(entries, entry{checked: checked, text: text}) + } + for _, h := range held { + text := reviewFollowUpLine(h) + if seen[text] { + continue + } + seen[text] = true + entries = append(entries, entry{text: text}) + } + if len(entries) == 0 { + return "" + } + sort.SliceStable(entries, func(i, j int) bool { return entries[i].text < entries[j].text }) + var b strings.Builder + b.WriteString(reviewFollowUpsCommentMarker) + fmt.Fprintf(&b, "\n### Review follow-ups (below `review.backlog.min_severity: %s`)\n", minSeverity) + b.WriteString("Non-blocking findings below the backlog filing line are listed here instead of as issues, one comment per PR, updated in place. Tick them off as they are fixed; set `review.backlog.min_severity: P3` to file every finding.\n\n") + for _, e := range entries { + box := "[ ]" + if e.checked { + box = "[x]" + } + fmt.Fprintf(&b, "- %s %s\n", box, e.text) + } + return b.String() +} + +// reviewFollowUpLine is one held finding's checklist text: +// `**P3 low** docs-currency — (`file:line`): <summary>`. +func reviewFollowUpLine(h reviewBacklogHeldFinding) string { + f := h.Finding + text := fmt.Sprintf("**P%d %s** %s — %s (`%s:%d`)", ReviewFindingPriority(f.Severity), strings.ToLower(strings.TrimSpace(string(f.Severity))), h.Perspective, strings.TrimSpace(f.Title), strings.TrimSpace(f.File), f.Line) + summary := strings.Join(strings.Fields(f.Summary), " ") + if r := []rune(summary); len(r) > reviewFollowUpsSummaryMaxLen { + summary = string(r[:reviewFollowUpsSummaryMaxLen-1]) + "…" + } + if summary != "" { + text += ": " + summary + } + return text +} diff --git a/src/pkg/github/review_backlog_collapse_test.go b/src/pkg/github/review_backlog_collapse_test.go new file mode 100644 index 0000000000..d7d908ca85 --- /dev/null +++ b/src/pkg/github/review_backlog_collapse_test.go @@ -0,0 +1,297 @@ +package github + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/outputschema" + "github.com/hivecommons/hive/pkg/review" +) + +func TestReviewFindingBelowMinSeverity(t *testing.T) { + for _, tc := range []struct { + name string + severity string + minSeverity string + want bool + }{ + {name: "low below default", severity: "low", want: true}, + {name: "info below default", severity: "info", want: true}, + {name: "medium at default", severity: "medium"}, + {name: "high above default", severity: "high"}, + {name: "P3 files everything", severity: "info", minSeverity: "P3"}, + {name: "P1 holds medium", severity: "medium", minSeverity: "p1", want: true}, + {name: "P0 holds high", severity: "high", minSeverity: "P0", want: true}, + {name: "P0 keeps critical", severity: "critical", minSeverity: "P0"}, + {name: "invalid uses default", severity: "low", minSeverity: "P9", want: true}, + } { + t.Run(tc.name, func(t *testing.T) { + f := outputschema.Finding{Severity: outputschema.Severity(tc.severity)} + if got := ReviewFindingBelowMinSeverity(f, tc.minSeverity); got != tc.want { + t.Fatalf("below = %v, want %v", got, tc.want) + } + }) + } +} + +func TestGroupReviewBacklogPending(t *testing.T) { + p := func(persp review.Perspective, title string, oos bool) reviewBacklogPending { + return reviewBacklogPending{perspective: persp, finding: outputschema.Finding{Title: title}, outOfScope: oos} + } + for _, tc := range []struct { + name string + pending []reviewBacklogPending + want [][]string + }{ + {name: "empty"}, + {name: "one", pending: []reviewBacklogPending{p("docs-currency", "a", false)}, want: [][]string{{"a"}}}, + { + name: "same perspective collapses in order", + pending: []reviewBacklogPending{ + p("docs-currency", "a", false), p("correctness", "b", false), p("docs-currency", "c", false), + }, + want: [][]string{{"a", "c"}, {"b"}}, + }, + { + name: "scope splits a perspective", + pending: []reviewBacklogPending{ + p("style", "in", false), p("style", "out", true), p("style", "in2", false), + }, + want: [][]string{{"in", "in2"}, {"out"}}, + }, + } { + t.Run(tc.name, func(t *testing.T) { + groups := groupReviewBacklogPending(tc.pending) + var got [][]string + for _, g := range groups { + var titles []string + for _, f := range g { + titles = append(titles, f.finding.Title) + } + got = append(got, titles) + } + if fmt.Sprint(got) != fmt.Sprint(tc.want) { + t.Fatalf("groups = %v, want %v", got, tc.want) + } + }) + } +} + +func TestRenderReviewFollowUpsComment(t *testing.T) { + held := func(persp review.Perspective, title, sev, file string, line int, summary string) reviewBacklogHeldFinding { + return reviewBacklogHeldFinding{Perspective: persp, Finding: outputschema.Finding{Title: title, Severity: outputschema.Severity(sev), File: file, Line: line, Summary: summary}} + } + docs := held("docs-currency", "stale example", "low", "docs/a.md", 3, "shows the old flag") + docsLine := "- [ ] **P3 low** docs-currency — stale example (`docs/a.md:3`): shows the old flag" + for _, tc := range []struct { + name string + previous string + held []reviewBacklogHeldFinding + want []string + wantNot []string + empty bool + }{ + {name: "nothing", empty: true}, + { + name: "fresh comment", + held: []reviewBacklogHeldFinding{docs}, + want: []string{reviewFollowUpsCommentMarker, "min_severity: P2", docsLine}, + }, + { + name: "re-report is not listed twice", + previous: reviewFollowUpsCommentMarker + "\n### x\n\n" + docsLine + "\n", + held: []reviewBacklogHeldFinding{docs}, + want: []string{docsLine}, + }, + { + name: "ticked entry stays ticked and new one is appended", + previous: reviewFollowUpsCommentMarker + "\n\n- [x] **P3 low** docs-currency — stale example (`docs/a.md:3`): shows the old flag\n", + held: []reviewBacklogHeldFinding{docs, held("style", "naming", "info", "pkg/c.go", 4, "prefer camelCase")}, + want: []string{"- [x] **P3 low** docs-currency — stale example (`docs/a.md:3`): shows the old flag", "- [ ] **P3 info** style — naming (`pkg/c.go:4`): prefer camelCase"}, + wantNot: []string{docsLine}, + }, + { + name: "long summary is collapsed to one line and truncated", + held: []reviewBacklogHeldFinding{held("style", "long", "low", "pkg/l.go", 1, strings.Repeat("word\n", 100))}, + want: []string{"…", "(`pkg/l.go:1`): word word"}, + wantNot: []string{"word\nword"}, + }, + } { + t.Run(tc.name, func(t *testing.T) { + got := renderReviewFollowUpsComment(tc.previous, tc.held, "P2") + if tc.empty { + if got != "" { + t.Fatalf("want empty body, got %q", got) + } + return + } + for _, w := range tc.want { + if !strings.Contains(got, w) { + t.Errorf("body missing %q:\n%s", w, got) + } + } + for _, w := range tc.wantNot { + if strings.Contains(got, w) { + t.Errorf("body must not contain %q:\n%s", w, got) + } + } + if n := strings.Count(got, docsLine[:len("- [ ] **P3 low** docs-currency — stale example")]); n > 1 { + t.Errorf("entry listed %d times:\n%s", n, got) + } + }) + } +} + +// followUpsCommentServer serves the PR's issue comments so the Follow-ups +// marker comment can be created once and then edited in place. +type followUpsCommentServer struct { + bodies []string + posts int + patches int +} + +func (s *followUpsCommentServer) handler(t *testing.T) http.HandlerFunc { + t.Helper() + return func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch { + case r.Method == http.MethodGet && r.URL.Path == "/repos/o/r/issues/5/comments": + var out []map[string]any + for i, b := range s.bodies { + out = append(out, map[string]any{"id": i + 1, "body": b, "user": map[string]any{"login": "hive[bot]"}}) + } + if out == nil { + out = []map[string]any{} + } + _ = json.NewEncoder(w).Encode(out) + case r.Method == http.MethodPost && r.URL.Path == "/repos/o/r/issues/5/comments": + var payload struct { + Body string `json:"body"` + } + _ = json.NewDecoder(r.Body).Decode(&payload) + s.bodies = append(s.bodies, payload.Body) + s.posts++ + fmt.Fprintf(w, `{"id":%d}`, len(s.bodies)) + case r.Method == http.MethodPatch && strings.HasPrefix(r.URL.Path, "/repos/o/r/issues/comments/"): + var id int + _, _ = fmt.Sscanf(r.URL.Path, "/repos/o/r/issues/comments/%d", &id) + var payload struct { + Body string `json:"body"` + } + _ = json.NewDecoder(r.Body).Decode(&payload) + if id < 1 || id > len(s.bodies) { + w.WriteHeader(http.StatusNotFound) + return + } + s.bodies[id-1] = payload.Body + s.patches++ + fmt.Fprintf(w, `{"id":%d}`, id) + default: + w.WriteHeader(http.StatusNotFound) + _, _ = io.WriteString(w, `{"message":"not found"}`) + } + } +} + +// Findings below review.backlog.min_severity are never items: they land in +// one Follow-ups comment per PR, edited in place on later passes, while +// findings at or above the line still file (hivecommons/hive#17065). +func TestReviewBacklogHoldsFindingsBelowMinSeverityInOneComment(t *testing.T) { + for _, tc := range []struct { + name string + minSeverity string + wantItems int + wantHeld bool + }{ + {name: "default P2 holds the P3 nits", wantItems: 1, wantHeld: true}, + {name: "P3 files everything", minSeverity: "P3", wantItems: 2}, + {name: "P1 holds the medium too", minSeverity: "P1", wantItems: 0, wantHeld: true}, + } { + t.Run(tc.name, func(t *testing.T) { + withReviewBacklogState(t) + server := &followUpsCommentServer{} + srv := httptest.NewServer(server.handler(t)) + defer srv.Close() + c := reviewTestClient(t, srv.URL) + sink := &fakeBacklogSink{dest: config.ReviewBacklogLinear} + c.SetReviewBacklogRouting(func() ReviewBacklogRouting { + return ReviewBacklogRouting{ + Severity: config.ReviewSeverityConfig{BlockAt: "P1"}, + Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear, MinSeverity: tc.minSeverity}, + Sink: sink, + } + }) + ctx := context.Background() + now := time.Date(2026, 11, 7, 12, 0, 0, 0, time.UTC) + report := routingReport(t, 5, "abc", + nit("unused variable", "medium", "pkg/a.go", 7, "x is assigned and never read"), + nit("stale example", "low", "docs/a.md", 3, "shows the old flag"), + nit("naming", "info", "pkg/c.go", 4, "prefer camelCase"), + ) + req := ReviewRequest{Repo: "o/r", Number: 5, Agent: "reviewer", Report: report} + if err := c.fileOutOfScopeReviewBacklog(ctx, req, now); err != nil { + t.Fatal(err) + } + if len(sink.created) != tc.wantItems { + t.Fatalf("created = %d, want %d: %+v", len(sink.created), tc.wantItems, sink.created) + } + var followUps []string + for _, b := range server.bodies { + if strings.Contains(b, reviewFollowUpsCommentMarker) { + followUps = append(followUps, b) + } + } + if !tc.wantHeld { + if len(followUps) != 0 { + t.Fatalf("follow-ups comment posted with nothing held: %v", followUps) + } + return + } + if len(followUps) != 1 { + t.Fatalf("follow-ups comments = %d, want 1", len(followUps)) + } + for _, want := range []string{"stale example (`docs/a.md:3`)", "naming (`pkg/c.go:4`)"} { + if !strings.Contains(followUps[0], want) { + t.Errorf("follow-ups comment missing %q:\n%s", want, followUps[0]) + } + } + if tc.minSeverity == "P1" && !strings.Contains(followUps[0], "unused variable") { + t.Errorf("P1 must hold the medium finding too:\n%s", followUps[0]) + } + + // Next head: same nits plus one more. The comment is edited, + // never posted again, and lists the old and the new entries. + again := routingReport(t, 5, "abd", + nit("stale example", "low", "docs/a.md", 3, "shows the old flag"), + nit("naming", "info", "pkg/c.go", 4, "prefer camelCase"), + nit("typo", "low", "pkg/b.go", 9, "receive"), + ) + if err := c.fileOutOfScopeReviewBacklog(ctx, ReviewRequest{Repo: "o/r", Number: 5, Agent: "reviewer", Report: again}, now.Add(time.Hour)); err != nil { + t.Fatal(err) + } + if server.posts != 1 || server.patches != 1 { + t.Fatalf("posts=%d patches=%d, want one create then one edit", server.posts, server.patches) + } + body := server.bodies[0] + for _, want := range []string{"stale example", "naming", "typo (`pkg/b.go:9`)"} { + if !strings.Contains(body, want) { + t.Errorf("edited follow-ups comment missing %q:\n%s", want, body) + } + } + if strings.Count(body, "stale example") != 1 { + t.Errorf("re-reported nit listed twice:\n%s", body) + } + if len(sink.created) != tc.wantItems { + t.Fatalf("held findings were filed on the second pass: %+v", sink.created) + } + }) + } +} diff --git a/src/pkg/github/review_backlog_dedupe_test.go b/src/pkg/github/review_backlog_dedupe_test.go index 3cfebe4b8d..4e0634e6a4 100644 --- a/src/pkg/github/review_backlog_dedupe_test.go +++ b/src/pkg/github/review_backlog_dedupe_test.go @@ -110,7 +110,7 @@ func TestReviewBacklogOneItemPerFindingAcrossPerspectivesAndHeads(t *testing.T) c.SetReviewBacklogRouting(func() ReviewBacklogRouting { return ReviewBacklogRouting{ Severity: config.ReviewSeverityConfig{BlockAt: "P1"}, - Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear, MaxPerPRPerDay: 10}, + Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear, MaxPerPRPerDay: 10, MinSeverity: "P3"}, Sink: sink, } }) diff --git a/src/pkg/github/review_backlog_heads_test.go b/src/pkg/github/review_backlog_heads_test.go index 6cd6fa2dfa..6bebca9216 100644 --- a/src/pkg/github/review_backlog_heads_test.go +++ b/src/pkg/github/review_backlog_heads_test.go @@ -71,7 +71,7 @@ func headsBacklogClient(t *testing.T, srvURL string, sink *fakeBacklogSink) *Cli c.SetReviewBacklogRouting(func() ReviewBacklogRouting { return ReviewBacklogRouting{ Severity: config.ReviewSeverityConfig{BlockAt: "P2"}, - Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear, Labels: []string{"followup"}}, + Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear, Labels: []string{"followup"}, MinSeverity: "P3"}, Sink: sink, } }) @@ -239,7 +239,7 @@ func TestReconcileReviewBacklogBlocks(t *testing.T) { 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} + return ReviewBacklogRouting{Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear, MinSeverity: "P3"}, Sink: sink} }) now := time.Now().UTC() ledger := newReviewBacklogLedger() @@ -336,7 +336,7 @@ func TestReconcileReviewBacklogBlocksSupersede(t *testing.T) { 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} + return ReviewBacklogRouting{Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear, MinSeverity: "P3"}, Sink: sink} }) now := time.Now().UTC() ledger := newReviewBacklogLedger() diff --git a/src/pkg/github/review_backlog_routing.go b/src/pkg/github/review_backlog_routing.go index da4914e014..9a115b57a0 100644 --- a/src/pkg/github/review_backlog_routing.go +++ b/src/pkg/github/review_backlog_routing.go @@ -349,11 +349,12 @@ func appendLabelOnce(labels []string, label string) []string { return append(labels, label) } -// reviewBacklogDailyCap is review.backlog.max_per_pr_per_day when routing is -// configured, else the legacy per-PR cap. -func reviewBacklogDailyCap(r ReviewBacklogRouting, routed bool, legacyCap int) int { +// reviewBacklogPRCap is review.backlog.max_per_pr (per PR lifetime, +// hivecommons/hive#17065) when routing is configured, else the legacy per-PR +// cap. +func reviewBacklogPRCap(r ReviewBacklogRouting, routed bool, legacyCap int) int { if routed { - return r.Backlog.EffectiveMaxPerPRPerDay() + return r.Backlog.EffectiveMaxPerPR() } if legacyCap <= 0 { return DefaultReviewBacklogIssueCap @@ -398,6 +399,22 @@ func ReviewFindingBelowLine(f outputschema.Finding, blockAt string) bool { return ReviewFindingPriority(f.Severity) > line } +// ReviewFindingBelowMinSeverity reports whether f is below +// review.backlog.min_severity (P0–P3): such a finding is listed in the PR's +// Follow-ups comment instead of filed as an item (hivecommons/hive#17065). +// An unrecognised minSeverity is treated as the default. +func ReviewFindingBelowMinSeverity(f outputschema.Finding, minSeverity string) bool { + m, ok := config.NormalizeReviewBacklogMinSeverity(minSeverity) + if !ok { + m = config.DefaultReviewBacklogMinSeverity + } + line, err := strconv.Atoi(strings.TrimPrefix(m, "P")) + if err != nil { + return false + } + return ReviewFindingPriority(f.Severity) > line +} + // ReviewFindingFingerprint is sha256(rule | path | snippet) for a finding: // the finding title is the rule, the cited file the path, and the summary // the snippet. Text is lower-cased with whitespace runs collapsed and paths @@ -480,24 +497,6 @@ func (l *reviewBacklogLedger) setFingerprint(repo, fp string, rec reviewBacklogF l.Fingerprints[key][fp] = rec } -const reviewBacklogDayLayout = "2006-01-02" - -func reviewBacklogDayKey(prKey string, now time.Time) string { - return prKey + "@" + now.UTC().Format(reviewBacklogDayLayout) -} - -// pruneDailyCounts drops per-day counters older than yesterday so the ledger -// does not grow without bound. -func (l *reviewBacklogLedger) pruneDailyCounts(now time.Time) { - cutoff := now.UTC().AddDate(0, 0, -1).Format(reviewBacklogDayLayout) - for k := range l.PRDailyCount { - i := strings.LastIndex(k, "@") - if i < 0 || k[i+1:] < cutoff { - delete(l.PRDailyCount, k) - } - } -} - func reviewPRURL(owner, repo string, number int) string { return fmt.Sprintf("https://github.com/%s/%s/pull/%d", owner, repo, number) } diff --git a/src/pkg/github/review_backlog_routing_test.go b/src/pkg/github/review_backlog_routing_test.go index 1e07931418..91fb34393b 100644 --- a/src/pkg/github/review_backlog_routing_test.go +++ b/src/pkg/github/review_backlog_routing_test.go @@ -94,28 +94,26 @@ func TestReviewBacklogTargetFallsBackToGitHubWithReviewerAgentLabel(t *testing.T } } -func TestReviewBacklogDailyCapAndPrune(t *testing.T) { - if got := reviewBacklogDailyCap(ReviewBacklogRouting{}, false, 0); got != DefaultReviewBacklogIssueCap { - t.Fatalf("unrouted cap = %d, want legacy default %d", got, DefaultReviewBacklogIssueCap) - } - if got := reviewBacklogDailyCap(ReviewBacklogRouting{}, false, 5); got != 5 { - t.Fatalf("unrouted cap = %d, want legacy 5", got) - } - if got := reviewBacklogDailyCap(ReviewBacklogRouting{}, true, 5); got != config.DefaultReviewBacklogMaxPerPRPerDay { - t.Fatalf("routed default cap = %d, want %d", got, config.DefaultReviewBacklogMaxPerPRPerDay) - } - if got := reviewBacklogDailyCap(ReviewBacklogRouting{Backlog: config.ReviewBacklogConfig{MaxPerPRPerDay: 4}}, true, 5); got != 4 { - t.Fatalf("routed cap = %d, want 4", got) - } - now := time.Date(2026, 10, 9, 12, 0, 0, 0, time.UTC) - l := newReviewBacklogLedger() - l.PRDailyCount["o/r#5@2026-10-01"] = 3 - l.PRDailyCount["o/r#5@2026-10-08"] = 2 - l.PRDailyCount[reviewBacklogDayKey("o/r#5", now)] = 1 - l.PRDailyCount["garbage"] = 1 - l.pruneDailyCounts(now) - if len(l.PRDailyCount) != 2 || l.PRDailyCount["o/r#5@2026-10-08"] != 2 || l.PRDailyCount["o/r#5@2026-10-09"] != 1 { - t.Fatalf("pruned counts = %v, want yesterday and today only", l.PRDailyCount) +func TestReviewBacklogPRCap(t *testing.T) { + for _, tc := range []struct { + name string + routing ReviewBacklogRouting + routed bool + legacy int + want int + }{ + {name: "unrouted default", want: DefaultReviewBacklogIssueCap}, + {name: "unrouted legacy", legacy: 5, want: 5}, + {name: "routed default", routed: true, legacy: 5, want: config.DefaultReviewBacklogMaxPerPR}, + {name: "routed max_per_pr", routing: ReviewBacklogRouting{Backlog: config.ReviewBacklogConfig{MaxPerPR: 4}}, routed: true, legacy: 5, want: 4}, + {name: "routed retired per-day alias", routing: ReviewBacklogRouting{Backlog: config.ReviewBacklogConfig{MaxPerPRPerDay: 7}}, routed: true, want: 7}, + {name: "max_per_pr wins over alias", routing: ReviewBacklogRouting{Backlog: config.ReviewBacklogConfig{MaxPerPR: 2, MaxPerPRPerDay: 7}}, routed: true, want: 2}, + } { + t.Run(tc.name, func(t *testing.T) { + if got := reviewBacklogPRCap(tc.routing, tc.routed, tc.legacy); got != tc.want { + t.Fatalf("cap = %d, want %d", got, tc.want) + } + }) } } @@ -233,7 +231,7 @@ func prCommentServer(t *testing.T, bodies map[int][]string) *httptest.Server { return srv } -func TestReviewBacklogRoutesBelowLineFindingsWithDedupAndDailyCap(t *testing.T) { +func TestReviewBacklogRoutesBelowLineFindingsWithDedupAndPRCap(t *testing.T) { withReviewBacklogState(t) comments := map[int][]string{} srv := prCommentServer(t, comments) @@ -243,7 +241,7 @@ func TestReviewBacklogRoutesBelowLineFindingsWithDedupAndDailyCap(t *testing.T) c.SetReviewBacklogRouting(func() ReviewBacklogRouting { return ReviewBacklogRouting{ Severity: config.ReviewSeverityConfig{BlockAt: "P1"}, - Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear, Labels: []string{"nit"}, MaxPerPRPerDay: 2}, + Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear, Labels: []string{"nit"}, MaxPerPR: 2, MinSeverity: "P3"}, Sink: sink, } }) @@ -254,15 +252,13 @@ func TestReviewBacklogRoutesBelowLineFindingsWithDedupAndDailyCap(t *testing.T) pr5 := routingReport(t, 5, "abc", nit("unused variable", "medium", "pkg/a.go", 7, "x is assigned and never read"), nit("blocking bug", "high", "pkg/a.go", 9, "this blocks and must not be filed"), - nit("typo in comment", "low", "pkg/b.go", 3, "receive should be receive"), - nit("naming", "info", "pkg/c.go", 4, "prefer camelCase"), map[string]any{"title": "no evidence", "severity": "low", "summary": "uncited", "review_scope": "in-scope"}, ) if err := c.fileOutOfScopeReviewBacklog(ctx, ReviewRequest{Repo: "o/r", Number: 5, Agent: "reviewer", Report: pr5}, day1); err != nil { t.Fatalf("file PR 5: %v", err) } - if len(sink.created) != 2 { - t.Fatalf("created = %d, want 2 (daily cap) below-line items", len(sink.created)) + if len(sink.created) != 1 { + t.Fatalf("created = %d, want 1 below-line item", len(sink.created)) } first := sink.created[0] if first.Repo != "o/r" || first.Title != "[reviewer] Review backlog (correctness): unused variable" || strings.Join(first.Labels, ",") != "nit,blocked" { @@ -289,17 +285,52 @@ func TestReviewBacklogRoutesBelowLineFindingsWithDedupAndDailyCap(t *testing.T) t.Errorf("filed a blocking or uncited finding: %q", item.Title) } } - if len(comments[5]) != 1 || !strings.Contains(comments[5][0], "ENG-1") || !strings.Contains(comments[5][0], "ENG-2") { - t.Fatalf("PR 5 summary comments = %v, want one listing ENG-1 and ENG-2", comments[5]) + if len(comments[5]) != 1 || !strings.Contains(comments[5][0], "ENG-1") { + t.Fatalf("PR 5 summary comments = %v, want one listing ENG-1", comments[5]) } batch := assertTypedWrite(t, *recs, AuditActionReviewBacklogBatchRouted, "o/r", 5, 1) - for _, want := range []string{"destination=linear", "filed=2", "updated=0", "capped=1"} { + for _, want := range []string{"destination=linear", "filed=1", "updated=0", "capped=0"} { if !strings.Contains(batch.Detail, want) { t.Errorf("batch detail %q missing %q", batch.Detail, want) } } assertTypedWrite(t, *recs, AuditActionReviewBacklogIssueFiled, "", 0, 0) + // A later head adds two P3 nits from the same perspective: one + // collapsed item with a checklist, not two (hivecommons/hive#17065). + pr5b := routingReport(t, 5, "abd", + nit("unused variable", "medium", "pkg/a.go", 7, "x is assigned and never read"), + nit("typo in comment", "low", "pkg/b.go", 3, "receive should be receive"), + nit("naming", "info", "pkg/c.go", 4, "prefer camelCase"), + ) + if err := c.fileOutOfScopeReviewBacklog(ctx, ReviewRequest{Repo: "o/r", Number: 5, Agent: "reviewer", Report: pr5b}, day1.Add(time.Hour)); err != nil { + t.Fatalf("file PR 5 head abd: %v", err) + } + if len(sink.created) != 2 { + t.Fatalf("created = %d, want the two nits collapsed into one item", len(sink.created)) + } + group := sink.created[1] + if group.Title != "[reviewer] Review backlog (correctness): o/r#5 — 2 follow-ups" { + t.Fatalf("collapsed title = %q", group.Title) + } + for _, want := range []string{"- [ ] **typo in comment** — low (P3) (`pkg/b.go:3`)", "- [ ] **naming** — info (P3) (`pkg/c.go:4`)", "https://github.com/o/r/blob/abd/pkg/b.go#L3", "prefer camelCase", "2 correctness findings collapsed into one item"} { + if !strings.Contains(group.Body, want) { + t.Errorf("collapsed body missing %q:\n%s", want, group.Body) + } + } + // The cap is per PR lifetime: a third pass, even a day later, files + // nothing new. + pr5c := routingReport(t, 5, "abe", nit("shadowed err", "medium", "pkg/d.go", 2, "err is shadowed")) + if err := c.fileOutOfScopeReviewBacklog(ctx, ReviewRequest{Repo: "o/r", Number: 5, Agent: "reviewer", Report: pr5c}, day1.Add(48*time.Hour)); err != nil { + t.Fatal(err) + } + if len(sink.created) != 2 { + t.Fatalf("per-PR cap reset: created = %d, want still 2", len(sink.created)) + } + if len(comments[5]) != 1 { + t.Fatalf("PR 5 summary comments = %d, want still one", len(comments[5])) + } + // PR 6 re-raises the first finding: reflowed text, other line, other head. pr6 := routingReport(t, 6, "def", nit("Unused Variable", "medium", "./pkg/a.go", 42, "x is assigned\nand never read"), @@ -336,22 +367,18 @@ func TestReviewBacklogRoutesBelowLineFindingsWithDedupAndDailyCap(t *testing.T) if !ok || fmt.Sprint(rec.PRs) != "[5 6]" || rec.Destination != config.ReviewBacklogLinear || rec.ItemKey != "ENG-1" { t.Fatalf("fingerprint record = %+v ok=%v, want PRs [5 6] on linear ENG-1", rec, ok) } - - // Same day, PR 5 again: still capped. Next day: the capped finding files. - if err := c.fileOutOfScopeReviewBacklog(ctx, ReviewRequest{Repo: "o/r", Number: 5, Agent: "reviewer", Report: pr5}, day1.Add(time.Hour)); err != nil { - t.Fatal(err) - } - if len(sink.created) != 2 { - t.Fatalf("same-day re-review created past the cap: %d", len(sink.created)) - } - if err := c.fileOutOfScopeReviewBacklog(ctx, ReviewRequest{Repo: "o/r", Number: 5, Agent: "reviewer", Report: pr5}, day1.Add(24*time.Hour)); err != nil { - t.Fatal(err) - } - if len(sink.created) != 3 || !strings.Contains(sink.created[2].Title, "naming") { - t.Fatalf("next day: created = %d, want the capped finding filed", len(sink.created)) + // Both collapsed findings map to the collapsed item. + for _, f := range []outputschema.Finding{ + {Title: "typo in comment", File: "pkg/b.go", Summary: "receive should be receive"}, + {Title: "naming", File: "pkg/c.go", Summary: "prefer camelCase"}, + } { + rec, ok := ledger.fingerprint("o/r", ReviewFindingFingerprint(f)) + if !ok || rec.ItemKey != "ENG-2" { + t.Fatalf("%s: fingerprint record = %+v ok=%v, want ENG-2", f.Title, rec, ok) + } } - if len(comments[5]) != 1 { - t.Fatalf("PR 5 summary comments = %d, want still one", len(comments[5])) + if got := ledger.PRItemCount[ReviewLinkKey("o/r", 5)]; got != 2 { + t.Fatalf("PR 5 lifetime count = %d, want 2", got) } } @@ -367,7 +394,7 @@ func TestReviewBacklogNoBlockingLineLeavesInScopeFindingsAlone(t *testing.T) { backlogBelow := false c.SetReviewBacklog(func() (bool, int) { return false, 3 }) c.SetReviewBacklogRouting(func() ReviewBacklogRouting { - return ReviewBacklogRouting{Severity: config.ReviewSeverityConfig{BlockAt: "P1", BacklogBelow: &backlogBelow}, Sink: sink} + return ReviewBacklogRouting{Backlog: config.ReviewBacklogConfig{MinSeverity: "P3"}, Severity: config.ReviewSeverityConfig{BlockAt: "P1", BacklogBelow: &backlogBelow}, Sink: sink} }) if err := c.fileOutOfScopeReviewBacklog(context.Background(), ReviewRequest{Repo: "o/r", Number: 5, Agent: "reviewer", Report: report}, time.Now()); err != nil { t.Fatal(err) @@ -384,7 +411,7 @@ func TestReviewBacklogCreateErrors(t *testing.T) { c.SetReviewBacklogRouting(func() ReviewBacklogRouting { return ReviewBacklogRouting{ Severity: config.ReviewSeverityConfig{BlockAt: "P2"}, - Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogJira}, + Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogJira, MinSeverity: "P3"}, Sink: sink, } }) @@ -443,7 +470,7 @@ func TestReviewBacklogGitHubIssueDestinationOverHTTP(t *testing.T) { // linear is not active, so the fallback GitHub issue sink is used. return ReviewBacklogRouting{ Severity: config.ReviewSeverityConfig{BlockAt: "P2"}, - Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear, Labels: []string{"nit"}}, + Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear, Labels: []string{"nit"}, MinSeverity: "P3"}, } }) recs := captureAudit(c) @@ -494,7 +521,7 @@ func TestReviewBacklogRefilesWhenLedgerItemClosed(t *testing.T) { c.SetReviewBacklogRouting(func() ReviewBacklogRouting { return ReviewBacklogRouting{ Severity: config.ReviewSeverityConfig{BlockAt: "P1"}, - Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear, MaxPerPRPerDay: 5}, + Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear, MaxPerPRPerDay: 5, MinSeverity: "P3"}, Sink: sink, } }) @@ -535,7 +562,7 @@ func TestReviewBacklogStateProbeErrorAppendsAndSamePRSkipsProbe(t *testing.T) { c.SetReviewBacklogRouting(func() ReviewBacklogRouting { return ReviewBacklogRouting{ Severity: config.ReviewSeverityConfig{BlockAt: "P1"}, - Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear, MaxPerPRPerDay: 5}, + Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear, MaxPerPRPerDay: 5, MinSeverity: "P3"}, Sink: sink, } }) @@ -658,7 +685,7 @@ func TestReviewBacklogGitHubSinkFoldsIntoOpenAgentTwin(t *testing.T) { c.SetReviewBacklogRouting(func() ReviewBacklogRouting { return ReviewBacklogRouting{ Severity: config.ReviewSeverityConfig{BlockAt: "P2"}, - Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogGitHubIssue, Labels: []string{"nit"}}, + Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogGitHubIssue, Labels: []string{"nit"}, MinSeverity: "P3"}, } }) finding := nit("nit", "low", "pkg/a.go", 3, "minor") @@ -708,7 +735,7 @@ func TestReviewBacklogFoldedTwinKeepsFullFinding(t *testing.T) { c.SetReviewBacklogRouting(func() ReviewBacklogRouting { return ReviewBacklogRouting{ Severity: config.ReviewSeverityConfig{BlockAt: "P1"}, - Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear, Labels: []string{"nit"}, MaxPerPRPerDay: 5}, + Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear, Labels: []string{"nit"}, MaxPerPRPerDay: 5, MinSeverity: "P3"}, Sink: sink, } }) @@ -835,7 +862,7 @@ func TestReviewBacklogGitHubSinkReusedTwinDoesNotInheritBlocked(t *testing.T) { c.SetReviewBacklogRouting(func() ReviewBacklogRouting { return ReviewBacklogRouting{ Severity: config.ReviewSeverityConfig{BlockAt: "P2"}, - Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogGitHubIssue, Labels: []string{"nit", "help wanted"}}, + Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogGitHubIssue, Labels: []string{"nit", "help wanted"}, MinSeverity: "P3"}, } }) recs := captureAudit(c) @@ -928,7 +955,7 @@ func TestReviewBacklogGitHubSinkFilesSeparatelyForSameFileDifferentFinding(t *te c.SetReviewBacklogRouting(func() ReviewBacklogRouting { return ReviewBacklogRouting{ Severity: config.ReviewSeverityConfig{BlockAt: "P2"}, - Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogGitHubIssue, Labels: []string{"nit"}}, + Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogGitHubIssue, Labels: []string{"nit"}, MinSeverity: "P3"}, } }) finding := nit("nit", "low", "pkg/a.go", 3, "minor") diff --git a/src/pkg/github/review_backlog_scope_test.go b/src/pkg/github/review_backlog_scope_test.go index b34feb74da..7580399164 100644 --- a/src/pkg/github/review_backlog_scope_test.go +++ b/src/pkg/github/review_backlog_scope_test.go @@ -61,11 +61,11 @@ func TestReviewBacklogBlocksOnlyRelatedFiles(t *testing.T) { defer srv.Close() sink := &fakeBacklogSink{dest: config.ReviewBacklogLinear} c := headsBacklogClient(t, srv.URL, sink) - report := routingReport(t, 5, "abc", nit("first", "low", file, 7, "evidence"), nit("second", "low", file, 8, "other evidence")) + report := routingReport(t, 5, "abc", nit("first", "low", file, 7, "evidence")) if err := c.fileOutOfScopeReviewBacklog(context.Background(), ReviewRequest{Repo: "o/r", Number: 5, Agent: "reviewer", Report: report}, time.Now()); err != nil { t.Fatal(err) } - if len(sink.created) != 2 { + if len(sink.created) != 1 { t.Fatalf("created %d items", len(sink.created)) } for _, item := range sink.created { diff --git a/src/pkg/github/review_backlog_selfcheck_test.go b/src/pkg/github/review_backlog_selfcheck_test.go index a2b6b4dad1..82be5c8237 100644 --- a/src/pkg/github/review_backlog_selfcheck_test.go +++ b/src/pkg/github/review_backlog_selfcheck_test.go @@ -65,7 +65,7 @@ func TestReconcileReviewBacklogBlocksSelfVerifiesOnMerge(t *testing.T) { 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} + return ReviewBacklogRouting{Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear, MinSeverity: "P3"}, Sink: sink} }) ledger := newReviewBacklogLedger() rec := func(key, file, title string) reviewBacklogFingerprintRecord { @@ -139,7 +139,7 @@ func TestReconcileReviewBacklogBlocksLiftsWhenSinkCannotClose(t *testing.T) { 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} + return ReviewBacklogRouting{Backlog: config.ReviewBacklogConfig{Destination: config.ReviewBacklogLinear, MinSeverity: "P3"}, 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"}) From 63b398f3ac87f925d0d4426a3480816a2b01627b Mon Sep 17 00:00:00 2001 From: Andrew Anderson <andy@clubanderson.com> Date: Sat, 7 Nov 2026 14:10:28 -0500 Subject: [PATCH 2/3] review backlog: tests follow collapsed items and uncapped still-present notes Signed-off-by: Andrew Anderson <andy@clubanderson.com> --- .../github/review_backlog_collapse_test.go | 30 +++++++++++++++---- src/pkg/github/review_backlog_routing_test.go | 12 ++++++-- src/pkg/github/review_request_watcher_test.go | 14 +++++---- src/pkg/github/write_surface_internal_test.go | 10 ++++--- 4 files changed, 47 insertions(+), 19 deletions(-) diff --git a/src/pkg/github/review_backlog_collapse_test.go b/src/pkg/github/review_backlog_collapse_test.go index d7d908ca85..0306128efe 100644 --- a/src/pkg/github/review_backlog_collapse_test.go +++ b/src/pkg/github/review_backlog_collapse_test.go @@ -154,7 +154,6 @@ func TestRenderReviewFollowUpsComment(t *testing.T) { // marker comment can be created once and then edited in place. type followUpsCommentServer struct { bodies []string - posts int patches int } @@ -178,7 +177,6 @@ func (s *followUpsCommentServer) handler(t *testing.T) http.HandlerFunc { } _ = json.NewDecoder(r.Body).Decode(&payload) s.bodies = append(s.bodies, payload.Body) - s.posts++ fmt.Fprintf(w, `{"id":%d}`, len(s.bodies)) case r.Method == http.MethodPatch && strings.HasPrefix(r.URL.Path, "/repos/o/r/issues/comments/"): var id int @@ -212,7 +210,9 @@ func TestReviewBacklogHoldsFindingsBelowMinSeverityInOneComment(t *testing.T) { wantHeld bool }{ {name: "default P2 holds the P3 nits", wantItems: 1, wantHeld: true}, - {name: "P3 files everything", minSeverity: "P3", wantItems: 2}, + // All three share the correctness perspective, so filing them + // yields one collapsed item, not three. + {name: "P3 files everything", minSeverity: "P3", wantItems: 1}, {name: "P1 holds the medium too", minSeverity: "P1", wantItems: 0, wantHeld: true}, } { t.Run(tc.name, func(t *testing.T) { @@ -243,6 +243,11 @@ func TestReviewBacklogHoldsFindingsBelowMinSeverityInOneComment(t *testing.T) { if len(sink.created) != tc.wantItems { t.Fatalf("created = %d, want %d: %+v", len(sink.created), tc.wantItems, sink.created) } + if tc.minSeverity == "P3" { + if got := sink.created[0].Title; !strings.HasSuffix(got, "o/r#5 — 3 follow-ups") { + t.Fatalf("P3 must file every finding, collapsed: title = %q", got) + } + } var followUps []string for _, b := range server.bodies { if strings.Contains(b, reviewFollowUpsCommentMarker) { @@ -277,10 +282,23 @@ func TestReviewBacklogHoldsFindingsBelowMinSeverityInOneComment(t *testing.T) { if err := c.fileOutOfScopeReviewBacklog(ctx, ReviewRequest{Repo: "o/r", Number: 5, Agent: "reviewer", Report: again}, now.Add(time.Hour)); err != nil { t.Fatal(err) } - if server.posts != 1 || server.patches != 1 { - t.Fatalf("posts=%d patches=%d, want one create then one edit", server.posts, server.patches) + // The PR's once-per-PR backlog summary is also a POST; the + // Follow-ups comment itself is created once and then edited. + var followUpPosts int + for _, b := range server.bodies { + if strings.Contains(b, reviewFollowUpsCommentMarker) { + followUpPosts++ + } + } + if followUpPosts != 1 || server.patches != 1 { + t.Fatalf("follow-ups comments=%d patches=%d, want one create then one edit", followUpPosts, server.patches) + } + body := followUps[0] + for _, b := range server.bodies { + if strings.Contains(b, reviewFollowUpsCommentMarker) { + body = b + } } - body := server.bodies[0] for _, want := range []string{"stale example", "naming", "typo (`pkg/b.go:9`)"} { if !strings.Contains(body, want) { t.Errorf("edited follow-ups comment missing %q:\n%s", want, body) diff --git a/src/pkg/github/review_backlog_routing_test.go b/src/pkg/github/review_backlog_routing_test.go index 91fb34393b..b351f51423 100644 --- a/src/pkg/github/review_backlog_routing_test.go +++ b/src/pkg/github/review_backlog_routing_test.go @@ -344,10 +344,16 @@ func TestReviewBacklogRoutesBelowLineFindingsWithDedupAndPRCap(t *testing.T) { if len(sink.created) != 2 { t.Fatalf("re-raise created a new item: %d items", len(sink.created)) } - if len(sink.appends) != 1 { - t.Fatalf("appends = %d, want one update of the existing item", len(sink.appends)) + // Head abd re-reported "unused variable", so ENG-1 carries one + // still-present note (bounded per head, not by the cap); PR 6's + // re-raise is the only other update. + if len(sink.appends) != 2 { + t.Fatalf("appends = %d, want one still-present note and one re-raise", len(sink.appends)) } - up := sink.appends[0] + if !strings.Contains(sink.appends[0].comment, reviewBacklogStillPresentMarker) || sink.appends[0].ref.Key != "ENG-1" { + t.Fatalf("first append = %+v, want a still-present note on ENG-1", sink.appends[0]) + } + up := sink.appends[1] if up.ref.ID != "id-1" || up.ref.Key != "ENG-1" { t.Fatalf("updated %+v, want ENG-1", up.ref) } diff --git a/src/pkg/github/review_request_watcher_test.go b/src/pkg/github/review_request_watcher_test.go index 4f12d0e976..05b421f419 100644 --- a/src/pkg/github/review_request_watcher_test.go +++ b/src/pkg/github/review_request_watcher_test.go @@ -497,7 +497,9 @@ func TestReviewRequestWatcherFilesOutOfScopeBacklogOnce(t *testing.T) { if !containsString(payload.Labels, reviewBacklogLabel) { t.Errorf("created issue labels = %v, want %s", payload.Labels, reviewBacklogLabel) } - if !strings.Contains(payload.Body, "PR: #5") || !strings.Contains(payload.Body, "Evidence: `pkg/old.go:") { + // Both fixture findings share the correctness perspective, so + // they collapse into one checklist item (hivecommons/hive#17065). + if !strings.Contains(payload.Body, "PR: #5") || !strings.Contains(payload.Body, "(`pkg/old.go:10`)") || !strings.Contains(payload.Body, "(`pkg/old.go:20`)") { t.Errorf("created issue body did not link PR/evidence: %q", payload.Body) } w.Header().Set("Content-Type", "application/json") @@ -508,7 +510,7 @@ func TestReviewRequestWatcherFilesOutOfScopeBacklogOnce(t *testing.T) { Body string `json:"body"` } _ = json.NewDecoder(r.Body).Decode(&payload) - if !strings.Contains(payload.Body, reviewBacklogSummaryMarker) || !strings.Contains(payload.Body, "#101") || !strings.Contains(payload.Body, "#102") { + if !strings.Contains(payload.Body, reviewBacklogSummaryMarker) || !strings.Contains(payload.Body, "#101") || !strings.Contains(payload.Body, "2 follow-ups") { t.Errorf("summary comment missing filed issues: %q", payload.Body) } w.Header().Set("Content-Type", "application/json") @@ -533,8 +535,8 @@ func TestReviewRequestWatcherFilesOutOfScopeBacklogOnce(t *testing.T) { if reviews != 2 { t.Fatalf("reviews = %d, want 2 re-reviews", reviews) } - if createdIssues != 2 { - t.Fatalf("created backlog issues = %d, want 2 with no duplicates on re-review", createdIssues) + if createdIssues != 1 { + t.Fatalf("created backlog issues = %d, want one collapsed item with no duplicates on re-review", createdIssues) } if prSummaryComments != 1 { t.Fatalf("PR summary comments = %d, want one", prSummaryComments) @@ -714,8 +716,8 @@ func TestReviewRequestWatcher_PerHeadBackstop(t *testing.T) { if !resp.OK || resp.State != ReviewEventRecordVerdict || posts != 1 { t.Fatalf("second review on the same head must record the verdict but post nothing: %+v posts=%d", resp, posts) } - if createdIssues != 2 || prSummaryComments != 1 { - t.Fatalf("suppressed verdict should file capped backlog once: issues=%d summary=%d", createdIssues, prSummaryComments) + if createdIssues != 1 || prSummaryComments != 1 { + t.Fatalf("suppressed verdict should file the backlog once, both findings collapsed into one item: issues=%d summary=%d", createdIssues, prSummaryComments) } if !strings.Contains(resp.Note, "already carries 1 hive review") || !strings.Contains(resp.Note, "--revise") { t.Fatalf("note must explain the cap and the way out: %q", resp.Note) diff --git a/src/pkg/github/write_surface_internal_test.go b/src/pkg/github/write_surface_internal_test.go index 6241e64384..561b18a91f 100644 --- a/src/pkg/github/write_surface_internal_test.go +++ b/src/pkg/github/write_surface_internal_test.go @@ -519,14 +519,16 @@ func TestReviewBacklog_FiledIssuesAndSummaryAudited(t *testing.T) { } c.ProcessReviewRequestsOnce(context.Background()) - rec := assertTypedWrite(t, *recs, AuditActionReviewBacklogIssueFiled, "o/r", 101, 2) - for _, want := range []string{"pr=5", "perspective=correctness", "reused=false", "review_agent=reviewer"} { + // The two fixture findings share a perspective and collapse into one + // item, so one filed entry carrying findings=2 (hivecommons/hive#17065). + rec := assertTypedWrite(t, *recs, AuditActionReviewBacklogIssueFiled, "o/r", 101, 1) + for _, want := range []string{"pr=5", "perspective=correctness", "findings=2", "reused=false", "review_agent=reviewer"} { if !strings.Contains(rec.Detail, want) { t.Errorf("filed-issue detail %q missing %q", rec.Detail, want) } } rec = assertTypedWrite(t, *recs, AuditActionReviewBacklogSummaryPosted, "o/r", 5, 1) - if !strings.Contains(rec.Detail, "issues=2") { - t.Errorf("summary detail %q missing issues=2", rec.Detail) + if !strings.Contains(rec.Detail, "issues=1") { + t.Errorf("summary detail %q missing issues=1", rec.Detail) } } From 0688d2ab7f0ae8d7f542f9ebbedf5e81e914becc Mon Sep 17 00:00:00 2001 From: Andrew Anderson <andy@clubanderson.com> Date: Sat, 7 Nov 2026 14:47:10 -0500 Subject: [PATCH 3/3] review backlog: expose min_severity and max_per_pr in Settings TestSettingsDialogReferencesEveryHiveYAMLLeaf requires every hive.yaml leaf to have a Settings control. Add a min_severity select and a lifetime max_per_pr input next to the retired per-day cap, accept both on PUT /api/config/review, and cover them in the 11088 settings test. Signed-off-by: Andrew Anderson <andy@clubanderson.com> --- src/pkg/dashboard/api_config_review.go | 13 +++++++++++++ .../review_severity_settings_11088_test.go | 14 ++++++++++++-- .../dashboard/static-src/js/config-helpers.js | 18 +++++++++++++++--- src/pkg/dashboard/static/index.html | 18 +++++++++++++++--- 4 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/pkg/dashboard/api_config_review.go b/src/pkg/dashboard/api_config_review.go index d5dfc625c8..fb129ebb6e 100644 --- a/src/pkg/dashboard/api_config_review.go +++ b/src/pkg/dashboard/api_config_review.go @@ -181,6 +181,8 @@ func (s *Server) handleReviewConfigPut(w http.ResponseWriter, r *http.Request) { JiraStatus *string `json:"jira_status"` Labels *[]string `json:"labels"` MaxPerPRPerDay *int `json:"max_per_pr_per_day"` + MaxPerPR *int `json:"max_per_pr"` + MinSeverity *string `json:"min_severity"` } `json:"backlog"` // ReviewerAutoscale is review.reviewers.autoscale // (hivecommons/hive#14685). Pointer fields keep "absent means @@ -359,6 +361,17 @@ func (s *Server) handleReviewConfigPut(w http.ResponseWriter, r *http.Request) { if bl.MaxPerPRPerDay != nil { backlog.MaxPerPRPerDay = *bl.MaxPerPRPerDay } + if bl.MaxPerPR != nil { + backlog.MaxPerPR = *bl.MaxPerPR + } + if bl.MinSeverity != nil { + // Empty keeps "use the default" rather than pinning P2 into the file. + if v := strings.TrimSpace(*bl.MinSeverity); v == "" { + backlog.MinSeverity = "" + } else { + backlog.MinSeverity = strings.ToUpper(v) + } + } } if err := severity.Validate(); err != nil { jsonError(w, err.Error(), http.StatusBadRequest) diff --git a/src/pkg/dashboard/review_severity_settings_11088_test.go b/src/pkg/dashboard/review_severity_settings_11088_test.go index f87a0ec0e5..259ebeda74 100644 --- a/src/pkg/dashboard/review_severity_settings_11088_test.go +++ b/src/pkg/dashboard/review_severity_settings_11088_test.go @@ -30,6 +30,8 @@ func TestReviewConfigPut_SeverityBacklog(t *testing.T) { {map[string]any{"backlog": map[string]any{"destination": "trello"}}, "review.backlog.destination"}, {map[string]any{"backlog": map[string]any{"destination": "linear"}}, "review.backlog.destination"}, {map[string]any{"backlog": map[string]any{"max_per_pr_per_day": -1}}, "review.backlog.max_per_pr_per_day"}, + {map[string]any{"backlog": map[string]any{"max_per_pr": -1}}, "review.backlog.max_per_pr"}, + {map[string]any{"backlog": map[string]any{"min_severity": "P9"}}, "review.backlog.min_severity"}, } for _, tc := range bad { rec := doPut(s, "/api/config/review", tc.body) @@ -56,13 +58,19 @@ func TestReviewConfigPut_SeverityBacklog(t *testing.T) { } s.deps.Config.Governor.WorkSource.Type = "linear" - if rec := doPut(s, "/api/config/review", map[string]any{"backlog": map[string]any{"destination": "linear", "linear_state": " Triage ", "labels": []string{"origin/reviewer", " ", "nit"}, "max_per_pr_per_day": 4}}); rec.Code != http.StatusOK { + if rec := doPut(s, "/api/config/review", map[string]any{"backlog": map[string]any{"destination": "linear", "linear_state": " Triage ", "labels": []string{"origin/reviewer", " ", "nit"}, "max_per_pr_per_day": 4, "max_per_pr": 5, "min_severity": " p1 "}}); rec.Code != http.StatusOK { t.Fatalf("PUT backlog: %d: %s", rec.Code, rec.Body.String()) } bl := s.deps.Config.Review.Backlog - if bl.Destination != "linear" || bl.LinearState != "Triage" || !reflect.DeepEqual(bl.Labels, []string{"origin/reviewer", "nit"}) || bl.MaxPerPRPerDay != 4 { + if bl.Destination != "linear" || bl.LinearState != "Triage" || !reflect.DeepEqual(bl.Labels, []string{"origin/reviewer", "nit"}) || bl.MaxPerPRPerDay != 4 || bl.MaxPerPR != 5 || bl.MinSeverity != "P1" { t.Fatalf("backlog not stored: %+v", bl) } + if rec := doPut(s, "/api/config/review", map[string]any{"backlog": map[string]any{"min_severity": ""}}); rec.Code != http.StatusOK { + t.Fatalf("PUT backlog clear min_severity: %d: %s", rec.Code, rec.Body.String()) + } + if s.deps.Config.Review.Backlog.MinSeverity != "" { + t.Fatalf("empty min_severity must clear the override, got %q", s.deps.Config.Review.Backlog.MinSeverity) + } if rec := doPut(s, "/api/config/review", map[string]any{"all_authors": true, "severity": map[string]any{}}); rec.Code != http.StatusOK { t.Fatalf("PUT without severity keys: %d", rec.Code) @@ -112,6 +120,8 @@ func TestReviewSeverityBacklogSettingsStaticWiring(t *testing.T) { "linear_state", "jira_status", "max_per_pr_per_day", + "max_per_pr", + "min_severity", } { if !strings.Contains(render, want) { t.Errorf("renderReviewSeverityBacklog missing %q", want) diff --git a/src/pkg/dashboard/static-src/js/config-helpers.js b/src/pkg/dashboard/static-src/js/config-helpers.js index eff36d0293..76d92dbcff 100644 --- a/src/pkg/dashboard/static-src/js/config-helpers.js +++ b/src/pkg/dashboard/static-src/js/config-helpers.js @@ -208,6 +208,7 @@ const dests = reviewBacklogDestinationsFor(ws); const dest = dests.some(o => o.value === bl.destination) ? bl.destination : 'github_issue'; const labels = Array.isArray(bl.labels) ? bl.labels : ['origin/reviewer']; + const minSeverity = ['P0', 'P1', 'P2', 'P3'].includes(String(bl.min_severity || '').toUpperCase()) ? String(bl.min_severity).toUpperCase() : ''; const presetBtns = REVIEW_SEVERITY_PRESETS.map(p => `<button type="button" class="hv-btn ${blockAt === p.blockAt ? 'btn-primary' : 'btn-secondary'} btn-sm btn-small" data-action="applyReviewSeverityPreset" data-arg0="${p.blockAt}" aria-pressed="${blockAt === p.blockAt ? 'true' : 'false'}"${dis}>${esc(p.label)}</button>`).join(' '); const toggle = (key, on, label, tip) => `<div class="config-toggle mt-3"> <div class="config-toggle-switch ${on ? 'on' : ''}${canEdit ? '' : ' disabled'}" data-action="toggleReviewSeverityFlag" data-arg0="${key}" data-arg-types="s" role="switch" aria-checked="${on ? 'true' : 'false'}"></div> @@ -247,8 +248,18 @@ <input type="text" id="review-backlog-labels" value="${esc(labels.join(', '))}" placeholder="origin/reviewer" data-change-action="markDirtyReviewBacklogLabels" data-arg-types="v"${dis}> </div> <div class="config-field mt-3"> - <label>Max backlog items per PR per day ${settingHelpMark('review-swarm', 'blocking-line-reviewseverity-reviewbacklog', 'Max backlog items per PR per day', `review.backlog.max_per_pr_per_day: cap on items filed from one PR in a day. 0 or blank uses the default of 10.`)}</label> - <input type="number" id="review-backlog-max" min="0" value="${esc(String(bl.max_per_pr_per_day || ''))}" placeholder="10" data-change-action="markDirtyReviewBacklog" data-arg0="max_per_pr_per_day" data-arg-types="s,N"${dis}> + <label>Lowest severity filed as a backlog item ${settingHelpMark('review-swarm', 'blocking-line-reviewseverity-reviewbacklog', 'Lowest severity filed as a backlog item', `review.backlog.min_severity: findings at or above this priority become backlog items; anything below is listed in the PR's Follow-ups comment instead. Default P2 (P3 files everything).`)}</label> + <select id="review-backlog-min-severity" data-change-action="markDirtyReviewBacklog" data-arg0="min_severity" data-arg-types="s,v"${dis}> + ${[['', 'Default (P0–P2)'], ['P0', 'P0 only'], ['P1', 'P0–P1'], ['P2', 'P0–P2'], ['P3', 'P0–P3 (everything)']].map(([v, l]) => `<option value="${v}"${minSeverity === v ? ' selected' : ''}>${l}</option>`).join('')} + </select> + </div> + <div class="config-field mt-3"> + <label>Max backlog items per PR ${settingHelpMark('review-swarm', 'blocking-line-reviewseverity-reviewbacklog', 'Max backlog items per PR', `review.backlog.max_per_pr: cap on items filed or re-raised from one PR over its lifetime. 0 or blank uses the default of 3 (or the retired review.backlog.max_per_pr_per_day if still set).`)}</label> + <input type="number" id="review-backlog-max" min="0" value="${esc(String(bl.max_per_pr || ''))}" placeholder="3" data-change-action="markDirtyReviewBacklog" data-arg0="max_per_pr" data-arg-types="s,N"${dis}> + </div> + <div class="config-field mt-3"> + <label>Max backlog items per PR per day (retired) ${settingHelpMark('review-swarm', 'blocking-line-reviewseverity-reviewbacklog', 'Max backlog items per PR per day (retired)', `review.backlog.max_per_pr_per_day: the retired per-day cap. Still read as the lifetime cap when max_per_pr is unset. 0 or blank means not set.`)}</label> + <input type="number" id="review-backlog-max-per-day" min="0" value="${esc(String(bl.max_per_pr_per_day || ''))}" placeholder="not set" data-change-action="markDirtyReviewBacklog" data-arg0="max_per_pr_per_day" data-arg-types="s,N"${dis}> </div> </div>`; } @@ -274,7 +285,8 @@ } function markDirtyReviewBacklog(key, value) { const rv = reviewSeverityState(); - if (key === 'max_per_pr_per_day') value = Number.isFinite(value) && value > 0 ? Math.floor(value) : 0; + if (key === 'max_per_pr_per_day' || key === 'max_per_pr') value = Number.isFinite(value) && value > 0 ? Math.floor(value) : 0; + if (key === 'min_severity') value = String(value || '').trim().toUpperCase(); rv.backlog[key] = value; markDirty('review', 'backlog', Object.assign({}, (_configState.dirty.review || {}).backlog, { [key]: value })); } diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index f3e9a1cc4e..74f56e5f88 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -64901,6 +64901,7 @@ <h4>${model.editIndex === null ? 'Add Event trigger' : 'Edit Event trigger'}</h4 const dests = reviewBacklogDestinationsFor(ws); const dest = dests.some(o => o.value === bl.destination) ? bl.destination : 'github_issue'; const labels = Array.isArray(bl.labels) ? bl.labels : ['origin/reviewer']; + const minSeverity = ['P0', 'P1', 'P2', 'P3'].includes(String(bl.min_severity || '').toUpperCase()) ? String(bl.min_severity).toUpperCase() : ''; const presetBtns = REVIEW_SEVERITY_PRESETS.map(p => `<button type="button" class="hv-btn ${blockAt === p.blockAt ? 'btn-primary' : 'btn-secondary'} btn-sm btn-small" data-action="applyReviewSeverityPreset" data-arg0="${p.blockAt}" aria-pressed="${blockAt === p.blockAt ? 'true' : 'false'}"${dis}>${esc(p.label)}</button>`).join(' '); const toggle = (key, on, label, tip) => `<div class="config-toggle mt-3"> <div class="config-toggle-switch ${on ? 'on' : ''}${canEdit ? '' : ' disabled'}" data-action="toggleReviewSeverityFlag" data-arg0="${key}" data-arg-types="s" role="switch" aria-checked="${on ? 'true' : 'false'}"></div> @@ -64940,8 +64941,18 @@ <h4>${model.editIndex === null ? 'Add Event trigger' : 'Edit Event trigger'}</h4 <input type="text" id="review-backlog-labels" value="${esc(labels.join(', '))}" placeholder="origin/reviewer" data-change-action="markDirtyReviewBacklogLabels" data-arg-types="v"${dis}> </div> <div class="config-field mt-3"> - <label>Max backlog items per PR per day ${settingHelpMark('review-swarm', 'blocking-line-reviewseverity-reviewbacklog', 'Max backlog items per PR per day', `review.backlog.max_per_pr_per_day: cap on items filed from one PR in a day. 0 or blank uses the default of 10.`)}</label> - <input type="number" id="review-backlog-max" min="0" value="${esc(String(bl.max_per_pr_per_day || ''))}" placeholder="10" data-change-action="markDirtyReviewBacklog" data-arg0="max_per_pr_per_day" data-arg-types="s,N"${dis}> + <label>Lowest severity filed as a backlog item ${settingHelpMark('review-swarm', 'blocking-line-reviewseverity-reviewbacklog', 'Lowest severity filed as a backlog item', `review.backlog.min_severity: findings at or above this priority become backlog items; anything below is listed in the PR's Follow-ups comment instead. Default P2 (P3 files everything).`)}</label> + <select id="review-backlog-min-severity" data-change-action="markDirtyReviewBacklog" data-arg0="min_severity" data-arg-types="s,v"${dis}> + ${[['', 'Default (P0–P2)'], ['P0', 'P0 only'], ['P1', 'P0–P1'], ['P2', 'P0–P2'], ['P3', 'P0–P3 (everything)']].map(([v, l]) => `<option value="${v}"${minSeverity === v ? ' selected' : ''}>${l}</option>`).join('')} + </select> + </div> + <div class="config-field mt-3"> + <label>Max backlog items per PR ${settingHelpMark('review-swarm', 'blocking-line-reviewseverity-reviewbacklog', 'Max backlog items per PR', `review.backlog.max_per_pr: cap on items filed or re-raised from one PR over its lifetime. 0 or blank uses the default of 3 (or the retired review.backlog.max_per_pr_per_day if still set).`)}</label> + <input type="number" id="review-backlog-max" min="0" value="${esc(String(bl.max_per_pr || ''))}" placeholder="3" data-change-action="markDirtyReviewBacklog" data-arg0="max_per_pr" data-arg-types="s,N"${dis}> + </div> + <div class="config-field mt-3"> + <label>Max backlog items per PR per day (retired) ${settingHelpMark('review-swarm', 'blocking-line-reviewseverity-reviewbacklog', 'Max backlog items per PR per day (retired)', `review.backlog.max_per_pr_per_day: the retired per-day cap. Still read as the lifetime cap when max_per_pr is unset. 0 or blank means not set.`)}</label> + <input type="number" id="review-backlog-max-per-day" min="0" value="${esc(String(bl.max_per_pr_per_day || ''))}" placeholder="not set" data-change-action="markDirtyReviewBacklog" data-arg0="max_per_pr_per_day" data-arg-types="s,N"${dis}> </div> </div>`; } @@ -64967,7 +64978,8 @@ <h4>${model.editIndex === null ? 'Add Event trigger' : 'Edit Event trigger'}</h4 } function markDirtyReviewBacklog(key, value) { const rv = reviewSeverityState(); - if (key === 'max_per_pr_per_day') value = Number.isFinite(value) && value > 0 ? Math.floor(value) : 0; + if (key === 'max_per_pr_per_day' || key === 'max_per_pr') value = Number.isFinite(value) && value > 0 ? Math.floor(value) : 0; + if (key === 'min_severity') value = String(value || '').trim().toUpperCase(); rv.backlog[key] = value; markDirty('review', 'backlog', Object.assign({}, (_configState.dirty.review || {}).backlog, { [key]: value })); }