diff --git a/changelog.d/added-review-require-labels.md b/changelog.d/added-review-require-labels.md new file mode 100644 index 0000000000..5c0fcdb82a --- /dev/null +++ b/changelog.d/added-review-require-labels.md @@ -0,0 +1 @@ +- `review.require_labels` — a PR-side approval allow-list for reviews, the counterpart of `project.issue_filter.require_labels`: when set, the hive reviews only PRs carrying at least one of the labels. Enforced server-side in the cadence reviewer's PR list, the fan-out dispatch planner and the `hive-review` relay (which re-reads the PR's labels and refuses any review, thread reply or verdict on an unlabelled PR, audited as `review_label_gate_refused`), so a prompt cannot bypass it. New **Settings → Features → Review Gate → "Only review labelled PRs"** toggle with a label list, saved through `PUT /api/config/review`. Empty (the default) changes nothing. Reported-by: @joerunde (a1-vllm-spyre spoke: "the team wants reviews gated — run only when a label is added to a PR"). diff --git a/src/cmd/hive/main.go b/src/cmd/hive/main.go index 7f36745a29..45ae35bd30 100644 --- a/src/cmd/hive/main.go +++ b/src/cmd/hive/main.go @@ -10453,6 +10453,9 @@ func installReviewRelaySettings(client *github.Client, cfg *config.Config, logge func() bool { return cfg.Review.CombinedPerspectives }, func() int { return cfg.Review.MaxReviewsPerHead }, ) + // review.require_labels (PR approval allow-list): the relay's half of the + // gate, read live so a Features-tab edit applies to the next request. + client.SetReviewRequireLabels(func() []string { return cfg.Review.RequireLabels }) } // reviewBacklogRouting resolves review.severity / review.backlog and the @@ -10801,6 +10804,7 @@ func planReviewDispatchWith(ctx context.Context, cfg *config.Config, actionable prAgents := auditPRAgents(cfg.Project.Org, time.Now().Add(-auditPRAttributionWindow), "") candidates := make([]github.PullRequest, 0, len(actionable.PRs.Items)+len(actionable.PRs.Held)) seenPRs := map[string]bool{} + labelGated := 0 addCandidates := func(items []github.PullRequest) { for _, pr := range items { fullRepo := fullRepoName(pr.Repo, cfg.Project.Org) @@ -10809,11 +10813,22 @@ func planReviewDispatchWith(ctx context.Context, cfg *config.Config, actionable continue } seenPRs[key] = true + // review.require_labels: an unlabelled PR is never dispatched + // to the swarm. The relay refuses it independently. + if !cfg.Review.AdmitsPRLabels(pr.Labels) { + labelGated++ + continue + } candidates = append(candidates, pr) } } addCandidates(actionable.PRs.Items) addCandidates(actionable.PRs.Held) + if labelGated > 0 && logger != nil { + logger.Info("review label gate withheld PRs from review dispatch", + "withheld", labelGated, "candidates", len(candidates), + "require_labels", strings.Join(cfg.Review.NormalizedRequireLabels(), ",")) + } sort.SliceStable(candidates, func(i, j int) bool { ri, rj := candidates[i].ReviewRank, candidates[j].ReviewRank if ri > 0 && rj > 0 && ri != rj { diff --git a/src/docs/agent-configuration.md b/src/docs/agent-configuration.md index 268888bca1..636bce3b99 100644 --- a/src/docs/agent-configuration.md +++ b/src/docs/agent-configuration.md @@ -1031,6 +1031,7 @@ Semantics: - An issue must carry **at least one** required label to be eligible. Matching is case-insensitive and **exact** (a prefix like `approved-for-agents-maybe` does not satisfy `approved-for-agents` — prefix matching would over-admit through an approval gate). - **Exempt wins on conflict**: an issue carrying both an exempt label and a required label stays excluded. There is deliberately no separate `exclude_labels` field — the exempt list *is* the exclusion mechanism, applied first. - PRs and the Hold list are unaffected: open PRs are in-flight work, and held issues still appear under On Hold. +- PRs have their own, separate allow-list for **reviews**: `review.require_labels` (Settings → Features → Review Gate → "Only review labelled PRs") keeps the hive from reviewing a PR until it carries an approval label; see [review-swarm.md](review-swarm.md#only-review-labelled-prs-reviewrequire_labels). Use a distinct label from the issue gate if the two approvals mean different things to your maintainers. Both polarities are enforced at **enumeration** — the point where GitHub issues become the hive's actionable set — not in the prompt. A filtered issue never enters the queue, never appears in a kick, never triggers plan-from-label, and cannot be re-selected by a confused (or prompt-injected) agent re-listing the repo. Kick prompts additionally state the active require policy so agents know the list is intentionally short. Both lists are edited on the Labels tab; an active require gate is also noted read-only under **Projects**, and hub-managed hives can receive `issue_filter` with their project config over the heartbeat. diff --git a/src/docs/dashboard-sections.md b/src/docs/dashboard-sections.md index 0cdea35f66..ff7e6f2baf 100644 --- a/src/docs/dashboard-sections.md +++ b/src/docs/dashboard-sections.md @@ -431,6 +431,8 @@ Review Pipeline shows pull requests by stage first; Review Queue lists the same Each open PR in the stage the review pipeline derived for it (GET /api/review/pipeline), with reviewers, P0–P3 findings, the fix-loop counter and the next action. +When **Settings → Features → Review Gate → Only review labelled PRs** (`review.require_labels`) is on, PRs without one of the approval labels stay in the *unreviewed* stage until a maintainer labels them: the planner never dispatches them and the relay refuses reviews on them. + **What it tells you.** Each column is one stage of the review loop: unreviewed, reviewing, changes requested, fixing, waiting for a human, approved, merged and abandoned. Each card shows the pull request, who has reviewed it, how many findings were raised at each severity and what should happen next. **How the numbers are worked out.** Hive reads the last review results it recorded for each pull request and places the card in a stage from those results alone. The collapsible Pipeline tile header shows how many pull requests are on the board. Nothing is fetched from GitHub when you open it. diff --git a/src/docs/dashboard.md b/src/docs/dashboard.md index a1304230be..efd1c1c46d 100644 --- a/src/docs/dashboard.md +++ b/src/docs/dashboard.md @@ -99,6 +99,14 @@ through `/api/config/auto-merge`, with a watched-repo multi-select (no selection means all watched repositories), role selector, GitHub permission switch, and excluded-label list. +Governor settings **Features → Review Gate** includes the owner-only +**Only review labelled PRs** toggle with a comma-separated label list. It +edits `review.require_labels` through `/api/config/review`; on, the hive +reviews only PRs carrying one of the labels (enforced in the reviewer kick +list, the review dispatch planner and the hive-review relay — see +[review-swarm.md](review-swarm.md#only-review-labelled-prs-reviewrequire_labels)). +Off (the default) sends an empty list and every open PR stays reviewable. + The cadence table starts each agent row with the same `1`/`0` Agent power rocker used in the agent settings panel, so owners can enable or disable an agent from the table; disabled rows are dimmed, and read-only viewers see the diff --git a/src/docs/review-swarm.md b/src/docs/review-swarm.md index dfa0221475..fec72d5ddb 100644 --- a/src/docs/review-swarm.md +++ b/src/docs/review-swarm.md @@ -181,6 +181,7 @@ review: contributor_prs: base_sync: false # optional; let GitHub update-branch unstick hive-authored fork PRs (default off) priority_labels: false # optional; mirror the PR review queue rank onto review-priority/* labels (default off) + require_labels: [] # optional; review ONLY PRs carrying one of these labels (default empty = every open PR); see "Only review labelled PRs" pending_ttl: 45m # optional; re-dispatch a review that has posted no verdict after this long (default 45m) pending_fix_ttl: 2h # optional; dispatch the next bounded fix attempt when a fixer has pushed no new head after this long (default 2h; negative disables expiry) max_redispatch: 3 # optional; expiries per head before the PR is held for a human (default 3) @@ -202,6 +203,46 @@ review: ### Blocking line (`review.severity`, `review.backlog`) +### Only review labelled PRs (`review.require_labels`) + +`review.require_labels` is the PR-side counterpart of the issue approval gate +`project.issue_filter.require_labels` ([agent-configuration.md](agent-configuration.md#label-policy-which-issues-agents-may-work)): +a team that wants hive reviews **only on PRs a maintainer has opted in** names +the label(s) here, and the hive reviews nothing else. + +```yaml +review: + require_labels: [hive-review] # a PR is reviewed only if it carries at least one of these +``` + +Dashboard: **Settings → Features → Review Gate → "Only review labelled PRs"** +(toggle + comma-separated label list; saved through `PUT /api/config/review` +into the dashboard overlay, so it survives restarts and ACMM pack re-applies — +it is a `review.*` key, not an agent block). + +Semantics: + +- **Absent/empty = no gate.** Every open PR stays reviewable; no existing hive + goes quiet on upgrade. +- A PR must carry **at least one** listed label. Matching is case-insensitive + and **exact** (`approved-for-review` does not satisfy `approved`). +- **Enforced server-side in three places**, so a prompt cannot route around it: + the cadence reviewer's `${PR_LIST}` and the fan-out dispatch planner never + offer an unlabelled PR (log line `review label gate withheld PRs from …`), + and the `hive-review` relay refuses any request — review, thread reply, + resolution or bare `record_verdict` — on a PR that lacks the label after + re-reading its labels with the App token (`.denied`, audit + `review_label_gate_refused`). A failed label fetch is retried, never waved + through. +- The reviewer's kick carries a `REVIEW LABEL GATE` notice naming the labels, + mirroring the issue-filter notice. +- Labels are **never created** by the hive; name one the repo already + maintains. Exempt/hold labels still exclude a PR that also carries an + approval label. +- It is independent of `review.require_approval`: a hive that gates merge on + review AND gates review on a label simply does not merge unlabelled PRs + until a person labels (or merges) them. + `review.severity.block_at` is the lowest finding priority that still blocks merge, on the P0–P3 scale (critical → P0, high → P1, medium → P2, low/info → P3). **Settings → Features → Review Gate → Severity & backlog** offers three presets: *Strict (P0–P2 block)* sets `P2`, *Ship fast (P0–P1 block)* sets `P1`, and *Everything blocks* sets `P3`. Unset keeps today's behaviour. Findings below the line are posted as non-blocking comments when `comment_below` is true (default) and filed as follow-up work when `backlog_below` is true (default). `review.backlog` says where that follow-up work goes: diff --git a/src/pkg/config/config.go b/src/pkg/config/config.go index 660e44c5c4..d123b7eabb 100644 --- a/src/pkg/config/config.go +++ b/src/pkg/config/config.go @@ -7565,6 +7565,18 @@ type ReviewConfig struct { // a repo that does not already have them is skipped. Off by default: a // label on every PR is churn a repo must ask for. PriorityLabels bool `yaml:"priority_labels,omitempty" json:"priority_labels,omitempty"` + // RequireLabels is the PR-side approval allow-list, the review + // counterpart of project.issue_filter.require_labels. Non-empty: a PR is + // reviewed by the hive only if it carries AT LEAST ONE of these labels + // (case-insensitive exact match). It is enforced in two places so a + // prompt cannot route around it: the reviewer kick lists and the review + // dispatch planner never offer an unlabelled PR, and the hive-review + // relay refuses to post on one. Absent/empty keeps today's behaviour — + // every open PR is reviewable — so no existing hive goes quiet on + // upgrade. The label is never created: name one the governed repos + // already maintain. Operator-added hold/exempt labels still exclude a PR + // that also carries an approval label. + RequireLabels []string `yaml:"require_labels,omitempty" json:"require_labels,omitempty"` // VerdictLabels mirrors the review swarm's state for each PR's current // head onto exactly one label (hivecommons/hive#13833): reviewing while // the head's review is pending, then the settled verdict. Independent of diff --git a/src/pkg/config/review_require_labels.go b/src/pkg/config/review_require_labels.go new file mode 100644 index 0000000000..dbac8cb463 --- /dev/null +++ b/src/pkg/config/review_require_labels.go @@ -0,0 +1,52 @@ +package config + +import "strings" + +// ReviewRequireLabelsSkipReason is the one phrase every surface uses when +// review.require_labels keeps a PR out of review, so a kick list, the relay's +// refusal and the governor log all read the same. +const ReviewRequireLabelsSkipReason = "review.require_labels: PR carries none of the approval labels" + +// NormalizedRequireLabels returns review.require_labels trimmed and without +// blanks or duplicates, in the operator's order. An all-blank list is nil, +// which every caller reads as "gate off". +func (r ReviewConfig) NormalizedRequireLabels() []string { + if len(r.RequireLabels) == 0 { + return nil + } + out := make([]string, 0, len(r.RequireLabels)) + seen := map[string]bool{} + for _, l := range r.RequireLabels { + l = strings.TrimSpace(l) + if l == "" { + continue + } + key := strings.ToLower(l) + if seen[key] { + continue + } + seen[key] = true + out = append(out, l) + } + if len(out) == 0 { + return nil + } + return out +} + +// RequireLabelsEnabled reports whether the PR review label gate is on. +func (r ReviewConfig) RequireLabelsEnabled() bool { + return len(r.NormalizedRequireLabels()) > 0 +} + +// AdmitsPRLabels is the review-side counterpart of IssueFilterConfig.Admits: +// with review.require_labels empty every PR is admitted; otherwise the PR must +// carry at least one of the configured labels (case-insensitive exact match, +// never prefix — "approved-for-review" must not admit under "approved"). +func (r ReviewConfig) AdmitsPRLabels(labels []string) bool { + required := r.NormalizedRequireLabels() + if len(required) == 0 { + return true + } + return anyLabelMatches(required, labels) +} diff --git a/src/pkg/config/review_require_labels_test.go b/src/pkg/config/review_require_labels_test.go new file mode 100644 index 0000000000..fabd7a027e --- /dev/null +++ b/src/pkg/config/review_require_labels_test.go @@ -0,0 +1,46 @@ +package config + +import "testing" + +func TestReviewConfigAdmitsPRLabels(t *testing.T) { + t.Run("empty gate admits every PR", func(t *testing.T) { + var r ReviewConfig + if !r.AdmitsPRLabels(nil) || !r.AdmitsPRLabels([]string{"anything"}) { + t.Fatal("empty review.require_labels must admit every PR") + } + if r.RequireLabelsEnabled() { + t.Fatal("empty gate must report disabled") + } + }) + t.Run("blank-only list is the empty gate", func(t *testing.T) { + r := ReviewConfig{RequireLabels: []string{" ", ""}} + if r.RequireLabelsEnabled() || !r.AdmitsPRLabels(nil) { + t.Fatal("blank entries must not turn the gate on") + } + }) + t.Run("requires at least one label, case-insensitive exact", func(t *testing.T) { + r := ReviewConfig{RequireLabels: []string{"hive-review", " approved-for-agents "}} + if !r.RequireLabelsEnabled() { + t.Fatal("expected gate on") + } + if r.AdmitsPRLabels(nil) { + t.Fatal("unlabelled PR must be refused") + } + if r.AdmitsPRLabels([]string{"bug", "approved"}) { + t.Fatal("prefix of an approval label must not admit") + } + if !r.AdmitsPRLabels([]string{"bug", "Hive-Review"}) { + t.Fatal("case-insensitive match must admit") + } + if !r.AdmitsPRLabels([]string{"approved-for-agents"}) { + t.Fatal("trimmed configured label must admit") + } + }) + t.Run("normalized drops blanks and duplicates", func(t *testing.T) { + r := ReviewConfig{RequireLabels: []string{"a", " a ", "", "B", "b"}} + got := r.NormalizedRequireLabels() + if len(got) != 2 || got[0] != "a" || got[1] != "B" { + t.Fatalf("unexpected normalized labels: %v", got) + } + }) +} diff --git a/src/pkg/dashboard/api_config_review.go b/src/pkg/dashboard/api_config_review.go index 6964ee228d..84f4c32de8 100644 --- a/src/pkg/dashboard/api_config_review.go +++ b/src/pkg/dashboard/api_config_review.go @@ -117,6 +117,9 @@ func (s *Server) handleReviewConfigPut(w http.ResponseWriter, r *http.Request) { SelfDecisionPolicy *string `json:"self_authored_decision_policy"` ConfidenceScore *bool `json:"confidence_score"` PriorityLabels *bool `json:"priority_labels"` + // RequireLabels replaces review.require_labels whole; an empty list + // turns the PR review label gate off. + RequireLabels *[]string `json:"require_labels"` // VerdictLabels replaces review.verdict_labels whole: its four keys // only mean something together (one label per state), and an empty // object turns the feature off (hivecommons/hive#13833). @@ -519,6 +522,9 @@ func (s *Server) handleReviewConfigPut(w http.ResponseWriter, r *http.Request) { if body.PriorityLabels != nil { cfg.Review.PriorityLabels = *body.PriorityLabels } + if body.RequireLabels != nil { + cfg.Review.RequireLabels = config.ReviewConfig{RequireLabels: *body.RequireLabels}.NormalizedRequireLabels() + } if body.MaxReviewsPerHead != nil { cfg.Review.MaxReviewsPerHead = *body.MaxReviewsPerHead } diff --git a/src/pkg/dashboard/api_config_review_require_labels_test.go b/src/pkg/dashboard/api_config_review_require_labels_test.go new file mode 100644 index 0000000000..396bab830c --- /dev/null +++ b/src/pkg/dashboard/api_config_review_require_labels_test.go @@ -0,0 +1,40 @@ +package dashboard + +import ( + "net/http" + "testing" +) + +// PUT /api/config/review replaces review.require_labels whole, normalizes it, +// and an empty list turns the gate off; an absent key leaves it alone. +func TestReviewConfigPut_RequireLabels(t *testing.T) { + s := covApiServer(t) + + rec := doPut(s, "/api/config/review", map[string]any{"require_labels": []string{" hive-review ", "", "hive-review", "approved-for-agents"}}) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d: %s", rec.Code, rec.Body.String()) + } + got := s.deps.Config.Review.RequireLabels + if len(got) != 2 || got[0] != "hive-review" || got[1] != "approved-for-agents" { + t.Fatalf("require_labels not normalized: %v", got) + } + + if rec := doPut(s, "/api/config/review", map[string]any{"priority_labels": true}); rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d", rec.Code) + } + if len(s.deps.Config.Review.RequireLabels) != 2 { + t.Fatalf("absent key must leave require_labels untouched: %v", s.deps.Config.Review.RequireLabels) + } + + if rec := doPut(s, "/api/config/review", map[string]any{"require_labels": []string{}}); rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d", rec.Code) + } + if s.deps.Config.Review.RequireLabelsEnabled() { + t.Fatalf("empty list must turn the gate off: %v", s.deps.Config.Review.RequireLabels) + } + + get := doGetNoRole(s, "/api/config/review") + if get.Code != http.StatusOK { + t.Fatalf("GET expected 200, got %d", get.Code) + } +} diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index a99590e4c7..2794d85c5e 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -3705,6 +3705,7 @@ .rv-reviewer-row .rv-reviewer-state, .rv-reviewer-list .rv-reviewer-empty { font-size: var(--fs-xs); color: var(--muted); } .rv-reviewer-help { margin: var(--sp-1) var(--sp-0) var(--sp-0); } .rv-reviewer-help-above { margin: var(--sp-0) var(--sp-0) var(--sp-2); } + #rv-require-labels-field[hidden] { display: none; } .am-trusted-authors-repos { min-height: 72px; } .am-bot-empty { color: var(--muted); margin: var(--sp-1) 0; } .am-bot-add { display: flex; gap: var(--sp-3); margin-top: var(--sp-4); align-items: center; } @@ -36927,6 +36928,7 @@

