diff --git a/changelog.d/changed-reviewer-question-autodecide.md b/changelog.d/changed-reviewer-question-autodecide.md new file mode 100644 index 0000000000..57d332b6c5 --- /dev/null +++ b/changelog.d/changed-reviewer-question-autodecide.md @@ -0,0 +1 @@ +- A reviewer's scope / follow-up question on a hive-authored PR with CI green and no unresolved finding no longer parks the PR at 085 "Needs human decision": the dashboard classifies the ask and runs the existing *Let hive decide* pipeline itself (approve → merge queue plus a `follow-up:` issue; direction → fixer; owner-only → park with the recommendation), once per head, while policy sign-off asks (workflow edits from forks, OWNERS / secrets / permissions, auto-merge config, sentinel) stay a person's. Off switch `governor.review.auto_decide_questions`; counter `review_questions_autodecided_1h` on `/api/status`. diff --git a/src/docs/api-reference/auth.md b/src/docs/api-reference/auth.md index f53613dd29..459faedfbf 100644 --- a/src/docs/api-reference/auth.md +++ b/src/docs/api-reference/auth.md @@ -24,6 +24,6 @@ Part of the [Dashboard REST API reference](../api-reference.md); see that page f | `GET` | `/api/openrouter/models` | Dashboard auth/session | Open Router Models | `pkg/dashboard/openrouter.go:47` | | `GET` | `/api/openrouter/credit` | Dashboard auth/session | Open Router Credit | `pkg/dashboard/openrouter.go:48` | | `GET` | `/openrouter/callback` | Public | Open Router Callback | `pkg/dashboard/openrouter.go:49` | -| `POST` | `/api/github-app/recheck` | Dashboard auth/session | GitHub App Recheck | `pkg/dashboard/server.go:1662` | -| `POST` | `/api/github-app/install-clicked` | Dashboard auth/session | GitHub App Install Clicked | `pkg/dashboard/server.go:1663` | -| `GET` | `/gh-setup` | Public | GitHub App Setup Callback | `pkg/dashboard/server.go:1664` | +| `POST` | `/api/github-app/recheck` | Dashboard auth/session | GitHub App Recheck | `pkg/dashboard/server.go:1665` | +| `POST` | `/api/github-app/install-clicked` | Dashboard auth/session | GitHub App Install Clicked | `pkg/dashboard/server.go:1666` | +| `GET` | `/gh-setup` | Public | GitHub App Setup Callback | `pkg/dashboard/server.go:1667` | diff --git a/src/docs/api-reference/dashboard-misc.md b/src/docs/api-reference/dashboard-misc.md index f19178f89f..66daa880fd 100644 --- a/src/docs/api-reference/dashboard-misc.md +++ b/src/docs/api-reference/dashboard-misc.md @@ -80,7 +80,7 @@ The monitor renders CI and Review concurrently on one shared PR row; see | `POST` | `/api/hives/{id}/heartbeat` | Dashboard auth/session | Hives Heartbeat | `pkg/dashboard/api_contribute.go:377` | | `DELETE` | `/api/hives/{id}` | Owner only | Hives Delete | `pkg/dashboard/api_contribute.go:378` | | `POST` | `/api/hives/onboard` | Dashboard auth/session | Hives Onboard | `pkg/dashboard/api_contribute.go:379` | -| `GET` | `/sso` | Public | SSO | `pkg/dashboard/server.go:1669` | +| `GET` | `/sso` | Public | SSO | `pkg/dashboard/server.go:1672` | ### Activity observation provenance The CI snapshot retains real check/node IDs, head, App ID and transition times. diff --git a/src/docs/api-reference/health-status-events.md b/src/docs/api-reference/health-status-events.md index 5e9728defb..48e9983a4f 100644 --- a/src/docs/api-reference/health-status-events.md +++ b/src/docs/api-reference/health-status-events.md @@ -24,17 +24,17 @@ Part of the [Dashboard REST API reference](../api-reference.md); see that page f | `GET` | `/api/version` | Dashboard auth/session | Build/version metadata; includes `upgradeMarker` (`target`, `current`, `attempts`, `maxAttempts`, `failed`, `requestedAt`, `lastError`) while a self-upgrade is in flight or has failed ([#6765](https://github.com/hivecommons/hive/issues/6765)), the hub-delivered `upgradePolicy` (`hub_managed`, `spoke_managed`, `schedule`, `schedule_hour`, `schedule_timezone`, `schedule_weekday`, `paused`, `branch`, `channel`, `target_sha`, `target_resolved`, `armed_target`, and `next_update_at` — the hub's RFC3339 ETA for the next promotion into the `stable` channel, omitted when unknown ([#10256](https://github.com/hivecommons/hive/issues/10256))) when available, and an `autoUpdate` object (`enabled`, `state` — one of `disabled`/`up_to_date`/`behind`/`retrying`/`failed`/`unknown` — `healthy`, `period`, `targetBranch`, `targetCommit`, `currentCommit`, `commitsBehind`, `lastAttemptAt`, `lastError`, `detail`, and `nextUpdateAt` — the hub's RFC3339 ETA for the next update, omitted when unknown, disabled or paused ([#10257](https://github.com/hivecommons/hive/issues/10257))) that never reports a failed or unknown update as healthy ([#6962](https://github.com/hivecommons/hive/issues/6962), [#6963](https://github.com/hivecommons/hive/issues/6963)). Also `startedAt` (RFC3339 process start — changes on every pod roll even when the build does not, so the dashboard can say "Restarted" vs "Upgraded") and, until the first status is published, `startupPhase`/`startupPhaseLabel` (the boot phase a fresh pod is in) ([#15788](https://github.com/hivecommons/hive/issues/15788)) | `pkg/dashboard/api.go:44` | | `GET` | `/api/version/release-notes` | Dashboard auth/session | Changelog gap between two revisions: `?from=&to=` returns `{from:{sha,version?}, to:{sha,ref,version?}, sections:[{version,date,categories:{added,changed,fixed,security,deprecated}}], unreleased?, truncated, source, error?}` — the `CHANGELOG.md` release sections present at `to` and absent at `from`, newest first, plus (when `to` is not a release tag) the `changelog.d/` fragments new at `to` under an `Unreleased (in this build)` section. Fetch failures return `source: "unavailable"` with `error` rather than an error status; results are cached per (from, to) for an hour ([#11065](https://github.com/hivecommons/hive/issues/11065)) | `pkg/dashboard/api.go:45` | | `POST` | `/api/release-channel` | Owner only | Hosted spoke self-service release-channel selector; relays `stable`/`candidate`/`edge` to the hub's existing switch-branch endpoint with the spoke dashboard-token proof, and reports the requested channel as pending until the Deployment image lands | `pkg/dashboard/api.go:69` | -| `GET` | `/api/health` | Public | Basic health probe | `pkg/dashboard/server.go:1642` | -| `GET` | `/api/health/deep` | Public | Deep health probe | `pkg/dashboard/server.go:1643` | -| `GET` | `/api/livez` | Public | Kubernetes liveness probe | `pkg/dashboard/server.go:1644` | -| `GET` | `/metrics` | Registered only when `HIVE_METRICS_ENABLED`; requires `Authorization: ****** (403 if the token is unset) | Prometheus metrics | `pkg/dashboard/server.go:1650` | -| `GET` | `/api/status` | Dashboard auth/session | Dashboard aggregate status; `labelPresets[]` (`{key,label,description,labels[],states[]}`) lists the built-in label-filter presets for the Projects cards, led by `needs-human` whose `labels[]` are the effective hard-suppress labels plus `needs-signal`, the hold labels, `stage/071-needs-author` and the reporter-trust awaiting label (#12172); accepts `?fields=a,b` for top-level selection and `?omit=repos` to drop heavy sections, and honors `Accept-Encoding: gzip`; `agents[]` includes busy-pane visibility fields (`kicksUndeliverable`, `busySince`, `lastTranscriptActivity`, `condition`, `conditionMessage`) when a Working pane cannot accept kicks; a working agent also carries `agents[].run` — the live per-run line (#14576): `{since, elapsedS, costUSD, tokens, priced, tests, filesChanged}`, the estimated spend/tokens since the kick was delivered (`lastKickAt`, falling back to `busySince`) (delta of the token collector's per-agent cumulative figure; `priced:false` when the agent has none yet), the last visible test-runner result (`pass`/`fail`/empty) and the last `N files changed` git summary (`-1` when none) parsed from the captured pane; top-level `fleetRun` `{running, hourUSD, hourPriced, dayUSD, weekUSD, monthUSD, windowsPriced}` is the governor strip's rotating `running N · $X this hour / today / this week / this month` figure (#15614) — the hour is the rise since the top of the clock hour, the wider windows are the daily cost ledger summed from 00:00 UTC today, Monday 00:00 UTC (ISO week) and the 1st 00:00 UTC plus the rise since the ledger's last snapshot, `windowsPriced:false` when there is no ledger yet; `sla` (fleet) and `repos[].sla` (per repo) carry the Delivery SLA block derived from the lifecycle timeline with no extra GitHub calls: `windows.{7d,30d}.{open_to_triage,triage_to_start,start_to_pr,pr_to_merge,open_to_merge}` as `{p50_h,p95_h,n}` (hours, nearest-rank), `history[]` of up to 30 daily buckets `{day,start_to_pr_p50,pr_to_merge_p50,open_to_merge_p50,n_started,n_pr,n_merged}` persisted under `/data/sla-history.json`, `stale_in_progress[]` (`{number,repo,started_at,age_h,agent}` — issues carrying `hive/in-progress` longer than the agent claim TTL with no linked PR), `stale_count`, `merged_7d`, `claim_ttl_s` and, on the fleet block, the effective `targets` (`start_to_pr_h`, `pr_to_merge_h`, `open_to_merge_h`); `staleSince` and `repos[].countsStale` / `countsStaleSince` mark totals served from last-good figures — the previous enumeration (#13382) or the ForgeState cache's last-good counts (#13376); `ci_failure_signals` `{count, last, kicks, unmatched}` is the webhook CI-failure signal store — distinct red (repo, head, check) signals retained, the most recent one `{repo, number, head_sha, check, conclusion, details_url, agent, at}`, webhook-triggered kicks/nudges since boot, and failure deliveries whose head matched no open PR; `hotspots` `{count, actionable, filed}` is the merge-hotspot advisor's one-line summary (files at/above the thresholds, those with a split recommendation, those with a modularize issue filed), absent when the advisor is not wired — details on `GET /api/hotspots`; `writeback.dedupe` `{enabled, dedupe_same_item, dedupe_burst, rate_capped, hourly_cap, burst_items, burst_window_s, same_item_ttl_s}` is the write-back gate's content layer — the effective `writeback.dedupe.*` settings and the last-hour counts of writes held as `duplicate`, `burst` and `rate-capped` (each also listed under `GET /api/writeback/suppressed`); `config.gate_regressions[]` `{gate, previous, effective, source}` is the boot-time safety-gate regression list (#16438) — gates a previous runtime overlay on the PVC carried that the running config lacks or loosens (`review.require_labels`, `project.issue_filter.require_labels`, `project.paused_repos[]`, `writeback.per_repo[].mode`), empty on a healthy boot | `pkg/dashboard/server.go:1655` | -| `GET` | `/api/status/summary` | Dashboard auth/session | Compact agent/governor/budget status summary for pollers; honors `Accept-Encoding: gzip` | `pkg/dashboard/server.go:1656` | -| `GET` | `/api/overview/issues.csv` | Dashboard auth/session | Cached Overview issue-band export as browser-compatible CSV; filters `band`, `repo`/`repos`, `stale`, and `held` are ANDed. | `pkg/dashboard/server.go:1657` | -| `GET` | `/api/overview/issues.json` | Dashboard auth/session | Cached Overview issue-band export as typed JSON with `bands[]` metadata and row fields matching the CSV columns. | `pkg/dashboard/server.go:1658` | -| `GET` | `/api/overview/prs.csv` | Dashboard auth/session | Cached Overview PR-band export as browser-compatible CSV; filters `band`, `repo`/`repos`, `stale`, and `held` are ANDed. | `pkg/dashboard/server.go:1659` | -| `GET` | `/api/overview/prs.json` | Dashboard auth/session | Cached Overview PR-band export as typed JSON with `bands[]` metadata and row fields matching the CSV columns. | `pkg/dashboard/server.go:1660` | -| `GET` | `/api/events` | Dashboard auth/session | Server-sent event stream | `pkg/dashboard/server.go:1661` | +| `GET` | `/api/health` | Public | Basic health probe | `pkg/dashboard/server.go:1645` | +| `GET` | `/api/health/deep` | Public | Deep health probe | `pkg/dashboard/server.go:1646` | +| `GET` | `/api/livez` | Public | Kubernetes liveness probe | `pkg/dashboard/server.go:1647` | +| `GET` | `/metrics` | Registered only when `HIVE_METRICS_ENABLED`; requires `Authorization: ****** (403 if the token is unset) | Prometheus metrics | `pkg/dashboard/server.go:1653` | +| `GET` | `/api/status` | Dashboard auth/session | Dashboard aggregate status; `labelPresets[]` (`{key,label,description,labels[],states[]}`) lists the built-in label-filter presets for the Projects cards, led by `needs-human` whose `labels[]` are the effective hard-suppress labels plus `needs-signal`, the hold labels, `stage/071-needs-author` and the reporter-trust awaiting label (#12172); accepts `?fields=a,b` for top-level selection and `?omit=repos` to drop heavy sections, and honors `Accept-Encoding: gzip`; `agents[]` includes busy-pane visibility fields (`kicksUndeliverable`, `busySince`, `lastTranscriptActivity`, `condition`, `conditionMessage`) when a Working pane cannot accept kicks; a working agent also carries `agents[].run` — the live per-run line (#14576): `{since, elapsedS, costUSD, tokens, priced, tests, filesChanged}`, the estimated spend/tokens since the kick was delivered (`lastKickAt`, falling back to `busySince`) (delta of the token collector's per-agent cumulative figure; `priced:false` when the agent has none yet), the last visible test-runner result (`pass`/`fail`/empty) and the last `N files changed` git summary (`-1` when none) parsed from the captured pane; top-level `fleetRun` `{running, hourUSD, hourPriced, dayUSD, weekUSD, monthUSD, windowsPriced}` is the governor strip's rotating `running N · $X this hour / today / this week / this month` figure (#15614) — the hour is the rise since the top of the clock hour, the wider windows are the daily cost ledger summed from 00:00 UTC today, Monday 00:00 UTC (ISO week) and the 1st 00:00 UTC plus the rise since the ledger's last snapshot, `windowsPriced:false` when there is no ledger yet; `sla` (fleet) and `repos[].sla` (per repo) carry the Delivery SLA block derived from the lifecycle timeline with no extra GitHub calls: `windows.{7d,30d}.{open_to_triage,triage_to_start,start_to_pr,pr_to_merge,open_to_merge}` as `{p50_h,p95_h,n}` (hours, nearest-rank), `history[]` of up to 30 daily buckets `{day,start_to_pr_p50,pr_to_merge_p50,open_to_merge_p50,n_started,n_pr,n_merged}` persisted under `/data/sla-history.json`, `stale_in_progress[]` (`{number,repo,started_at,age_h,agent}` — issues carrying `hive/in-progress` longer than the agent claim TTL with no linked PR), `stale_count`, `merged_7d`, `claim_ttl_s` and, on the fleet block, the effective `targets` (`start_to_pr_h`, `pr_to_merge_h`, `open_to_merge_h`); `staleSince` and `repos[].countsStale` / `countsStaleSince` mark totals served from last-good figures — the previous enumeration (#13382) or the ForgeState cache's last-good counts (#13376); `ci_failure_signals` `{count, last, kicks, unmatched}` is the webhook CI-failure signal store — distinct red (repo, head, check) signals retained, the most recent one `{repo, number, head_sha, check, conclusion, details_url, agent, at}`, webhook-triggered kicks/nudges since boot, and failure deliveries whose head matched no open PR; `hotspots` `{count, actionable, filed}` is the merge-hotspot advisor's one-line summary (files at/above the thresholds, those with a split recommendation, those with a modularize issue filed), absent when the advisor is not wired — details on `GET /api/hotspots`; `writeback.dedupe` `{enabled, dedupe_same_item, dedupe_burst, rate_capped, hourly_cap, burst_items, burst_window_s, same_item_ttl_s}` is the write-back gate's content layer — the effective `writeback.dedupe.*` settings and the last-hour counts of writes held as `duplicate`, `burst` and `rate-capped` (each also listed under `GET /api/writeback/suppressed`); `config.gate_regressions[]` `{gate, previous, effective, source}` is the boot-time safety-gate regression list (#16438) — gates a previous runtime overlay on the PVC carried that the running config lacks or loosens (`review.require_labels`, `project.issue_filter.require_labels`, `project.paused_repos[]`, `writeback.per_repo[].mode`), empty on a healthy boot | `pkg/dashboard/server.go:1658` | +| `GET` | `/api/status/summary` | Dashboard auth/session | Compact agent/governor/budget status summary for pollers; honors `Accept-Encoding: gzip` | `pkg/dashboard/server.go:1659` | +| `GET` | `/api/overview/issues.csv` | Dashboard auth/session | Cached Overview issue-band export as browser-compatible CSV; filters `band`, `repo`/`repos`, `stale`, and `held` are ANDed. | `pkg/dashboard/server.go:1660` | +| `GET` | `/api/overview/issues.json` | Dashboard auth/session | Cached Overview issue-band export as typed JSON with `bands[]` metadata and row fields matching the CSV columns. | `pkg/dashboard/server.go:1661` | +| `GET` | `/api/overview/prs.csv` | Dashboard auth/session | Cached Overview PR-band export as browser-compatible CSV; filters `band`, `repo`/`repos`, `stale`, and `held` are ANDed. | `pkg/dashboard/server.go:1662` | +| `GET` | `/api/overview/prs.json` | Dashboard auth/session | Cached Overview PR-band export as typed JSON with `bands[]` metadata and row fields matching the CSV columns. | `pkg/dashboard/server.go:1663` | +| `GET` | `/api/events` | Dashboard auth/session | Server-sent event stream | `pkg/dashboard/server.go:1664` | | `GET` | `/api/swarm` | Dashboard auth/session | Current swarm status: display name, duration, active repo, expiry, persisted prep metrics when available, and idle-unlock fields. | `pkg/dashboard/swarm.go:310` | | `POST` | `/api/swarm` | Owner only | Start a 24h repo swarm for a configured repo, returning 409 when another swarm is active and 423 when a previous swarm keeps the next swarm locked until enough agents are idle unless body `{"repo":"owner/name","force":true}` is used; saves the active record before repo prep, persists prep metrics, and announces the start to Discord when configured. | `pkg/dashboard/swarm.go:311` | | `DELETE` | `/api/swarm` | Owner only | End the active swarm, score issues closed and PRs merged in the window, append it to history, and announce the result to Discord when configured. | `pkg/dashboard/swarm.go:312` | diff --git a/src/docs/api-reference/snapshots-and-style.md b/src/docs/api-reference/snapshots-and-style.md index 2a7f4e28bc..6ab4807c0b 100644 --- a/src/docs/api-reference/snapshots-and-style.md +++ b/src/docs/api-reference/snapshots-and-style.md @@ -6,7 +6,7 @@ Part of the [Dashboard REST API reference](../api-reference.md); see that page f | Method | Path | Auth | Purpose | Source | |---|---|---|---|---| | `GET` | `/api/style` | Public | Sanitized custom dashboard CSS | `pkg/dashboard/api.go:46` | -| `GET` | `/branding/custom.css` | Dashboard auth/session | Operator branding stylesheet override, read per request (see [branding](../branding.md)) | `pkg/dashboard/server.go:1732` | +| `GET` | `/branding/custom.css` | Dashboard auth/session | Operator branding stylesheet override, read per request (see [branding](../branding.md)) | `pkg/dashboard/server.go:1735` | | `GET` | `/api/snapshot/frame-ancestors` | Public | Snapshot framing allowlist | `pkg/dashboard/api.go:82` | | `GET` | `/api/snapshot` | Public | Snapshot data | `pkg/dashboard/api.go:83` | | `GET` | `/snapshot` | Public | Public read-only snapshot page | `pkg/dashboard/api.go:84` | diff --git a/src/docs/operator-reference/sweeps-and-operations.md b/src/docs/operator-reference/sweeps-and-operations.md index e194a4d4b3..3c7117cd48 100644 --- a/src/docs/operator-reference/sweeps-and-operations.md +++ b/src/docs/operator-reference/sweeps-and-operations.md @@ -392,6 +392,7 @@ between. Two mechanisms close that gap |---|---|---| | `scanner.skip_issues_with_open_pr` | `true` (unset) | Set `false` to turn off both the pre-dispatch guard and the duplicate-fix sweep; the claim ledger alone then decides, as before. | | `governor.auto_close_superseded` | `true` (unset) | At ACMM L6, a **hive-authored** PR whose closing targets are all closed, one of them by a different merged PR, and whose head conflicts with the base (or whose files are a subset of that PR's) is closed by the hive itself as superseded — `hive/superseded`, one notice naming the superseding PR — in the self-authored auto-merge lane (before any review cycle), the supersession sweep, and close-on-merge of the superseding PR. Human-authored PRs are never auto-closed. Set `false` to report the refusal once and leave the PR for a person, as before ([#14965](https://github.com/hivecommons/hive/pull/14965)). See [labels-and-control-signals.md § Supersession](../labels-and-control-signals/stage-labels.md#supersession-closed-issue-another-merged-pr). | +| `governor.review.auto_decide_questions` | `true` (unset) | A review perspective that ends with a **question** rather than a finding used to park the PR at 085 "Needs human decision" on its wording alone. The dashboard now classifies the ask. **Policy sign-off** stays a person's: the PR edits `.github/workflows/*` or `.github/actions/*` from a fork; touches OWNERS / CODEOWNERS / SECURITY / rulesets / secrets / key material; adds or removes `auto_merge`, `human_merge_paths` or `self_authored_decision_policy` config; or the ask names an owner-only guard (sentinel, GitHub/App permissions, unauthorized, tokens / secrets / credentials, branch protection). **Scope / follow-up question** — "should we also…", "merge ahead of…", "apply X from the PR body", "confirm Y is acceptable" — on a hive-authored (or hive-adopted) PR with CI green and no unresolved finding on the head runs the existing *Let hive decide* pipeline with the hive as requester: an approve verdict admits the PR to the merge queue and files the question as a `follow-up: …` issue linked to the PR; a direction verdict routes the fixer (070); an owner-only or recommend-only verdict keeps the 085 park with the hive's recommendation on the row ("needs your sign-off"). At most one auto-decide per head (a new push asks again), one INFO line `reviewer question auto-decided` per run, and `review_questions_autodecided_1h` on `/api/status`. Set `false` to park every reviewer question for a person, as before. | ## What hive writes to GitHub, and when diff --git a/src/pkg/config/config.go b/src/pkg/config/config.go index 197209e3da..401f97b80d 100644 --- a/src/pkg/config/config.go +++ b/src/pkg/config/config.go @@ -1723,6 +1723,10 @@ type GovernorConfig struct { // the stage/085 decision park. Default on (hivecommons/hive#14965). AutoCloseSuperseded *bool `yaml:"auto_close_superseded,omitempty" json:"auto_close_superseded,omitempty"` + // Review tunes what the governor does with the review swarm's verdicts + // on the hive's own PRs. See GovernorReviewConfig. + Review GovernorReviewConfig `yaml:"review,omitempty" json:"review,omitempty"` + // Hotspot* tune the merge-hotspot advisor, which scores files that // concurrent PRs keep touching (from merge-train file lists and // rebase-conflict records the hive already holds — no extra GitHub @@ -2241,6 +2245,27 @@ func (g GovernorConfig) AutoCloseSupersededEnabled() bool { return g.AutoCloseSuperseded == nil || *g.AutoCloseSuperseded } +// GovernorReviewConfig is governor.review: how the governor treats the +// review swarm's verdicts on hive-authored PRs. +type GovernorReviewConfig struct { + // AutoDecideQuestions lets the hive answer a reviewer's scope or + // follow-up QUESTION on its own green, finding-free PR through the + // delegated-decision path instead of parking the PR at stage/085 for a + // person (hivecommons/docs#618: "this is auto-merge material, not + // needs-human"). Policy sign-off asks — workflow edits from a fork, + // secrets / permissions / OWNERS / security paths, auto_merge or + // human_merge_paths changes, an explicit owner-only guard in the ask — + // still park. At most one auto-decision per head; a new push re-asks. + // Default on. See review.ClassifyDecisionQuestion for the two classes. + AutoDecideQuestions *bool `yaml:"auto_decide_questions,omitempty" json:"auto_decide_questions,omitempty"` +} + +// AutoDecideQuestionsEnabled resolves GovernorReviewConfig.AutoDecideQuestions +// with its default (on). +func (r GovernorReviewConfig) AutoDecideQuestionsEnabled() bool { + return r.AutoDecideQuestions == nil || *r.AutoDecideQuestions +} + // Defaults for the merge-hotspot advisor (GovernorConfig.Hotspot*). const ( DefaultHotspotWindowDays = 7 diff --git a/src/pkg/config/testdata/agent_runtime_golden.json b/src/pkg/config/testdata/agent_runtime_golden.json index 9850c0d2d4..00fa498043 100644 --- a/src/pkg/config/testdata/agent_runtime_golden.json +++ b/src/pkg/config/testdata/agent_runtime_golden.json @@ -400,6 +400,7 @@ "queue_health": {} }, "auto_close_superseded": true, + "review": {}, "project_observability": {}, "work_source": { "type": "", diff --git a/src/pkg/dashboard/review_question_autodecide.go b/src/pkg/dashboard/review_question_autodecide.go new file mode 100644 index 0000000000..e37d8d127b --- /dev/null +++ b/src/pkg/dashboard/review_question_autodecide.go @@ -0,0 +1,473 @@ +package dashboard + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "log/slog" + "os" + "path/filepath" + "sort" + "strconv" + "strings" + "sync" + "time" + + gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/delegatedecision" + ghpkg "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/review" + "github.com/hivecommons/hive/pkg/writeback" +) + +// Reviewer-question auto-decide (hivecommons/hive#16578 follow-up). +// +// A review perspective on the hive's own PR sometimes ends with a QUESTION +// rather than a finding — "should this merge ahead of the workflow edit?", +// "confirm the relay may lift the hold" — and the tracker parked the row at +// 085 "Needs human decision" on the wording alone (docs#618 sat with CI 4/4 +// and Review 5/5 clean; hive#16456 likewise). The owner's verdict on those: +// "this is auto-merge material, not needs-human". +// +// This sweep classifies the ask (review.ClassifyDecisionQuestion): a +// POLICY SIGN-OFF stays the person's; a SCOPE / FOLLOW-UP QUESTION on a +// hive-authored PR with CI green and no unresolved finding runs the +// existing "Let hive decide" pipeline (runDelegatedDecision) with the +// hive as requester. An approve verdict admits the PR to the merge queue +// and files the question as a follow-up issue; a direction verdict routes +// the fixer; an owner-only / recommend-only verdict keeps the park, with +// the recommendation on the row. At most one auto-delegate runs per head +// (a new push asks again). Off switch: governor.review.auto_decide_questions. + +const ( + // reviewQuestionAutodecideInterval paces the sweep between published + // status snapshots. + reviewQuestionAutodecideInterval = 2 * time.Minute + // reviewQuestionAutodecideMaxPerSweep bounds the paid decisions one + // sweep may start. + reviewQuestionAutodecideMaxPerSweep = 3 + // reviewQuestionAutodecideCountWindow is the /api/status counter's window + // (review_questions_autodecided_1h). + reviewQuestionAutodecideCountWindow = time.Hour + // reviewQuestionAutodecideRetention bounds the marker ledger. + reviewQuestionAutodecideRetention = 30 * 24 * time.Hour + // reviewQuestionFollowUpTitleMax caps the follow-up issue title. + reviewQuestionFollowUpTitleMax = 120 + // reviewQuestionFollowUpLabel marks the follow-up issues this sweep files. + reviewQuestionFollowUpLabel = "follow-up" +) + +// reviewQuestionAutodecidePath is the once-per-head marker ledger. +var reviewQuestionAutodecidePath = "/data/review-question-autodecide.json" + +// reviewQuestionAutodecideMarker records one auto-delegate on a head. +type reviewQuestionAutodecideMarker struct { + Head string `json:"head"` + At time.Time `json:"at"` + Class string `json:"class"` + Reason string `json:"reason"` + Verdict string `json:"verdict,omitempty"` + Rule string `json:"rule,omitempty"` + // FollowUp is the follow-up issue filed on an approve verdict (0: none). + FollowUp int `json:"follow_up,omitempty"` +} + +// reviewQuestionAutodecide is the sweep's pacing state and marker ledger. +type reviewQuestionAutodecide struct { + mu sync.Mutex + running bool + lastAt time.Time + path string + loaded bool + items map[string]reviewQuestionAutodecideMarker +} + +func (st *reviewQuestionAutodecide) filePath() string { + if st.path != "" { + return st.path + } + return reviewQuestionAutodecidePath +} + +// load reads the ledger once; an unreadable file starts empty. +func (st *reviewQuestionAutodecide) load() { + if st.loaded { + return + } + st.loaded = true + if st.items == nil { + st.items = map[string]reviewQuestionAutodecideMarker{} + } + data, err := os.ReadFile(st.filePath()) + if err != nil { + return + } + var items map[string]reviewQuestionAutodecideMarker + if json.Unmarshal(data, &items) == nil && items != nil { + st.items = items + } +} + +func (st *reviewQuestionAutodecide) persist() error { + data, err := json.MarshalIndent(st.items, "", " ") + if err != nil { + return err + } + path := st.filePath() + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + return err + } + tmp := path + ".tmp" + if err := os.WriteFile(tmp, data, 0o644); err != nil { + return err + } + return os.Rename(tmp, path) +} + +// seen reports whether repo#number already auto-delegated on head. +func (st *reviewQuestionAutodecide) seen(repo string, number int, head string) bool { + st.mu.Lock() + defer st.mu.Unlock() + st.load() + m, ok := st.items[operatorActionKey(repo, number)] + return ok && strings.EqualFold(m.Head, head) +} + +// mark records an auto-delegate on head, dropping markers past retention. +func (st *reviewQuestionAutodecide) mark(repo string, number int, m reviewQuestionAutodecideMarker) error { + st.mu.Lock() + defer st.mu.Unlock() + st.load() + for k, old := range st.items { + if m.At.Sub(old.At) > reviewQuestionAutodecideRetention { + delete(st.items, k) + } + } + st.items[operatorActionKey(repo, number)] = m + return st.persist() +} + +// update rewrites the marker for repo#number in place (verdict, follow-up). +func (st *reviewQuestionAutodecide) update(repo string, number int, fn func(*reviewQuestionAutodecideMarker)) error { + st.mu.Lock() + defer st.mu.Unlock() + st.load() + key := operatorActionKey(repo, number) + m, ok := st.items[key] + if !ok { + return nil + } + fn(&m) + st.items[key] = m + return st.persist() +} + +// countSince is the number of auto-delegates recorded within window of now. +func (st *reviewQuestionAutodecide) countSince(now time.Time, window time.Duration) int { + st.mu.Lock() + defer st.mu.Unlock() + st.load() + n := 0 + for _, m := range st.items { + if !m.At.IsZero() && now.Sub(m.At) <= window && !m.At.After(now) { + n++ + } + } + return n +} + +// reviewQuestionsAutodecided1h is the /api/status counter. +func (s *Server) reviewQuestionsAutodecided1h(now time.Time) int { + if s == nil { + return 0 + } + return s.reviewQuestionAuto.countSince(now, reviewQuestionAutodecideCountWindow) +} + +// reviewQuestionAutodecideEnabled is governor.review.auto_decide_questions +// (default on); unreadable config counts as on. +func (s *Server) reviewQuestionAutodecideEnabled() bool { + if s == nil || s.deps == nil || s.deps.Config == nil { + return config.GovernorReviewConfig{}.AutoDecideQuestionsEnabled() + } + return s.deps.Config.Governor.Review.AutoDecideQuestionsEnabled() +} + +// reviewQuestionCandidate is one 085 row a reviewer's question parked. +type reviewQuestionCandidate struct { + Repo string + Number int + Head string + Question string + Reason string + Caption string + // HiveAuthored is the row's own authorship read (hive-authored or + // adopted in place by the hive). + HiveAuthored bool +} + +// reviewQuestionCandidates picks the tracker rows the sweep may consider: a +// PR parked at 085 by a reviewer's question, on a known head, not on hold, +// CI neither red nor still running. Classification (fork, files, findings) +// happens per candidate afterwards. +func reviewQuestionCandidates(items []StageTrackerItem, qualify func(string) string) []reviewQuestionCandidate { + var out []reviewQuestionCandidate + for _, it := range items { + if it.Kind != "pr" || it.Stage != trackerNumberNeedsDecision || it.Head == "" { + continue + } + dn := it.DecisionNeeded + if dn == nil || dn.AskedBy != decisionAskedByReviewer || strings.TrimSpace(dn.Question) == "" { + continue + } + if it.Hold != nil || it.OnHold || it.ciRed || it.ciChecking { + continue + } + repo := it.Repo + if qualify != nil { + repo = qualify(repo) + } + if !strings.Contains(repo, "/") { + continue + } + out = append(out, reviewQuestionCandidate{ + Repo: repo, Number: it.Number, Head: it.Head, Question: strings.TrimSpace(dn.Question), + Reason: it.Reason, Caption: it.Caption, + HiveAuthored: it.AuthorKind == authorKindHive || it.AdoptedBy > 0, + }) + } + sort.Slice(out, func(i, j int) bool { + if out[i].Repo != out[j].Repo { + return out[i].Repo < out[j].Repo + } + return out[i].Number < out[j].Number + }) + return out +} + +// reviewQuestionUnresolvedFindings reports whether the head's aggregate +// still carries something a person or the fixer must answer: a backed +// decision finding, an in-scope finding, or a perspective that asked for +// changes. A bare question with none of these is what this sweep decides. +func reviewQuestionUnresolvedFindings(agg review.Aggregate) bool { + if strings.TrimSpace(agg.DecisionFinding) != "" { + return true + } + for _, f := range agg.Findings { + if review.InScopeFinding(f.Finding) { + return true + } + } + for _, v := range agg.Perspectives { + if v == review.VerdictChangesRequested || v == review.VerdictReject { + return true + } + } + return false +} + +// maybeAutoDecideReviewQuestions starts a sweep in the background when one +// is due and none is running; it reports whether it started one. +// UpdateStatusIfFresh calls it on every published snapshot. +func (s *Server) maybeAutoDecideReviewQuestions(status *StatusPayload, now time.Time) bool { + if s == nil || s.deps == nil || status == nil || !s.reviewQuestionAutodecideEnabled() { + return false + } + if s.deps.GHClient == nil || s.deps.GHClient.GoGitHub() == nil { + return false + } + st := &s.reviewQuestionAuto + st.mu.Lock() + if st.running || (!st.lastAt.IsZero() && now.Sub(st.lastAt) < reviewQuestionAutodecideInterval) { + st.mu.Unlock() + return false + } + st.running = true + st.lastAt = now + st.mu.Unlock() + ctx := context.Background() + if s.deps.Ctx != nil { + ctx = s.deps.Ctx + } + go func() { + defer func() { + st.mu.Lock() + st.running = false + st.mu.Unlock() + }() + s.autoDecideReviewQuestions(ctx, status, now) + }() + return true +} + +// autoDecideReviewQuestions runs one sweep synchronously. +func (s *Server) autoDecideReviewQuestions(ctx context.Context, status *StatusPayload, now time.Time) int { + items := s.buildStageTracker(status, s.stageStoreOrShared(), now).Items + cands := reviewQuestionCandidates(items, stageTrackerRepoQualifier(status)) + if len(cands) == 0 { + return 0 + } + var lookup func(string, int, string, bool) (review.Aggregate, bool) + if art, err := review.LoadArtifact(""); err == nil { + lookup = stageTrackerDecisionVerdictLookup(&art) + } + started := 0 + for _, c := range cands { + if started >= reviewQuestionAutodecideMaxPerSweep || ctx.Err() != nil { + break + } + if !s.delegateDecisionEnabled(c.Repo) || s.reviewQuestionAuto.seen(c.Repo, c.Number, c.Head) { + continue + } + if s.autoDecideReviewQuestion(ctx, c, lookup, now) { + started++ + } + } + return started +} + +// autoDecideReviewQuestion classifies one candidate and, for a scope +// question, runs the delegated decision. It reports whether one ran. +func (s *Server) autoDecideReviewQuestion(ctx context.Context, c reviewQuestionCandidate, lookup func(string, int, string, bool) (review.Aggregate, bool), now time.Time) bool { + in := review.DecisionQuestionInput{Question: c.Question, HiveAuthored: c.HiveAuthored, CIGreen: true} + if lookup != nil { + if agg, ok := lookup(c.Repo, c.Number, c.Head, true); ok { + in.UnresolvedFindings = reviewQuestionUnresolvedFindings(agg) + if agg.HiveAuthoredKnown { + in.HiveAuthored = in.HiveAuthored || agg.HiveAuthored + } + } + } + fromFork, files, err := s.reviewQuestionPRFacts(ctx, c.Repo, c.Number) + if err != nil { + s.reviewQuestionLogger().Debug("reviewer question auto-decide skipped: PR read failed", "repo", c.Repo, "pr", c.Number, "error", err) + return false + } + in.FromFork, in.Files = fromFork, files + class, reason := review.ClassifyDecisionQuestion(in) + if class != review.DecisionQuestionScope { + s.reviewQuestionLogger().Debug("reviewer question kept for a person", "repo", c.Repo, "pr", c.Number, "head", c.Head, "class", string(class), "reason", reason) + return false + } + hiveID := s.delegateHiveID() + marker := reviewQuestionAutodecideMarker{Head: c.Head, At: now, Class: string(class), Reason: reason} + if err := s.reviewQuestionAuto.mark(c.Repo, c.Number, marker); err != nil { + s.reviewQuestionLogger().Warn("reviewer question auto-decide ledger write failed", "path", s.reviewQuestionAuto.filePath(), "error", err) + } + key := delegatedJobKey(c.Repo, c.Number) + job := &delegatedDecisionJob{ + ID: key + "@" + strconv.FormatInt(now.UnixNano(), 36), Repo: c.Repo, Number: c.Number, Type: "pr", + Status: delegatedJobStatusRunning, Requester: hiveID, StartedAt: now, Marker: delegatedecision.Marker(hiveID), + } + if _, ok := s.delegatedJobs.start(key, job, now); !ok { + return false + } + s.delegatedJobs.plans.forget(c.Repo, c.Number) + if err := s.operatorActions().Record(c.Repo, c.Number, OperatorAction{ + Action: operatorActionLetHiveDecide, Actor: hiveID, At: now, Outcome: operatorActionOutcomePending, + HeadSHA: c.Head, Stage: trackerNumberNeedsDecision, Note: "auto: " + reason, + }); err != nil { + s.reviewQuestionLogger().Warn("operator action ledger write failed", "path", operatorActionsPath, "error", err) + } + s.runDelegatedDecision(ctx, key, c.Repo, c.Number, "pr", hiveID, false, delegatedDecisionRequest{ + Type: "pr", Reason: c.Reason, Caption: c.Caption, Head: c.Head, + }) + done, _ := s.delegatedJobs.get(key) + verdict, rule := "", "" + if done.Decision != nil { + verdict, rule = string(done.Decision.Verdict), done.Decision.Rule + } + followUp := 0 + if done.Decision != nil && done.Decision.Verdict == delegatedecision.VerdictApprove { + followUp = s.fileReviewQuestionFollowUp(ctx, c) + } + _ = s.reviewQuestionAuto.update(c.Repo, c.Number, func(m *reviewQuestionAutodecideMarker) { + m.Verdict, m.Rule, m.FollowUp = verdict, rule, followUp + }) + s.reviewQuestionLogger().Info("reviewer question auto-decided", + "repo", c.Repo, "pr", c.Number, "head", c.Head, "class", string(class), "reason", reason, + "verdict", verdict, "rule", rule, "status", done.Status, "error", done.Error, "follow_up", followUp) + s.invalidateStageTrackerCache() + return true +} + +// reviewQuestionPRFacts reads what the classifier needs from GitHub: whether +// the head lives on a fork, and the changed files with their patches. +func (s *Server) reviewQuestionPRFacts(ctx context.Context, repoFull string, number int) (bool, []review.DecisionQuestionFile, error) { + client := s.deps.GHClient.GoGitHub() + owner, name, ok := strings.Cut(repoFull, "/") + if !ok || client == nil { + return false, nil, errors.New("no GitHub client") + } + pr, _, err := client.PullRequests.Get(ctx, owner, name, number) + if err != nil { + return false, nil, err + } + fromFork := false + if pr.GetHead().GetRepo() != nil && pr.GetBase().GetRepo() != nil { + fromFork = !strings.EqualFold(pr.GetHead().GetRepo().GetFullName(), pr.GetBase().GetRepo().GetFullName()) + } + var files []review.DecisionQuestionFile + opt := &gh.ListOptions{PerPage: 100} + for { + page, resp, err := client.PullRequests.ListFiles(ctx, owner, name, number, opt) + if err != nil { + return fromFork, nil, err + } + for _, f := range page { + files = append(files, review.DecisionQuestionFile{Path: f.GetFilename(), Patch: f.GetPatch()}) + if prev := f.GetPreviousFilename(); prev != "" && prev != f.GetFilename() { + files = append(files, review.DecisionQuestionFile{Path: prev}) + } + } + if resp == nil || resp.NextPage == 0 { + break + } + opt.Page = resp.NextPage + } + return fromFork, files, nil +} + +// fileReviewQuestionFollowUp records the reviewer's question as a scanner +// issue linked to the PR once the hive admitted the PR as-is. It returns the +// issue number (0 when the gate refused or the create failed). +func (s *Server) fileReviewQuestionFollowUp(ctx context.Context, c reviewQuestionCandidate) int { + if s.deps == nil || s.deps.GHClient == nil { + return 0 + } + title := reviewQuestionFollowUpTitle(c.Question) + preview := "follow-up issue for reviewer question on " + c.Repo + "#" + strconv.Itoa(c.Number) + if !s.deps.GHClient.WritebackAllowed(c.Repo, writeback.Item{HiveAuthored: true}, writeback.KindIssueFile, preview).Allowed { + return 0 + } + body := fmt.Sprintf("A review perspective on #%d ended with a question rather than a finding. "+ + "The hive decided the PR merges as-is (governor.review.auto_decide_questions); the question is kept here as follow-up work.\n\n"+ + "> %s\n\nSource: %s#%d (head `%s`).\n", c.Number, strings.ReplaceAll(c.Question, "\n", "\n> "), c.Repo, c.Number, c.Head) + res, err := s.deps.GHClient.CreateIssue(ctx, ghpkg.CreateIssueRequest{ // writeback:gated + Repo: c.Repo, + Title: title, + Body: body, + Labels: []string{reviewQuestionFollowUpLabel}, + Origin: config.OriginSourceGovernor, + }) + if err != nil { + s.reviewQuestionLogger().Warn("reviewer question follow-up issue not filed", "repo", c.Repo, "pr", c.Number, "error", err) + return 0 + } + return res.Number +} + +// reviewQuestionFollowUpTitle is "follow-up: " on one line, +// capped at reviewQuestionFollowUpTitleMax runes. +func reviewQuestionFollowUpTitle(question string) string { + q := strings.Join(strings.Fields(question), " ") + title := "follow-up: " + q + if r := []rune(title); len(r) > reviewQuestionFollowUpTitleMax { + title = strings.TrimSpace(string(r[:reviewQuestionFollowUpTitleMax-1])) + "…" + } + return title +} + +func (s *Server) reviewQuestionLogger() *slog.Logger { return s.webhookLogger() } diff --git a/src/pkg/dashboard/review_question_autodecide_test.go b/src/pkg/dashboard/review_question_autodecide_test.go new file mode 100644 index 0000000000..ccfc0a9c17 --- /dev/null +++ b/src/pkg/dashboard/review_question_autodecide_test.go @@ -0,0 +1,107 @@ +package dashboard + +import ( + "path/filepath" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/outputschema" + "github.com/hivecommons/hive/pkg/review" +) + +func TestReviewQuestionCandidatesPicksReviewerQuestionsAt085(t *testing.T) { + ask := &StageTrackerDecision{Question: "@clubanderson should decide whether this merges ahead of the workflow edit.", AskedBy: decisionAskedByReviewer} + items := []StageTrackerItem{ + {Kind: "pr", Repo: "docs", Number: 618, Head: "abc", Stage: trackerNumberNeedsDecision, AuthorKind: authorKindHive, DecisionNeeded: ask}, + {Kind: "pr", Repo: "hivecommons/hive", Number: 2, Head: "def", Stage: trackerNumberNeedsDecision, AuthorKind: authorKindHive, DecisionNeeded: ask, ciRed: true}, + {Kind: "pr", Repo: "hivecommons/hive", Number: 3, Head: "ghi", Stage: trackerNumberNeedsDecision, AuthorKind: authorKindHive, DecisionNeeded: ask, ciChecking: true}, + {Kind: "pr", Repo: "hivecommons/hive", Number: 4, Head: "jkl", Stage: trackerNumberNeedsDecision, AuthorKind: authorKindHive, DecisionNeeded: ask, OnHold: true}, + {Kind: "pr", Repo: "hivecommons/hive", Number: 5, Head: "mno", Stage: trackerNumberNeedsDecision, AuthorKind: authorKindHive, DecisionNeeded: &StageTrackerDecision{Question: "sentinel flagged", AskedBy: "sentinel"}}, + {Kind: "pr", Repo: "hivecommons/hive", Number: 6, Head: "", Stage: trackerNumberNeedsDecision, AuthorKind: authorKindHive, DecisionNeeded: ask}, + {Kind: "pr", Repo: "hivecommons/hive", Number: 7, Head: "pqr", Stage: 70, AuthorKind: authorKindHive, DecisionNeeded: ask}, + {Kind: "issue", Repo: "hivecommons/hive", Number: 8, Head: "stu", Stage: trackerNumberNeedsDecision, DecisionNeeded: ask}, + {Kind: "pr", Repo: "hivecommons/hive", Number: 9, Head: "vwx", Stage: trackerNumberNeedsDecision, AuthorKind: "person", AdoptedBy: 10, DecisionNeeded: ask}, + } + qualify := func(r string) string { + if r == "docs" { + return "hivecommons/docs" + } + return r + } + got := reviewQuestionCandidates(items, qualify) + if len(got) != 2 { + t.Fatalf("candidates = %+v, want docs#618 and hive#9", got) + } + if got[0].Repo != "hivecommons/docs" || got[0].Number != 618 || !got[0].HiveAuthored { + t.Errorf("first = %+v, want qualified hive-authored docs#618", got[0]) + } + if got[1].Number != 9 || !got[1].HiveAuthored { + t.Errorf("second = %+v, want adopted hive#9 read as hive-authored", got[1]) + } +} + +func TestReviewQuestionUnresolvedFindings(t *testing.T) { + clean := review.Aggregate{DecisionQuestion: "should this merge ahead?", Perspectives: map[review.Perspective]review.Verdict{"security": review.VerdictApprove}} + if reviewQuestionUnresolvedFindings(clean) { + t.Errorf("a bare question with approving perspectives is not an unresolved finding") + } + backed := clean + backed.DecisionFinding = "security: token in workflow" + if !reviewQuestionUnresolvedFindings(backed) { + t.Errorf("a backed decision finding is unresolved") + } + changes := review.Aggregate{Perspectives: map[review.Perspective]review.Verdict{"correctness": review.VerdictChangesRequested}} + if !reviewQuestionUnresolvedFindings(changes) { + t.Errorf("changes_requested is unresolved") + } + finding := review.Aggregate{Findings: []review.PerspectiveFinding{{Perspective: "correctness", Finding: outputschema.Finding{Title: "nil deref", Severity: outputschema.SeverityHigh}}}} + if !reviewQuestionUnresolvedFindings(finding) { + t.Errorf("an in-scope finding is unresolved") + } +} + +func TestReviewQuestionAutodecideOncePerHead(t *testing.T) { + st := &reviewQuestionAutodecide{path: filepath.Join(t.TempDir(), "markers.json")} + now := time.Date(2026, 10, 21, 12, 0, 0, 0, time.UTC) + if st.seen("hivecommons/docs", 618, "abc") { + t.Fatal("fresh ledger reports seen") + } + if err := st.mark("hivecommons/docs", 618, reviewQuestionAutodecideMarker{Head: "abc", At: now, Class: "scope-question", Reason: "r"}); err != nil { + t.Fatal(err) + } + if !st.seen("hivecommons/docs", 618, "ABC") { + t.Error("same head (case-insensitive) must read as seen") + } + if st.seen("hivecommons/docs", 618, "def") { + t.Error("a new push must ask again") + } + reloaded := &reviewQuestionAutodecide{path: st.path} + if !reloaded.seen("HiveCommons/Docs", 618, "abc") { + t.Error("marker must survive a reload") + } + if got := reloaded.countSince(now.Add(30*time.Minute), time.Hour); got != 1 { + t.Errorf("count within window = %d, want 1", got) + } + if got := reloaded.countSince(now.Add(2*time.Hour), time.Hour); got != 0 { + t.Errorf("count past window = %d, want 0", got) + } +} + +func TestReviewQuestionFollowUpTitle(t *testing.T) { + got := reviewQuestionFollowUpTitle(" should we\n also apply the opt-in? ") + if got != "follow-up: should we also apply the opt-in?" { + t.Errorf("title = %q", got) + } + long := reviewQuestionFollowUpTitle(repeatRune('q', 300)) + if r := []rune(long); len(r) != reviewQuestionFollowUpTitleMax || r[len(r)-1] != '…' { + t.Errorf("long title not capped: %d runes", len(r)) + } +} + +func repeatRune(r rune, n int) string { + out := make([]rune, n) + for i := range out { + out[i] = r + } + return string(out) +} diff --git a/src/pkg/dashboard/server.go b/src/pkg/dashboard/server.go index cb331ae8cc..b6a84cb3e8 100644 --- a/src/pkg/dashboard/server.go +++ b/src/pkg/dashboard/server.go @@ -237,6 +237,9 @@ type Server struct { // delegatedJobs tracks "Let hive decide" jobs by repo#number // (api_repo_item_decide_delegated.go). delegatedJobs delegatedDecisionJobs + // reviewQuestionAuto is the reviewer-question auto-decide sweep's + // pacing and once-per-head ledger (review_question_autodecide.go). + reviewQuestionAuto reviewQuestionAutodecide // operatorActionStore is the owner row-action ledger behind the // activity monitor's ✓ badge (operator_actions.go), opened on first use. operatorActionStore *operatorActionStore @@ -2831,6 +2834,9 @@ func (s *Server) UpdateStatusIfFresh(status *StatusPayload, buildEpoch uint64) b // Backstop for closed items whose delivery never arrived or whose // confirming read was refused (hivecommons/hive#14159, #14064). s.maybeSweepClosedRows(time.Now().UTC()) + // A reviewer's scope question on the hive's own green PR is the hive's + // to decide, not an 085 park (review_question_autodecide.go). + s.maybeAutoDecideReviewQuestions(status, time.Now().UTC()) s.AppendTokenSparkline(status) s.AppendTrendHistory(status) @@ -3739,7 +3745,7 @@ func (s *Server) handleStatus(w http.ResponseWriter, r *http.Request) { return } now := time.Now().UTC() - payload := statusWithWebhooks{StatusPayload: s.statusWithOverviewBands(status, now), Webhooks: github.WebhookHealthSnapshot(), CIFailureSignals: github.CIFailureSignals().Snapshot(), MatrixEnabled: s.currentMatrixConfig().Active()} + payload := statusWithWebhooks{StatusPayload: s.statusWithOverviewBands(status, now), Webhooks: github.WebhookHealthSnapshot(), CIFailureSignals: github.CIFailureSignals().Snapshot(), MatrixEnabled: s.currentMatrixConfig().Active(), ReviewQuestionsAutodecided1h: s.reviewQuestionsAutodecided1h(now)} if s.deps != nil && s.deps.EvalLoop != nil { loop := s.deps.EvalLoop() payload.EvalLoopStatus = &loop @@ -3800,6 +3806,10 @@ type statusWithWebhooks struct { // config.gate_regressions is the safety gates a previous runtime // overlay carried that this boot dropped (#16438). Config *ConfigHealthStatus `json:"config,omitempty"` + // ReviewQuestionsAutodecided1h counts the reviewer questions the hive + // decided itself in the last hour instead of parking the PR at 085 + // (review_question_autodecide.go, governor.review.auto_decide_questions). + ReviewQuestionsAutodecided1h int `json:"review_questions_autodecided_1h"` } func (s *Server) handleStatusSummary(w http.ResponseWriter, r *http.Request) { diff --git a/src/pkg/review/decision_question_class.go b/src/pkg/review/decision_question_class.go new file mode 100644 index 0000000000..3abfd5c968 --- /dev/null +++ b/src/pkg/review/decision_question_class.go @@ -0,0 +1,168 @@ +package review + +import ( + "path" + "regexp" + "strconv" + "strings" + + "github.com/hivecommons/hive/pkg/intent" +) + +// DecisionQuestionClass sorts one reviewer "decision needed" ask on a +// hive-authored PR into what it needs: a person's sign-off, or the hive's own +// delegated decision. hivecommons/docs#618 parked a green, finding-free, +// hive-authored PR at stage/085 because one perspective ended with a +// scope question ("should this merge ahead of that workflow edit?") that the +// risk-class denylist (DecisionRiskClasses) read as a sensitive-path ask — +// the owner's words: "this is auto-merge material, not needs-human". +type DecisionQuestionClass string + +const ( + // DecisionQuestionPolicySignOff: the ask stays a person's. The PR edits + // workflows from a fork, touches secrets / permissions / OWNERS / + // security-labelled paths, changes the auto-merge or human-merge + // policy, or the reviewer names an owner-only guard outright. + DecisionQuestionPolicySignOff DecisionQuestionClass = "policy-sign-off" + // DecisionQuestionScope: a scope or follow-up question ("should we + // also…", "merge ahead of…", "apply X from the PR body", "confirm Y is + // acceptable") on a hive-authored PR with CI green and no unresolved + // finding on its head. The hive answers it through the delegate path. + DecisionQuestionScope DecisionQuestionClass = "scope-question" + // DecisionQuestionIneligible: not a candidate for auto-decision at all + // (no question, not hive-authored, CI not green, findings open). The + // ordinary 085 park stands. + DecisionQuestionIneligible DecisionQuestionClass = "ineligible" +) + +// DecisionQuestionInput is what the classifier sees. It is a value, not a +// GitHub read: callers gather the head's files and status first. +type DecisionQuestionInput struct { + // Question is the reviewer's decision question, verbatim. + Question string + // HiveAuthored: the hive opened the PR, or adopted it in place. + HiveAuthored bool + // FromFork: the head lives in a fork of the base repository. + FromFork bool + // CIGreen: every gating check on the head passed. + CIGreen bool + // UnresolvedFindings: the head's review verdict carries a finding or a + // changes-requested perspective that no fix has cleared. + UnresolvedFindings bool + // Files are the head's changed files with their patches (patch may be + // empty when the caller did not fetch it). + Files []DecisionQuestionFile +} + +// DecisionQuestionFile is one changed file of the PR under question. +type DecisionQuestionFile struct { + Path string + Patch string +} + +// decisionPolicyPaths are the paths whose edit turns a reviewer question +// into a sign-off: trust and governance files, security policy, workflow +// and ruleset config, the hive's own policy files, and secrets-adjacent +// files. Deliberately narrower than sentinel.DefaultSensitivePaths, whose +// go.mod / Makefile / package.json entries guard supply chain, not policy. +var decisionPolicyPaths = []string{ + "OWNERS", "**/OWNERS", "OWNERS_ALIASES", "**/OWNERS_ALIASES", + "CODEOWNERS", "**/CODEOWNERS", "MAINTAINERS", "**/MAINTAINERS", + "GOVERNANCE.md", "SECURITY.md", "**/SECURITY.md", + ".github/rulesets/**", ".github/settings.yml", ".github/CODEOWNERS", + "policies/**", "**/policies/**", + "hive.yaml", "**/hive.yaml", "hive.yaml.dashboard", "**/hive.yaml.dashboard", + "**/.env*", "**/*.pem", "**/*.key", "**/id_rsa*", "**/secrets*", +} + +// decisionWorkflowPaths are the CI-automation paths a fork may not edit +// without a person's sign-off (the App cannot push them either). +var decisionWorkflowPaths = []string{".github/workflows/**", "**/.github/workflows/**", ".github/actions/**"} + +// decisionPolicyConfigKeys are hive config keys whose change in a patch is +// a merge-policy change, whatever file carries them. +var decisionPolicyConfigKeys = []string{"auto_merge", "human_merge_paths", "self_authored_decision_policy", "auto_decide_questions"} + +// decisionOwnerOnlyGuards are the asks a reviewer phrases as an explicit +// owner-only guard. Each is a whole-word match on the lower-cased question. +var decisionOwnerOnlyGuards = []*regexp.Regexp{ + regexp.MustCompile(`\bsentinel\b`), + regexp.MustCompile(`\bgithub permissions?\b`), + regexp.MustCompile(`\bapp permissions?\b`), + regexp.MustCompile(`\borg(?:anization)? (?:permissions?|admin|owner|membership)\b`), + regexp.MustCompile(`\bunauthori[sz]ed\b`), + regexp.MustCompile(`\bsecrets?\b`), + regexp.MustCompile(`\btokens?\b`), + regexp.MustCompile(`\bcredentials?\b`), + regexp.MustCompile(`\bbranch protection\b`), + regexp.MustCompile(`\bcodeowners\b`), + regexp.MustCompile(`\bowners file\b`), + regexp.MustCompile(`\bowner-only\b`), +} + +// ClassifyDecisionQuestion is the pure classifier behind the reviewer +// question auto-decide (governor.review.auto_decide_questions). The reason +// names the first rule that settled the class, for the INFO line and the +// audit record. Policy sign-off wins over scope: a question on a PR that +// edits OWNERS is the owner's however it is phrased. +func ClassifyDecisionQuestion(in DecisionQuestionInput) (DecisionQuestionClass, string) { + q := strings.ToLower(strings.TrimSpace(in.Question)) + if q == "" { + return DecisionQuestionIneligible, "no reviewer question" + } + for _, f := range in.Files { + p := strings.TrimSpace(f.Path) + if p == "" { + continue + } + if in.FromFork && intent.PathMatchesAny(p, decisionWorkflowPaths) { + return DecisionQuestionPolicySignOff, "fork PR edits workflow path " + p + } + if intent.PathMatchesAny(p, decisionPolicyPaths) { + return DecisionQuestionPolicySignOff, "PR touches policy path " + p + } + if key := decisionPatchPolicyKey(p, f.Patch); key != "" { + return DecisionQuestionPolicySignOff, "PR changes " + key + " in " + p + } + } + for _, guard := range decisionOwnerOnlyGuards { + if m := guard.FindString(q); m != "" { + return DecisionQuestionPolicySignOff, "ask names owner-only guard " + strconv.Quote(m) + } + } + switch { + case !in.HiveAuthored: + return DecisionQuestionIneligible, "PR is not hive-authored" + case !in.CIGreen: + return DecisionQuestionIneligible, "CI is not green on head" + case in.UnresolvedFindings: + return DecisionQuestionIneligible, "head has unresolved review findings" + } + return DecisionQuestionScope, "scope/follow-up question on a green hive-authored PR with no findings" +} + +// decisionPatchPolicyKey reports the merge-policy config key a patch adds +// or removes in a hive config file; "" when none. Only YAML-shaped files are +// read, so a Go test naming `auto_merge` in a string is not a policy edit. +func decisionPatchPolicyKey(p, patch string) string { + if patch == "" { + return "" + } + switch strings.ToLower(path.Ext(p)) { + case ".yaml", ".yml", ".dashboard": + default: + return "" + } + for _, line := range strings.Split(patch, "\n") { + if len(line) < 2 || (line[0] != '+' && line[0] != '-') || strings.HasPrefix(line, "+++") || strings.HasPrefix(line, "---") { + continue + } + body := strings.TrimSpace(line[1:]) + for _, key := range decisionPolicyConfigKeys { + if strings.HasPrefix(body, key+":") { + return key + } + } + } + return "" +} diff --git a/src/pkg/review/decision_question_class_test.go b/src/pkg/review/decision_question_class_test.go new file mode 100644 index 0000000000..aa88efd1da --- /dev/null +++ b/src/pkg/review/decision_question_class_test.go @@ -0,0 +1,134 @@ +package review + +import ( + "strings" + "testing" +) + +// The two asks that parked green, finding-free, hive-authored PRs for a +// person (hivecommons/docs#618 and hivecommons/hive#16456), verbatim. +const ( + docs618Ask = "@clubanderson should decide whether to apply the hive-docs-refresh.yml opt-in from the PR body (adding the \"hive-docs-refresh\" workflow_dispatch input) and whether this PR should merge ahead of that workflow edit." + hive16456Ask = "Decision needed: @clubanderson should confirm the merge-request relay may lift the hold…" +) + +func TestClassifyDecisionQuestion(t *testing.T) { + green := func(q string, files ...DecisionQuestionFile) DecisionQuestionInput { + return DecisionQuestionInput{Question: q, HiveAuthored: true, CIGreen: true, Files: files} + } + cases := []struct { + name string + in DecisionQuestionInput + want DecisionQuestionClass + reason string + }{ + { + name: "docs#618 scope question is the hive's", + in: green(docs618Ask, DecisionQuestionFile{Path: "docs/sync/README.md"}, DecisionQuestionFile{Path: "scripts/sync.sh"}), + want: DecisionQuestionScope, reason: "scope/follow-up", + }, + { + name: "hive#16456 confirm-the-relay question is the hive's", + in: green(hive16456Ask, DecisionQuestionFile{Path: "src/pkg/github/review_verdict_relay.go"}), + want: DecisionQuestionScope, reason: "scope/follow-up", + }, + { + name: "should-we-also follow-up", + in: green("Should we also update the operator reference to mention the new flag, or is a follow-up fine?"), + want: DecisionQuestionScope, + }, + { + name: "confirm acceptable wording", + in: green("@clubanderson should confirm the renamed caption is acceptable before this merges."), + want: DecisionQuestionScope, + }, + { + name: "fork PR editing a workflow is a sign-off", + in: DecisionQuestionInput{Question: "Should the new job run on pull_request_target?", HiveAuthored: true, CIGreen: true, FromFork: true, + Files: []DecisionQuestionFile{{Path: ".github/workflows/ci.yml"}}}, + want: DecisionQuestionPolicySignOff, reason: "fork PR edits workflow path", + }, + { + name: "same-repo hive PR editing a workflow is still the hive's", + in: green("Should this merge ahead of the docs refresh?", DecisionQuestionFile{Path: ".github/workflows/docs.yml"}), + want: DecisionQuestionScope, + }, + { + name: "OWNERS edit is a sign-off however it is phrased", + in: green("Should we also add the new reviewer to the docs alias?", DecisionQuestionFile{Path: "docs/OWNERS"}), + want: DecisionQuestionPolicySignOff, reason: "policy path docs/OWNERS", + }, + { + name: "SECURITY.md edit is a sign-off", + in: green("Confirm the new disclosure address is right.", DecisionQuestionFile{Path: "SECURITY.md"}), + want: DecisionQuestionPolicySignOff, reason: "policy path", + }, + { + name: "auto_merge config change in a patch is a sign-off", + in: green("Confirm the lane should merge its own PRs.", DecisionQuestionFile{Path: "deploy/overlays/prod/governor.yaml", + Patch: "@@ -1,3 +1,4 @@\n repos:\n hive:\n+ auto_merge: true\n"}), + want: DecisionQuestionPolicySignOff, reason: "changes auto_merge", + }, + { + name: "auto_merge named in a Go test is not a policy edit", + in: green("Should the test also cover the off case?", DecisionQuestionFile{Path: "src/pkg/config/config_test.go", + Patch: "+\tcfg.AutoMerge.Enabled = true // auto_merge: on\n"}), + want: DecisionQuestionScope, + }, + { + name: "explicit sentinel guard is a sign-off", + in: green("The sentinel flagged the install script; @clubanderson should acknowledge it before merge."), + want: DecisionQuestionPolicySignOff, reason: `owner-only guard "sentinel"`, + }, + { + name: "GitHub permissions guard is a sign-off", + in: green("Whether the App's GitHub permissions allow pushing this is the owner's call."), + want: DecisionQuestionPolicySignOff, reason: "github permissions", + }, + { + name: "secret token guard is a sign-off", + in: green("Confirm the new token scope is intended."), + want: DecisionQuestionPolicySignOff, reason: "token", + }, + { + name: "contributor PR is not a candidate", + in: DecisionQuestionInput{Question: "Should this also handle the empty case?", CIGreen: true}, + want: DecisionQuestionIneligible, reason: "not hive-authored", + }, + { + name: "red CI is not a candidate", + in: DecisionQuestionInput{Question: "Should this also handle the empty case?", HiveAuthored: true}, + want: DecisionQuestionIneligible, reason: "CI is not green", + }, + { + name: "open findings are not a candidate", + in: DecisionQuestionInput{Question: "Should this also handle the empty case?", HiveAuthored: true, CIGreen: true, UnresolvedFindings: true}, + want: DecisionQuestionIneligible, reason: "unresolved review findings", + }, + { + name: "no question", + in: DecisionQuestionInput{HiveAuthored: true, CIGreen: true}, + want: DecisionQuestionIneligible, reason: "no reviewer question", + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + got, reason := ClassifyDecisionQuestion(tc.in) + if got != tc.want { + t.Fatalf("class = %q (%s), want %q", got, reason, tc.want) + } + if tc.reason != "" && !strings.Contains(strings.ToLower(reason), strings.ToLower(tc.reason)) { + t.Fatalf("reason = %q, want it to mention %q", reason, tc.reason) + } + }) + } +} + +// The two real asks still land in a DecisionRiskClass by wording alone +// (that is why they parked); the classifier is what lets the hive answer +// them regardless. +func TestClassifyDecisionQuestionRealAsksMatchRiskClassByWording(t *testing.T) { + if _, risk := DecisionQuestionRiskClass(docs618Ask); !risk { + t.Fatalf("docs#618 ask no longer matches a risk class; the classifier is not what routes it") + } +}