GitHub Projects sync

case 'nousCampaignAction': nousCampaignAction(el.dataset.arg0, el.dataset.arg1); break; case 'toggleConfigSwitch': toggleConfigSwitch(el, el.dataset.section, el.dataset.key); break; case 'toggleContributorPRBaseSync': toggleContributorPRBaseSync(el); break; + case 'toggleReviewRequireLabels': toggleReviewRequireLabels(el); break; case 'toggleSentinelEnabled': toggleSentinelEnabled(el); break; case 'toggleSentinelBehavior': toggleSentinelBehavior(el); break; case 'toggleSentinelTrustedAuthorsBlock': toggleSentinelTrustedAuthorsBlock(el); break; @@ -50758,6 +50760,14 @@

${esc(s.title)}

Label PRs with their review-queue priority ${yamlHint('review.priority_labels')} ${settingHelpMark('review-swarm', 'configuration', 'Label PRs with their review-queue priority', `OFF by default. Mirrors each open PR's place in the review queue (GET /api/review/queue) onto exactly one of review-priority/high, review-priority/normal or review-priority/low - high for fixes, normal for refactors, docs and unclassified PRs, low for test-only PRs - so the order shows up in plain GitHub searches. Applied by the hive from the computed rank, never by the PR's author, on contributor PRs too. The labels are never created: create them in each repo first; a repo without them is skipped.`)} +
+
+ Only review labelled PRs ${yamlHint('review.require_labels[]')} ${settingHelpMark('review-swarm', 'configuration', 'Only review labelled PRs', `OFF by default: every open PR is reviewable. ON: the hive reviews a PR only when it carries at least one of the labels below (case-insensitive exact match), the PR-side counterpart of the issue approval gate (project.issue_filter.require_labels). Enforced server-side: unlabelled PRs never appear in a reviewer kick or the review dispatch, and the hive-review relay refuses to post on one, so a prompt cannot bypass it. The labels are never created: use a label the repo already maintains. Exempt and hold labels still exclude a PR that also carries an approval label.`)} +
+
+ + +
@@ -52718,6 +52728,32 @@

${esc(s.title)}

if (section === 'review' && (key === 'fan_out' || key === 'combined_perspectives')) refreshReviewerAgentSummary(); } + // Review label gate (Settings → Features → Review Gate → "Only review + // labelled PRs"): the toggle shows/hides the label input; the saved value + // is review.require_labels, where an empty list means off. + function parseReviewRequireLabels(text) { + const seen = new Set(); + return String(text || '').split(',').map(l => l.trim()).filter(l => { + if (!l) return false; + const k = l.toLowerCase(); + if (seen.has(k)) return false; + seen.add(k); + return true; + }); + } + function toggleReviewRequireLabels(el) { + if (!el || el.classList.contains('disabled') || el.dataset.disabled === 'true') return; + const isOn = el.classList.toggle('on'); + const field = document.getElementById('rv-require-labels-field'); + if (field) field.hidden = !isOn; + const input = document.getElementById('rv-require-labels'); + markDirty('review', 'require_labels', isOn ? parseReviewRequireLabels(input && input.value) : []); + if (isOn && input) input.focus(); + } + function markDirtyReviewRequireLabels(text) { + markDirty('review', 'require_labels', parseReviewRequireLabels(text)); + } + // Reviewer agents pick-list (Settings → Features → Review Gate → // Reviewers, #13460). The checked rows become review.reviewer_agents; an // empty selection means "keyword scan", mirroring the server. diff --git a/src/pkg/github/attribution.go b/src/pkg/github/attribution.go index 718112e9d0..a8b8d95ec0 100644 --- a/src/pkg/github/attribution.go +++ b/src/pkg/github/attribution.go @@ -111,6 +111,9 @@ const ( // AuditActionSentinelBlockedApproval records an approval/LGTM path refused // because the configured sentinel alert label is present. AuditActionSentinelBlockedApproval = "sentinel-blocked-approval" + // AuditActionReviewLabelGateRefused records a hive-review relay request + // refused because the PR carries none of review.require_labels. + AuditActionReviewLabelGateRefused = "review_label_gate_refused" // AuditActionReviewModelFallback is recorded when an adversarial reviewer // pool has no independent candidate and the configured fallback is used. AuditActionReviewModelFallback = "review_model_fallback" diff --git a/src/pkg/github/client.go b/src/pkg/github/client.go index ae667f8dc3..22e9e2e02c 100644 --- a/src/pkg/github/client.go +++ b/src/pkg/github/client.go @@ -251,6 +251,10 @@ type Client struct { // a negative value disables the backstop. combinedPerspectivesFn func() bool maxReviewsPerHeadFn func() int + // reviewRequireLabelsFn returns review.require_labels, read live so a + // Features-tab edit gates the next relay request without a rebuild. nil + // or empty means the gate is off. + reviewRequireLabelsFn func() []string // prAuthz gates PR-open requests from the request-file watcher against the // per-agent ACMM write-policy + forge-resistance. nil fails closed. Set by // StartPRRequestWatcher. diff --git a/src/pkg/github/review_label_gate.go b/src/pkg/github/review_label_gate.go new file mode 100644 index 0000000000..7c440bb4f2 --- /dev/null +++ b/src/pkg/github/review_label_gate.go @@ -0,0 +1,47 @@ +package github + +import ( + "context" + + "github.com/hivecommons/hive/pkg/config" +) + +// SetReviewRequireLabels wires review.require_labels into the hive-review +// relay. Read through a func so a live config edit applies to the next +// request without rebuilding the client. +func (c *Client) SetReviewRequireLabels(fn func() []string) { + if c == nil { + return + } + c.reviewRequireLabelsFn = fn +} + +// reviewRequireLabels returns the normalized gate list, nil when off. +func (c *Client) reviewRequireLabels() []string { + if c == nil || c.reviewRequireLabelsFn == nil { + return nil + } + return config.ReviewConfig{RequireLabels: c.reviewRequireLabelsFn()}.NormalizedRequireLabels() +} + +// reviewLabelGateAdmits reports whether the relay may act on repo#number +// under review.require_labels. With the gate off it admits without a fetch; +// otherwise the PR's labels are read fresh with the App token (the relay +// trusts nothing the agent wrote in the request) and matched with +// config.ReviewConfig.AdmitsPRLabels. A fetch error is returned so the caller +// can retry rather than either post or quarantine on a transient failure. +func (c *Client) reviewLabelGateAdmits(ctx context.Context, repo string, number int) (bool, error) { + required := c.reviewRequireLabels() + if len(required) == 0 { + return true, nil + } + if c.client == nil { + return false, ErrNoGitHubClient + } + owner, repoName := c.splitRepo(repo) + pr, _, err := c.client.PullRequests.Get(WithRESTCaller(ctx, "hive:review_label_gate"), owner, repoName, number) + if err != nil { + return false, err + } + return config.ReviewConfig{RequireLabels: required}.AdmitsPRLabels(labelNames(pr.Labels)), nil +} diff --git a/src/pkg/github/review_label_gate_test.go b/src/pkg/github/review_label_gate_test.go new file mode 100644 index 0000000000..1e282fc570 --- /dev/null +++ b/src/pkg/github/review_label_gate_test.go @@ -0,0 +1,110 @@ +package github + +import ( + "context" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "os" + "strings" + "testing" +) + +func labelGateReviewServer(t *testing.T, labels []string, prStatus int, reviews *int) *httptest.Server { + t.Helper() + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/reviews"): + if reviews != nil { + *reviews++ + } + w.Header().Set("Content-Type", "application/json") + _, _ = io.WriteString(w, `{"id":1,"html_url":"https://example.test/pr/5#review-1"}`) + case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/pulls/"): + if prStatus != 0 { + w.WriteHeader(prStatus) + return + } + ls := make([]map[string]string, 0, len(labels)) + for _, l := range labels { + ls = append(ls, map[string]string{"name": l}) + } + b, _ := json.Marshal(map[string]any{"number": 5, "user": map[string]string{"login": "hive-bot", "type": "User"}, "labels": ls}) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write(b) + default: + w.WriteHeader(http.StatusNotFound) + } + })) +} + +// review.require_labels is enforced by the relay itself: a review aimed at a +// PR carrying none of the labels is refused (.denied, audited) before anything +// reaches GitHub; a labelled PR passes; with the gate off nothing changes. +func TestReviewRelay_RequireLabelsGate(t *testing.T) { + run := func(t *testing.T, required, prLabels []string, prStatus int) (reviews int, reqPath string, resp ReviewResponse, audits []string) { + t.Helper() + srv := labelGateReviewServer(t, prLabels, prStatus, &reviews) + defer srv.Close() + c := reviewTestClient(t, srv.URL) + c.SetHiveIdentity(HiveIdentity{AIAuthor: "hive-bot"}) + c.SetReviewRequireLabels(func() []string { return required }) + c.SetAttributionAudit(func(action, detail, agent string) { audits = append(audits, action+":"+detail) }) + dir := withReviewDir(t) + var err error + reqPath, err = WriteReviewRequest(dir, ReviewRequest{Repo: "o/r", Number: 5, Event: "comment", Body: "looks fine", Agent: "reviewer"}) + if err != nil { + t.Fatal(err) + } + c.ProcessReviewRequestsOnce(context.Background()) + resp = readReviewResult(t, reqPath) + return + } + + t.Run("unlabelled PR is refused", func(t *testing.T) { + reviews, reqPath, resp, audits := run(t, []string{"hive-review"}, []string{"bug"}, 0) + if reviews != 0 { + t.Fatalf("review must not reach GitHub, got %d", reviews) + } + if resp.OK || !strings.Contains(resp.Error, "review.require_labels") || !strings.Contains(resp.Error, "hive-review") { + t.Fatalf("result must explain the gate: %+v", resp) + } + if _, err := os.Stat(reqPath + ".denied"); err != nil { + t.Fatalf("request must be quarantined as .denied: %v", err) + } + found := false + for _, a := range audits { + if strings.HasPrefix(a, AuditActionReviewLabelGateRefused+":") { + found = true + } + } + if !found { + t.Fatalf("refusal must be audited, got %v", audits) + } + }) + + t.Run("labelled PR passes case-insensitively", func(t *testing.T) { + reviews, _, resp, _ := run(t, []string{"hive-review"}, []string{"bug", "Hive-Review"}, 0) + if reviews != 1 || !resp.OK { + t.Fatalf("labelled PR must be reviewed: reviews=%d resp=%+v", reviews, resp) + } + }) + + t.Run("gate off never fetches labels", func(t *testing.T) { + reviews, _, resp, _ := run(t, nil, nil, http.StatusInternalServerError) + if reviews != 1 || !resp.OK { + t.Fatalf("gate off must post without reading the PR: reviews=%d resp=%+v", reviews, resp) + } + }) + + t.Run("label fetch failure is retried, not posted", func(t *testing.T) { + reviews, reqPath, resp, _ := run(t, []string{"hive-review"}, nil, http.StatusInternalServerError) + if reviews != 0 || resp.OK { + t.Fatalf("fetch failure must not post: reviews=%d resp=%+v", reviews, resp) + } + if _, err := os.Stat(reqPath + ".denied"); err == nil { + t.Fatalf("transient fetch failure must not be a policy denial") + } + }) +} diff --git a/src/pkg/github/review_request_watcher.go b/src/pkg/github/review_request_watcher.go index a57e8d8adb..2322fad114 100644 --- a/src/pkg/github/review_request_watcher.go +++ b/src/pkg/github/review_request_watcher.go @@ -13,6 +13,7 @@ import ( "time" gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/config" "github.com/hivecommons/hive/pkg/effects" "github.com/hivecommons/hive/pkg/review" ) @@ -356,6 +357,22 @@ func (c *Client) handleOneReviewRequest(ctx context.Context, path string, nowFn c.denyReviewRequest(path, req, RepoPausedReason(req.Repo), nowFn) return } + // review.require_labels (PR approval allow-list): the server-side half of + // the gate. The kick lists never offer an unlabelled PR, but a reviewer + // that found one anyway — or was told to by a prompt — is refused here, + // whatever the event: a review, a thread reply, a resolution or a bare + // record_verdict are all "the hive reviewed this PR". Labels are read + // fresh with the App token; a fetch failure is retried, not waved through. + if admitted, err := c.reviewLabelGateAdmits(ctx, req.Repo, req.Number); err != nil { + c.retryOrQuarantineReview(path, req, fmt.Errorf("review.require_labels: could not read PR labels: %w", err), nowFn) + return + } else if !admitted { + c.recordWriteAudit(AuditActionReviewLabelGateRefused, c.attributionMeta(req.Agent), + WriteTarget{Repo: req.Repo, Number: req.Number}, + "event", strings.TrimSpace(req.Event), "require_labels", strings.Join(c.reviewRequireLabels(), ",")) + c.denyReviewRequest(path, req, config.ReviewRequireLabelsSkipReason+" ("+strings.Join(c.reviewRequireLabels(), ", ")+")", nowFn) + return + } if resolveThread || threadReply { c.handleReviewThreadRequest(ctx, path, req, threadID, resolveThread, nowFn) return diff --git a/src/pkg/scheduler/kickmessage.go b/src/pkg/scheduler/kickmessage.go index 425d991c7e..4afc1544fc 100644 --- a/src/pkg/scheduler/kickmessage.go +++ b/src/pkg/scheduler/kickmessage.go @@ -651,12 +651,28 @@ func issuePriorityMarker(issue github.Issue) string { return "[hive-filed]" } +// reviewLabelGateNotice renders review.require_labels as prompt text for the +// reviewer lane, mirroring issueFilterNotice: the list is already filtered, +// and the agent is told why so it does not go looking for the missing PRs. +func (s *Scheduler) reviewLabelGateNotice(agentName string) string { + if s == nil || s.cfg == nil || s.cfg.BaseAgentName(agentName) != "reviewer" { + return "" + } + labels := s.cfg.Review.NormalizedRequireLabels() + if len(labels) == 0 { + return "" + } + return fmt.Sprintf("REVIEW LABEL GATE (operator policy — already enforced; the PR list below reflects it):\n The hive reviews ONLY PRs carrying at least one of these labels: %s\n ⛔ Do NOT review, comment on, or fetch PRs outside this list; the hive-review relay refuses them.\n", strings.Join(labels, ", ")) +} + func (s *Scheduler) formatPRListWithPolicyForAgent(actionable *github.ActionableResult, agentName string) (string, bool) { + notice := s.reviewLabelGateNotice(agentName) prs := s.prListItemsForAgent(actionable, agentName) if len(prs) == 0 { - return "(none)", false + return notice + "(none)", false } var b strings.Builder + b.WriteString(notice) failClosed := false limit := s.prCap() shown := fairShareByRepo(prs, limit, func(pr github.PullRequest) string { return pr.Repo }) @@ -704,6 +720,7 @@ func (s *Scheduler) prListItemsForAgent(actionable *github.ActionableResult, age } prs := make([]github.PullRequest, 0, len(actionable.PRs.Items)+len(actionable.PRs.Held)) seen := map[string]bool{} + gated := 0 add := func(items []github.PullRequest) { for _, pr := range items { fullRepo := config.QualifyRepo(s.cfg.Project.Org, pr.Repo) @@ -712,11 +729,23 @@ func (s *Scheduler) prListItemsForAgent(actionable *github.ActionableResult, age continue } seen[key] = true + // review.require_labels (PR approval allow-list): an unlabelled + // PR never reaches the reviewer's list. The relay refuses it + // too, so this is the quiet half of the gate, not the only one. + if !s.cfg.Review.AdmitsPRLabels(pr.Labels) { + gated++ + continue + } prs = append(prs, pr) } } add(actionable.PRs.Items) add(actionable.PRs.Held) + if gated > 0 && s.logger != nil { + s.logger.Info("review label gate withheld PRs from reviewer kick", + "agent", agentName, "withheld", gated, "listed", len(prs), + "require_labels", strings.Join(s.cfg.Review.NormalizedRequireLabels(), ",")) + } sort.SliceStable(prs, func(i, j int) bool { a, b := prs[i], prs[j] if a.ReviewRank > 0 && b.ReviewRank > 0 && a.ReviewRank != b.ReviewRank { diff --git a/src/pkg/scheduler/review_label_gate_test.go b/src/pkg/scheduler/review_label_gate_test.go new file mode 100644 index 0000000000..d66263e779 --- /dev/null +++ b/src/pkg/scheduler/review_label_gate_test.go @@ -0,0 +1,76 @@ +package scheduler + +import ( + "strings" + "testing" + + "github.com/hivecommons/hive/pkg/github" +) + +// review.require_labels: the reviewer's PR list offers only labelled PRs and +// says why; other agents' lists are untouched; an empty gate changes nothing. +func TestFormatPRList_ReviewRequireLabelsGate(t *testing.T) { + actionable := &github.ActionableResult{} + actionable.PRs.Items = []github.PullRequest{ + {Repo: "actions", Number: 10, Title: "labelled", Author: "a", Labels: []string{"bug", "Hive-Review"}}, + {Repo: "actions", Number: 11, Title: "unlabelled", Author: "b"}, + } + actionable.PRs.Held = []github.PullRequest{ + {Repo: "actions", Number: 12, Title: "held unlabelled", Author: "c", Labels: []string{"hold"}}, + } + + t.Run("gate off lists every PR", func(t *testing.T) { + s := newSchedulerWithIoscan(false) + s.cfg.Project.Org = "org" + out, _ := s.formatPRListWithPolicyForAgent(actionable, "reviewer") + for _, n := range []string{"#10", "#11", "#12"} { + if !strings.Contains(out, n) { + t.Fatalf("gate off must list %s: %q", n, out) + } + } + if strings.Contains(out, "REVIEW LABEL GATE") { + t.Fatalf("gate off must not render the notice: %q", out) + } + }) + + t.Run("gate on withholds unlabelled PRs from the reviewer", func(t *testing.T) { + s := newSchedulerWithIoscan(false) + s.cfg.Project.Org = "org" + s.cfg.Review.RequireLabels = []string{"hive-review"} + out, _ := s.formatPRListWithPolicyForAgent(actionable, "reviewer") + if !strings.Contains(out, "#10") { + t.Fatalf("labelled PR must be listed: %q", out) + } + if strings.Contains(out, "#11") || strings.Contains(out, "#12") { + t.Fatalf("unlabelled PRs must be withheld: %q", out) + } + if !strings.Contains(out, "REVIEW LABEL GATE") || !strings.Contains(out, "hive-review") { + t.Fatalf("notice must name the gate labels: %q", out) + } + }) + + t.Run("gate on with nothing labelled renders the notice and (none)", func(t *testing.T) { + s := newSchedulerWithIoscan(false) + s.cfg.Project.Org = "org" + s.cfg.Review.RequireLabels = []string{"hive-review"} + none := &github.ActionableResult{} + none.PRs.Items = []github.PullRequest{{Repo: "actions", Number: 11, Title: "unlabelled", Author: "b"}} + out, _ := s.formatPRListWithPolicyForAgent(none, "reviewer") + if !strings.Contains(out, "REVIEW LABEL GATE") || !strings.HasSuffix(out, "(none)") { + t.Fatalf("expected notice + (none): %q", out) + } + }) + + t.Run("non-reviewer lists are not gated", func(t *testing.T) { + s := newSchedulerWithIoscan(false) + s.cfg.Project.Org = "org" + s.cfg.Review.RequireLabels = []string{"hive-review"} + out, _ := s.formatPRListWithPolicyForAgent(actionable, "scanner") + if !strings.Contains(out, "#11") { + t.Fatalf("scanner's PR list must not be review-gated: %q", out) + } + if strings.Contains(out, "REVIEW LABEL GATE") { + t.Fatalf("notice is reviewer-only: %q", out) + } + }) +}