From d60f35f6138737217675d5f70d284330c9001ee0 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Tue, 27 Oct 2026 13:38:57 -0400 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20review:=20route=20bot-authored?= =?UTF-8?q?=20PR=20reviews=20to=20a=20cheaper=20model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PRs opened by Dependabot, Renovate, github-actions and other GitHub Apps are reviewed with review.bot_author_model when set, through the same per-kick model choice the review_models pool uses. The hive's own App login never matches review.bot_authors; a PR touching review.bot_author_full_review_paths (default CI workflows and Dockerfiles) or whose changed files could not be listed keeps the full-strength model. Fixes #14625 Signed-off-by: Andrew Anderson --- .../added-14625-review-bot-author-model.md | 1 + src/cmd/hive/main.go | 63 +++++- src/cmd/hive/review_bot_route_wiring_test.go | 73 +++++++ src/docs/review-swarm.md | 13 ++ src/pkg/config/config.go | 14 ++ src/pkg/config/review_dispatch.go | 52 +++++ src/pkg/config/review_dispatch_test.go | 24 +++ src/pkg/github/review_bot_route.go | 30 +++ src/pkg/review/bot_route.go | 155 ++++++++++++++ src/pkg/review/bot_route_test.go | 193 ++++++++++++++++++ src/pkg/review/dispatch.go | 13 +- src/pkg/review/prompts.go | 19 +- 12 files changed, 638 insertions(+), 12 deletions(-) create mode 100644 changelog.d/added-14625-review-bot-author-model.md create mode 100644 src/cmd/hive/review_bot_route_wiring_test.go create mode 100644 src/pkg/github/review_bot_route.go create mode 100644 src/pkg/review/bot_route.go create mode 100644 src/pkg/review/bot_route_test.go diff --git a/changelog.d/added-14625-review-bot-author-model.md b/changelog.d/added-14625-review-bot-author-model.md new file mode 100644 index 0000000000..a8a61bda6d --- /dev/null +++ b/changelog.d/added-14625-review-bot-author-model.md @@ -0,0 +1 @@ +- `review.bot_author_model` (default empty = off), `review.bot_authors` (default `dependabot[bot]`, `renovate[bot]`, `github-actions[bot]`, `*[bot]`; the hive's own App never matches) and `review.bot_author_full_review_paths` (default `.github/workflows/**`, `Dockerfile*`): reviews of PRs opened by other bots run on a configured cheaper model unless the PR touches a full-review path or its files could not be listed ([#14625](https://github.com/hivecommons/hive/issues/14625)). diff --git a/src/cmd/hive/main.go b/src/cmd/hive/main.go index 47f1210d50..00a2e889d0 100644 --- a/src/cmd/hive/main.go +++ b/src/cmd/hive/main.go @@ -7443,7 +7443,7 @@ func runEvalCycle( dueBeforeDeferral := append([]string(nil), agentsDue...) agentsDue = sched.FilterFirstScanDeferredAgents(agentsDue) agentsSkipped = append(agentsSkipped, firstScanDeferredSkips(dueBeforeDeferral, agentsDue)...) - reviewPlan := planReviewDispatch(cfg, actionable, agentMgr, beadStores, logger) + reviewPlan := planReviewDispatchWith(ctx, cfg, actionable, agentMgr, beadStores, reviewChangedFilesListerFor(ghClient), logger) auditWithheldReviewFixes(reviewPlan, dashSrv, logger) if ghClient != nil { postReviewAcknowledgements(ctx, ghClient, reviewPlan, logger) @@ -10561,10 +10561,46 @@ func parseReviseCutoff(raw string, logger *slog.Logger) time.Time { return cutoff } +// reviewChangedFilesLister is the slice of the GitHub client the bot-author +// model route needs (review.bot_author_full_review_paths): a PR's changed +// paths. +type reviewChangedFilesLister interface { + ReviewPRChangedFiles(ctx context.Context, repo string, number int) ([]string, bool) +} + func planReviewDispatch(cfg *config.Config, actionable *github.ActionableResult, agentMgr crux.Runtime, beadStores map[string]*beads.Store, logger *slog.Logger) review.DispatchPlan { + return planReviewDispatchWith(context.Background(), cfg, actionable, agentMgr, beadStores, nil, logger) +} + +// reviewBotAuthorRoute is review.bot_authors / bot_author_model / +// bot_author_full_review_paths resolved for the planner. +func reviewBotAuthorRoute(cfg *config.Config) review.BotAuthorRoute { + backend, model := cfg.Review.BotAuthorModelRef() + return review.BotAuthorRoute{Authors: cfg.Review.EffectiveBotAuthors(), Backend: backend, Model: model, FullReviewPaths: cfg.Review.EffectiveBotAuthorFullReviewPaths()} +} + +// reviewBotRouteFilesFor fetches a PR's changed paths when the bot-author +// route may apply to it: route on, author a listed bot, no verdict at this +// head and no review in flight (one listing per head, not per cycle). nil, +// false otherwise, which the planner reads as unknown: no downgrade. +func reviewBotRouteFilesFor(ctx context.Context, cfg *config.Config, lister reviewChangedFilesLister, route review.BotAuthorRoute, artifact review.Artifact, state review.DispatchState, repo string, number int, author, headSHA, bodyDigest string) ([]string, bool) { + if lister == nil || !route.Enabled() || !review.IsBotAuthor(author, route.Authors, cfg.EffectiveAIAuthor()) { + return nil, false + } + if _, judged := artifact.AggregateForBody(repo, number, headSHA, bodyDigest); judged { + return nil, false + } + if !state.PendingSince(repo, number, headSHA).IsZero() { + return nil, false + } + return lister.ReviewPRChangedFiles(ctx, repo, number) +} + +func planReviewDispatchWith(ctx context.Context, cfg *config.Config, actionable *github.ActionableResult, agentMgr crux.Runtime, beadStores map[string]*beads.Store, lister reviewChangedFilesLister, logger *slog.Logger) review.DispatchPlan { if cfg == nil || actionable == nil || !cfg.Review.RequireApproval || !cfg.Review.FanOut { return review.DispatchPlan{} } + botRoute := reviewBotAuthorRoute(cfg) state, err := review.LoadDispatchState("") if err != nil && !os.IsNotExist(err) { logger.Warn("review dispatch state unavailable; starting fresh", "error", err) @@ -10609,7 +10645,7 @@ func planReviewDispatch(cfg *config.Config, actionable *github.ActionableResult, for _, pr := range candidates { lane := classify.Classify(github.Issue{Title: pr.Title, Labels: pr.Labels}).Lane fullRepo := fullRepoName(pr.Repo, cfg.Project.Org) - prs = append(prs, review.PullRequest{ + item := review.PullRequest{ Repo: pr.Repo, Number: pr.Number, Title: pr.Title, @@ -10649,7 +10685,12 @@ func planReviewDispatch(cfg *config.Config, actionable *github.ActionableResult, // on this head (hivecommons/hive#13041): the planner kicks the // fix lane with it instead of reviewing the head again. HumanChangeRequest: review.HumanChangeRequestFor(review.DefaultHumanChangeRequests(), fullRepo, pr.Number, pr.HeadSHA), - }) + } + // Changed paths for the bot-author model route's path exceptions + // (review.bot_author_full_review_paths); fetched only for a listed + // bot's PR that is about to be reviewed. + item.ChangedFiles, item.ChangedFilesKnown = reviewBotRouteFilesFor(ctx, cfg, lister, botRoute, artifact, state, fullRepo, pr.Number, pr.Author, pr.HeadSHA, pr.BodyDigest) + prs = append(prs, item) } // PRs a review webhook just fired for go first, so the early eval cycle // it triggered spends its review budget on them (hivecommons/hive#11091). @@ -10688,7 +10729,13 @@ func planReviewDispatch(cfg *config.Config, actionable *github.ActionableResult, ReviewBotLogins: reviewBotLogins, ReviewBotMinPriority: reviewBots.MinPriority, PerspectiveModels: cfg.Review.PerspectiveModels, + BotAuthorRoute: botRoute, }) + for _, k := range plan.ReviewKicks { + if k.BotAuthorRouted { + logger.Info("review routed bot-authored PR to bot_author_model", "repo", k.Repo, "pr", k.Number, "head", k.HeadSHA, "agent", k.Agent, "review_model", k.ReviewModel, "review_backend", k.ReviewBackend) + } + } for _, sk := range plan.SkippedReviewModels { logger.Info("review model fallback skipped perspective", "repo", sk.Repo, "pr", sk.Number, "head", sk.HeadSHA, "perspective", sk.Perspective, "agent", sk.Agent, "author_model", sk.AuthorModel) } @@ -10709,6 +10756,16 @@ func planReviewDispatch(cfg *config.Config, actionable *github.ActionableResult, return plan } +// reviewChangedFilesListerFor returns the GitHub client as a +// reviewChangedFilesLister, or nil for a nil client: a typed nil in the +// interface would not read as nil. +func reviewChangedFilesListerFor(c *github.Client) reviewChangedFilesLister { + if c == nil { + return nil + } + return c +} + func refreshReviewVerdicts(cfg *config.Config, logger *slog.Logger) { if cfg == nil || !shouldRefreshReviewVerdicts(cfg) { return diff --git a/src/cmd/hive/review_bot_route_wiring_test.go b/src/cmd/hive/review_bot_route_wiring_test.go new file mode 100644 index 0000000000..21ee7134ab --- /dev/null +++ b/src/cmd/hive/review_bot_route_wiring_test.go @@ -0,0 +1,73 @@ +package main + +import ( + "context" + "testing" +) + +type fakeChangedFilesLister struct { + calls int + files []string + known bool +} + +func (f *fakeChangedFilesLister) ReviewPRChangedFiles(context.Context, string, int) ([]string, bool) { + f.calls++ + return f.files, f.known +} + +func TestPlanReviewDispatch_BotAuthorRouteUsesCheapModel(t *testing.T) { + redirectReviewPaths(t) + cfg := reviewSwarmConfig() + cfg.Review.BotAuthorModel = "gpt-6-luna" + lister := &fakeChangedFilesLister{files: []string{"go.mod", "go.sum"}, known: true} + plan := planReviewDispatchWith(context.Background(), cfg, actionableWithPR("dependabot[bot]"), nil, nil, lister, restoreTestLogger()) + if lister.calls != 1 || len(plan.ReviewKicks) != 1 { + t.Fatalf("calls=%d kicks=%d, want 1/1", lister.calls, len(plan.ReviewKicks)) + } + if k := plan.ReviewKicks[0]; !k.BotAuthorRouted || k.ReviewModel != "gpt-6-luna" { + t.Fatalf("kick not routed: %+v", k) + } +} + +func TestPlanReviewDispatch_BotAuthorRouteExceptionsAndGates(t *testing.T) { + redirectReviewPaths(t) + cfg := reviewSwarmConfig() + cfg.Review.BotAuthorModel = "gpt-6-luna" + + // A workflow file keeps the full-strength model. + lister := &fakeChangedFilesLister{files: []string{"go.mod", ".github/workflows/ci.yml"}, known: true} + plan := planReviewDispatchWith(context.Background(), cfg, actionableWithPR("dependabot[bot]"), nil, nil, lister, restoreTestLogger()) + if len(plan.ReviewKicks) != 1 || plan.ReviewKicks[0].BotAuthorRouted { + t.Fatalf("workflow change was downgraded: %+v", plan.ReviewKicks) + } + + // The hive's own App (project.ai_author) is never a "bot author", and + // its files are not even listed. + lister = &fakeChangedFilesLister{files: []string{"go.mod"}, known: true} + plan = planReviewDispatchWith(context.Background(), cfg, actionableWithPR("hive-bot"), nil, nil, lister, restoreTestLogger()) + if lister.calls != 0 || len(plan.ReviewKicks) != 1 || plan.ReviewKicks[0].BotAuthorRouted { + t.Fatalf("own app routed: calls=%d kicks=%+v", lister.calls, plan.ReviewKicks) + } + + // Route off: no listing, no downgrade. + off := reviewSwarmConfig() + lister = &fakeChangedFilesLister{files: []string{"go.mod"}, known: true} + plan = planReviewDispatchWith(context.Background(), off, actionableWithPR("dependabot[bot]"), nil, nil, lister, restoreTestLogger()) + if lister.calls != 0 || len(plan.ReviewKicks) != 1 || plan.ReviewKicks[0].BotAuthorRouted { + t.Fatalf("route off still acted: calls=%d kicks=%+v", lister.calls, plan.ReviewKicks) + } + + // Listing failed: unknown files, full-strength review. + lister = &fakeChangedFilesLister{known: false} + plan = planReviewDispatchWith(context.Background(), cfg, actionableWithPR("dependabot[bot]"), nil, nil, lister, restoreTestLogger()) + if len(plan.ReviewKicks) != 1 || plan.ReviewKicks[0].BotAuthorRouted { + t.Fatalf("unknown files downgraded: %+v", plan.ReviewKicks) + } + + // No GitHub client at all (nil lister) behaves the same. + plan = planReviewDispatch(cfg, actionableWithPR("dependabot[bot]"), nil, nil, restoreTestLogger()) + if len(plan.ReviewKicks) != 1 || plan.ReviewKicks[0].BotAuthorRouted { + t.Fatalf("nil lister downgraded: %+v", plan.ReviewKicks) + } +} diff --git a/src/docs/review-swarm.md b/src/docs/review-swarm.md index 5feb932537..3f0baa4c68 100644 --- a/src/docs/review-swarm.md +++ b/src/docs/review-swarm.md @@ -183,6 +183,9 @@ review: priority_labels: false # optional; mirror the PR review queue rank onto review-priority/* labels (default off) pending_ttl: 45m # optional; re-dispatch a review that has posted no verdict after this long (default 45m) max_redispatch: 3 # optional; expiries per head before the PR is held for a human (default 3) + bot_author_model: "" # optional; model (or backend/model) that reviews PRs opened by bot_authors, e.g. gpt-6-luna (default empty = off) + bot_authors: [dependabot[bot], renovate[bot], github-actions[bot], "*[bot]"] # optional; logins routed to bot_author_model; the hive's own App never matches + bot_author_full_review_paths: [".github/workflows/**", "Dockerfile*", "**/Dockerfile*"] # optional; a bot PR touching these keeps the full-strength model severity: block_at: P2 # optional; P1 | P2 | P3, see "Blocking line" below comment_below: true # default true @@ -403,6 +406,16 @@ When the check refuses, the `changes_requested` verdict still stands and the rev **Upgrading.** Older configs may still show `fix_human_prs: true` because a prior compatibility rule promoted `all_authors: true`. Both values are left in config for audit/UI continuity — `all_authors` no longer does anything, and branch-push fix kicks still require hive authorship. +### Cheaper model for bot-authored PRs (`review.bot_author_model`) + +PRs opened by other bots (Dependabot, Renovate, github-actions) are mostly lockfile and version bumps, and over Oct 13-27 2026 they cost $38.61/day across three hosted hives ($30.60/day on the busiest), every one reviewed on the frontier model. Setting `review.bot_author_model` routes those reviews to a cheaper model: + +- `review.bot_authors` is the login list. Entries match case-insensitively; a leading `*` matches a suffix, so the default `*[bot]` catches any GitHub App. The hive's own App login (`project.ai_author`, or the App's `[bot]` login when it authors PRs) never matches, whatever the list says: the hive's own PRs are reviewed as before. +- `review.bot_author_model` is `model` or `backend/model` (`gpt-6-luna`, `copilot/gemini-flash-3-5`). It replaces whatever the reviewer's `review_models` pool or `perspective_models` chose for that kick, the same way: the kick's independent-review instruction names it and the reviewer runs the review in a sub-agent with that `model`, so **the reviewer agent's engine must be able to launch it**. A pool decision to skip or hold for a human is kept. Recommended values from `pkg/tokens/pricing.go`: `gemini-flash-3-5` ($0.075/$0.30 per MTok) or `gpt-6-luna` ($0.10/$0.50), against `claude-fable-5.1` at $10/$50. Empty (the default) leaves the route off. +- `review.bot_author_full_review_paths` are globs (`**` spans directories; a pattern without `/` also matches the base name) that keep a bot PR on the full-strength model when any changed file matches. The default is CI workflows and Dockerfiles only; lockfiles are deliberately not excepted, since they are the bulk of bot PRs. Set it to `[]` for no exceptions. The planner lists the PR's files once per head (only for a listed bot's PR about to be reviewed) and a PR whose files could not be listed is **not** downgraded. + +Routed kicks carry `bot_author_routed` on the dispatch kick and the model lands in the verdict's `review_model` as usual; the governor logs `review routed bot-authored PR to bot_author_model`. The route applies to the swarm's per-perspective and combined kicks; the cadence (queue-mode) kick's `review_with=` annotation still comes from the agent's `review_models` pool alone. + ## Revising a recorded verdict Dispatch skips any PR that already carries a verdict for its head SHA. That is the right default — it stops the reviewer re-reviewing unchanged code every sweep — but it also makes a review final the moment it is written, even when the reviewer itself was defective. Two settings, both required, re-open such verdicts narrowly: diff --git a/src/pkg/config/config.go b/src/pkg/config/config.go index c54ba1e118..c2008c3fdf 100644 --- a/src/pkg/config/config.go +++ b/src/pkg/config/config.go @@ -7377,6 +7377,20 @@ type ReviewConfig struct { // may be re-sent when it posts no verdict (agents..review_heads, // hivecommons/hive#13542). 0 means max_redispatch applies. HeadMaxRetries int `yaml:"head_max_retries,omitempty" json:"head_max_retries,omitempty"` + // BotAuthors lists PR author logins whose PRs are reviewed with + // BotAuthorModel instead of the reviewer's usual model. Entries match + // case-insensitively; a leading `*` matches any login with that suffix + // (`*[bot]`). The hive's own GitHub App login never matches. Unset means + // DefaultReviewBotAuthors. + BotAuthors []string `yaml:"bot_authors,omitempty" json:"bot_authors,omitempty"` + // BotAuthorModel is the model (or backend/model) a review of a + // BotAuthors PR runs with. Empty (the default) leaves the route off. The + // reviewer agent's engine must be able to run the model. + BotAuthorModel string `yaml:"bot_author_model,omitempty" json:"bot_author_model,omitempty"` + // BotAuthorFullReviewPaths are path globs (`**` spans directories) that + // keep a bot-authored PR on the full-strength model when it touches any of + // them. Unset means DefaultReviewBotAuthorFullReviewPaths. + BotAuthorFullReviewPaths []string `yaml:"bot_author_full_review_paths,omitempty" json:"bot_author_full_review_paths,omitempty"` // HumanDecisionLabel names an EXISTING repo label to apply when the review // swarm holds a PR for a human. The review comment's in-body marker is // always the primary signal and never depends on this: a label makes the diff --git a/src/pkg/config/review_dispatch.go b/src/pkg/config/review_dispatch.go index 76bdffea03..3a7bad96c9 100644 --- a/src/pkg/config/review_dispatch.go +++ b/src/pkg/config/review_dispatch.go @@ -97,3 +97,55 @@ func (r ReviewConfig) ValidatePendingReviewTTL() error { } return nil } + +// DefaultReviewBotAuthors is review.bot_authors unset: the dependency and +// automation bots, plus any other `[bot]` login that is not the hive's own. +var DefaultReviewBotAuthors = []string{"dependabot[bot]", "renovate[bot]", "github-actions[bot]", "*[bot]"} + +// DefaultReviewBotAuthorFullReviewPaths is review.bot_author_full_review_paths +// unset: a bot PR that edits CI workflows or container builds is reviewed at +// full strength. Lockfiles are deliberately not here: they are the bulk of +// bot PRs and the reason the route exists. +var DefaultReviewBotAuthorFullReviewPaths = []string{".github/workflows/**", "Dockerfile*", "**/Dockerfile*"} + +// EffectiveBotAuthors resolves review.bot_authors, trimmed; unset means the +// default list. +func (r ReviewConfig) EffectiveBotAuthors() []string { + if r.BotAuthors == nil { + return append([]string(nil), DefaultReviewBotAuthors...) + } + return trimmedNonEmpty(r.BotAuthors) +} + +// EffectiveBotAuthorFullReviewPaths resolves +// review.bot_author_full_review_paths; unset means the default list. +func (r ReviewConfig) EffectiveBotAuthorFullReviewPaths() []string { + if r.BotAuthorFullReviewPaths == nil { + return append([]string(nil), DefaultReviewBotAuthorFullReviewPaths...) + } + return trimmedNonEmpty(r.BotAuthorFullReviewPaths) +} + +// BotAuthorModelRef splits review.bot_author_model into backend and model: +// `gpt-6-luna` is a model with no backend, `copilot/gpt-6-luna` names both. +// Both empty when the route is off. +func (r ReviewConfig) BotAuthorModelRef() (backend, model string) { + raw := strings.TrimSpace(r.BotAuthorModel) + if raw == "" { + return "", "" + } + if i := strings.Index(raw, "/"); i >= 0 { + return strings.TrimSpace(raw[:i]), strings.TrimSpace(raw[i+1:]) + } + return "", raw +} + +func trimmedNonEmpty(in []string) []string { + out := make([]string, 0, len(in)) + for _, v := range in { + if v = strings.TrimSpace(v); v != "" { + out = append(out, v) + } + } + return out +} diff --git a/src/pkg/config/review_dispatch_test.go b/src/pkg/config/review_dispatch_test.go index f8bfe422b5..1cbe6e5944 100644 --- a/src/pkg/config/review_dispatch_test.go +++ b/src/pkg/config/review_dispatch_test.go @@ -112,3 +112,27 @@ func TestEffectiveSelfAuthoredDecisionPolicy(t *testing.T) { t.Fatalf("unknown policy = %q, want resolve", got) } } + +func TestReviewBotAuthorRouteConfig(t *testing.T) { + var r ReviewConfig + if got := r.EffectiveBotAuthors(); len(got) != len(DefaultReviewBotAuthors) || got[0] != "dependabot[bot]" || got[len(got)-1] != "*[bot]" { + t.Fatalf("default bot_authors = %v", got) + } + if got := r.EffectiveBotAuthorFullReviewPaths(); len(got) != len(DefaultReviewBotAuthorFullReviewPaths) || got[0] != ".github/workflows/**" { + t.Fatalf("default full-review paths = %v", got) + } + r.BotAuthors = []string{" dependabot[bot] ", ""} + r.BotAuthorFullReviewPaths = []string{} + if got := r.EffectiveBotAuthors(); len(got) != 1 || got[0] != "dependabot[bot]" { + t.Fatalf("explicit bot_authors = %v", got) + } + if got := r.EffectiveBotAuthorFullReviewPaths(); len(got) != 0 { + t.Fatalf("explicit empty full-review paths = %v, want none (no exceptions)", got) + } + for in, want := range map[string][2]string{"": {"", ""}, "gpt-6-luna": {"", "gpt-6-luna"}, " copilot/gemini-flash-3-5 ": {"copilot", "gemini-flash-3-5"}} { + b, m := (ReviewConfig{BotAuthorModel: in}).BotAuthorModelRef() + if b != want[0] || m != want[1] { + t.Fatalf("bot_author_model %q -> %q/%q, want %q/%q", in, b, m, want[0], want[1]) + } + } +} diff --git a/src/pkg/github/review_bot_route.go b/src/pkg/github/review_bot_route.go new file mode 100644 index 0000000000..23b6f5e7f1 --- /dev/null +++ b/src/pkg/github/review_bot_route.go @@ -0,0 +1,30 @@ +package github + +import ( + "context" + "strings" + + gh "github.com/google/go-github/v72/github" +) + +// ReviewPRChangedFiles lists a PR's changed paths for the review planner's +// bot-author model route (review.bot_author_full_review_paths). known is +// false on any failure, including an incomplete list: a PR whose files cannot +// be checked against the full-review paths is not downgraded. +func (c *Client) ReviewPRChangedFiles(ctx context.Context, repo string, number int) (paths []string, known bool) { + if c == nil || c.client == nil || number <= 0 { + return nil, false + } + owner, name := c.splitRepo(strings.TrimSpace(repo)) + files, err := ListPRChangedFiles(WithRESTCaller(ctx, "hive:review_bot_route"), c.client, owner, name, &gh.PullRequest{Number: gh.Ptr(number)}) + if err != nil { + return nil, false + } + paths = make([]string, 0, len(files)) + for _, f := range files { + if p := strings.TrimSpace(f.Filename); p != "" { + paths = append(paths, p) + } + } + return paths, true +} diff --git a/src/pkg/review/bot_route.go b/src/pkg/review/bot_route.go new file mode 100644 index 0000000000..412d7624da --- /dev/null +++ b/src/pkg/review/bot_route.go @@ -0,0 +1,155 @@ +package review + +import ( + "regexp" + "strings" +) + +// Bot-author model route (hivecommons/hive: reviewer-cost analysis, Oct 13-27 +// 2026). PRs opened by other bots (Dependabot, Renovate, github-actions) cost +// $38.61/day across three hosted hives, $30.60/day on the busiest, every one +// reviewed on the frontier model. A lockfile bump does not need it. The route +// sends a bot-authored PR's review kick to a configured cheaper model unless +// the PR touches a path the operator wants read at full strength. + +// BotAuthorRoute is review.bot_authors / review.bot_author_model / +// review.bot_author_full_review_paths as the planner sees them. Model empty +// means the route is off. +type BotAuthorRoute struct { + Authors []string + Backend string + Model string + FullReviewPaths []string +} + +// Enabled reports whether a model is configured for the route. +func (r BotAuthorRoute) Enabled() bool { + return strings.TrimSpace(r.Model) != "" +} + +// IsBotAuthor reports whether login matches one of the patterns: an exact +// case-insensitive login, or a `*suffix` pattern such as `*[bot]`. The hive's +// own App login (ownApp) never matches, whatever the patterns say: the hive's +// own PRs are not dependency bumps. +func IsBotAuthor(login string, patterns []string, ownApp string) bool { + login = strings.TrimSpace(login) + if login == "" || strings.EqualFold(login, strings.TrimSpace(ownApp)) { + return false + } + for _, p := range patterns { + p = strings.TrimSpace(p) + if p == "" { + continue + } + if strings.HasPrefix(p, "*") { + if suffix := p[1:]; suffix != "" && strings.HasSuffix(strings.ToLower(login), strings.ToLower(suffix)) { + return true + } + continue + } + if strings.EqualFold(login, p) { + return true + } + } + return false +} + +// PathMatchesAny reports whether path matches any glob. `**` spans path +// separators, `*` and `?` do not; a pattern without a slash is matched against +// the base name as well as the whole path, so `Dockerfile*` finds +// `build/Dockerfile.ci`. +func PathMatchesAny(path string, globs []string) bool { + path = strings.TrimSpace(strings.TrimPrefix(path, "./")) + if path == "" { + return false + } + base := path + if i := strings.LastIndex(path, "/"); i >= 0 { + base = path[i+1:] + } + for _, g := range globs { + g = strings.TrimSpace(strings.TrimPrefix(g, "./")) + if g == "" { + continue + } + re, err := globRegexp(g) + if err != nil { + continue + } + if re.MatchString(path) || (!strings.Contains(g, "/") && re.MatchString(base)) { + return true + } + } + return false +} + +func globRegexp(glob string) (*regexp.Regexp, error) { + var b strings.Builder + b.WriteString("^") + for i := 0; i < len(glob); i++ { + c := glob[i] + switch { + case c == '*' && i+1 < len(glob) && glob[i+1] == '*': + i++ + // `**/` may match nothing at all, so `.github/workflows/**` + // and `**/Dockerfile` both behave as operators expect. + if i+1 < len(glob) && glob[i+1] == '/' { + i++ + b.WriteString("(?:.*/)?") + } else { + b.WriteString(".*") + } + case c == '*': + b.WriteString("[^/]*") + case c == '?': + b.WriteString("[^/]") + default: + b.WriteString(regexp.QuoteMeta(string(c))) + } + } + b.WriteString("$") + return regexp.Compile(b.String()) +} + +// botAuthorRouteFor decides whether this PR's review kick goes to the bot +// route's model. ok is false, with a reason, when the route is off, the +// author is not a listed bot, the changed-file list is unknown (a PR that +// cannot be checked against FullReviewPaths is not downgraded), or a changed +// file matches FullReviewPaths. +func botAuthorRouteFor(pr PullRequest, opts DispatchOptions) (backend, model string, ok bool, reason string) { + route := opts.BotAuthorRoute + if !route.Enabled() { + return "", "", false, "route off" + } + if !IsBotAuthor(pr.Author, route.Authors, opts.AIAuthor) { + return "", "", false, "author not a listed bot" + } + if !pr.ChangedFilesKnown { + return "", "", false, "changed files unknown" + } + for _, f := range pr.ChangedFiles { + if PathMatchesAny(f, route.FullReviewPaths) { + return "", "", false, "touches full-review path " + f + } + } + return strings.TrimSpace(route.Backend), strings.TrimSpace(route.Model), true, "" +} + +// applyBotAuthorRoute replaces the model the pool chose with the bot route's +// when the PR qualifies. A pool decision to skip or hold for a human is +// kept: those are about independence from the author model, which a bot PR +// has none of, but the planner does not second-guess them here. +func applyBotAuthorRoute(choice ReviewModelChoice, pr PullRequest, opts DispatchOptions) (ReviewModelChoice, bool) { + if choice.Skip || choice.RequiresHuman { + return choice, false + } + backend, model, ok, _ := botAuthorRouteFor(pr, opts) + if !ok { + return choice, false + } + choice.Backend = backend + choice.Model = model + choice.Fallback = false + choice.Configured = true + return choice, true +} diff --git a/src/pkg/review/bot_route_test.go b/src/pkg/review/bot_route_test.go new file mode 100644 index 0000000000..766957f931 --- /dev/null +++ b/src/pkg/review/bot_route_test.go @@ -0,0 +1,193 @@ +package review + +import ( + "strings" + "testing" + + "github.com/hivecommons/hive/pkg/config" +) + +var defaultBotAuthors = []string{"dependabot[bot]", "renovate[bot]", "github-actions[bot]", "*[bot]"} + +func TestIsBotAuthor(t *testing.T) { + own := "hivecommons-hive[bot]" + cases := []struct { + login string + patterns []string + want bool + }{ + {"dependabot[bot]", defaultBotAuthors, true}, + {"Renovate[bot]", defaultBotAuthors, true}, + {"github-actions[bot]", defaultBotAuthors, true}, + {"some-other-app[bot]", defaultBotAuthors, true}, + {own, defaultBotAuthors, false}, + {strings.ToUpper(own), defaultBotAuthors, false}, + {"clubanderson", defaultBotAuthors, false}, + {"", defaultBotAuthors, false}, + {"some-other-app[bot]", []string{"dependabot[bot]"}, false}, + {"dependabot[bot]", nil, false}, + {"pre-commit-ci[bot]", []string{" *[BOT] "}, true}, + {"bot", []string{"*"}, false}, + } + for _, tc := range cases { + if got := IsBotAuthor(tc.login, tc.patterns, own); got != tc.want { + t.Errorf("IsBotAuthor(%q, %v) = %v, want %v", tc.login, tc.patterns, got, tc.want) + } + } + // Without an own-app login every [bot] matches the wildcard. + if !IsBotAuthor(own, defaultBotAuthors, "") { + t.Fatal("own-app exclusion applied with no own app configured") + } +} + +func TestPathMatchesAnyGlobs(t *testing.T) { + defaults := []string{".github/workflows/**", "Dockerfile*", "**/Dockerfile*"} + cases := map[string]bool{ + ".github/workflows/ci.yml": true, + ".github/workflows/sub/x.yaml": true, + ".github/dependabot.yml": false, + "Dockerfile": true, + "Dockerfile.ci": true, + "build/Dockerfile": true, + "images/agent/Dockerfile.alpine": true, + "docs/Dockerfiles.md": true, + "go.mod": false, + "package-lock.json": false, + "src/package-lock.json": false, + "": false, + "./.github/workflows/release.yml": true, + } + for p, want := range cases { + if got := PathMatchesAny(p, defaults); got != want { + t.Errorf("PathMatchesAny(%q) = %v, want %v", p, got, want) + } + } + if PathMatchesAny("a/b/c.go", []string{"a/*.go"}) { + t.Fatal("single star crossed a directory") + } + if !PathMatchesAny("a/b/c.go", []string{"a/**/*.go"}) || !PathMatchesAny("a/c.go", []string{"a/**/*.go"}) { + t.Fatal("double star did not span zero or more directories") + } + if !PathMatchesAny("x.go", []string{"?.go"}) || PathMatchesAny("xy.go", []string{"?.go"}) { + t.Fatal("question mark glob wrong") + } + if PathMatchesAny("a.go", nil) || PathMatchesAny("a.go", []string{"", " "}) { + t.Fatal("empty globs matched") + } +} + +func botRouteOpts(model string, agents ...AgentCapability) DispatchOptions { + o := swarmOpts(agents...) + o.AIAuthor = "hive-bot[bot]" + o.BotAuthorRoute = BotAuthorRoute{Authors: defaultBotAuthors, Model: model, FullReviewPaths: []string{".github/workflows/**", "Dockerfile*"}} + return o +} + +func botPR(author string, files ...string) PullRequest { + pr := dispatchPR("sha1") + pr.Author = author + pr.ChangedFiles = files + pr.ChangedFilesKnown = true + return pr +} + +func TestBotAuthorRouteSelectsCheapModel(t *testing.T) { + pr := botPR("dependabot[bot]", "go.mod", "go.sum") + plan := PlanDispatch([]PullRequest{pr}, Artifact{}, DispatchState{}, botRouteOpts("gpt-6-luna", reviewer("r1"))) + if len(plan.ReviewKicks) != 1 { + t.Fatalf("kicks = %d", len(plan.ReviewKicks)) + } + k := plan.ReviewKicks[0] + if !k.BotAuthorRouted || k.ReviewModel != "gpt-6-luna" || k.ReviewBackend != "" { + t.Fatalf("kick not routed: %+v", k) + } + if !strings.Contains(k.Message, "Run this review with `gpt-6-luna`") { + t.Fatalf("prompt lacks the model instruction:\n%s", k.Message) + } + if plan.State.Pending[0].ReviewModel != "gpt-6-luna" { + t.Fatalf("pending stamped %q, want gpt-6-luna", plan.State.Pending[0].ReviewModel) + } +} + +func TestBotAuthorRouteBackendModelAndPoolOverride(t *testing.T) { + pr := botPR("renovate[bot]", "package-lock.json") + pr.AuthorModel = "" + agent := poolReviewer("r1", config.ReviewModelPoolEntry{Backend: "copilot", Model: "claude-opus-4-6"}) + opts := botRouteOpts("gemini-flash-3-5", agent) + opts.BotAuthorRoute.Backend = "copilot" + plan := PlanDispatch([]PullRequest{pr}, Artifact{}, DispatchState{}, opts) + if len(plan.ReviewKicks) != 1 { + t.Fatalf("kicks = %d", len(plan.ReviewKicks)) + } + k := plan.ReviewKicks[0] + if !k.BotAuthorRouted || k.ReviewModel != "gemini-flash-3-5" || k.ReviewBackend != "copilot" || k.ReviewModelFallback { + t.Fatalf("pool model not replaced by the route: %+v", k) + } + if !strings.Contains(k.Message, "`copilot/gemini-flash-3-5`") { + t.Fatalf("prompt lacks backend/model:\n%s", k.Message) + } +} + +func TestBotAuthorRouteNotTaken(t *testing.T) { + cases := []struct { + name string + pr PullRequest + opts func(DispatchOptions) DispatchOptions + }{ + {"route off", botPR("dependabot[bot]", "go.mod"), func(o DispatchOptions) DispatchOptions { o.BotAuthorRoute.Model = ""; return o }}, + {"human author", botPR("clubanderson", "go.mod"), nil}, + {"hive's own app", botPR("hive-bot[bot]", "go.mod"), nil}, + {"workflow touched", botPR("dependabot[bot]", "go.mod", ".github/workflows/ci.yml"), nil}, + {"dockerfile touched", botPR("dependabot[bot]", "Dockerfile.agent"), nil}, + {"changed files unknown", func() PullRequest { p := botPR("dependabot[bot]"); p.ChangedFilesKnown = false; return p }(), nil}, + {"not in a narrowed list", botPR("some-app[bot]", "go.mod"), func(o DispatchOptions) DispatchOptions { + o.BotAuthorRoute.Authors = []string{"dependabot[bot]"} + return o + }}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + opts := botRouteOpts("gpt-6-luna", reviewer("r1")) + if tc.opts != nil { + opts = tc.opts(opts) + } + plan := PlanDispatch([]PullRequest{tc.pr}, Artifact{}, DispatchState{}, opts) + if len(plan.ReviewKicks) != 1 { + t.Fatalf("kicks = %d", len(plan.ReviewKicks)) + } + k := plan.ReviewKicks[0] + if k.BotAuthorRouted || k.ReviewModel != "" { + t.Fatalf("routed when it should not be: %+v", k) + } + }) + } + // An empty known file list (GitHub returned nothing) is still known and + // routes: there is nothing to except on. + plan := PlanDispatch([]PullRequest{botPR("dependabot[bot]")}, Artifact{}, DispatchState{}, botRouteOpts("gpt-6-luna", reviewer("r1"))) + if len(plan.ReviewKicks) != 1 || !plan.ReviewKicks[0].BotAuthorRouted { + t.Fatalf("empty known file list did not route: %+v", plan.ReviewKicks) + } +} + +func TestBotAuthorRouteKeepsPoolSkipAndHold(t *testing.T) { + // A pool that excludes the author's family with fallback skip still + // skips: the route does not override a decision to withhold. + pr := botPR("dependabot[bot]", "go.mod") + pr.AuthorModel = "claude-fable-5.1" + agent := poolReviewer("r1", config.ReviewModelPoolEntry{Backend: "copilot", Model: "claude-fable-5.1"}) + agent.ReviewModels.Fallback = config.ReviewModelsFallbackSkip + plan := PlanDispatch([]PullRequest{pr}, Artifact{}, DispatchState{}, botRouteOpts("gpt-6-luna", agent)) + if len(plan.ReviewKicks) != 0 || len(plan.SkippedReviewModels) == 0 { + t.Fatalf("route overrode a pool skip: kicks=%+v skipped=%d", plan.ReviewKicks, len(plan.SkippedReviewModels)) + } +} + +func TestBotAuthorRouteCombinedPerspectives(t *testing.T) { + pr := botPR("dependabot[bot]", "go.mod") + opts := botRouteOpts("gpt-6-luna", reviewer("r1")) + opts.CombinedPerspectives = true + plan := PlanDispatch([]PullRequest{pr}, Artifact{}, DispatchState{}, opts) + if len(plan.ReviewKicks) != 1 || !plan.ReviewKicks[0].BotAuthorRouted || plan.ReviewKicks[0].ReviewModel != "gpt-6-luna" { + t.Fatalf("combined kick not routed: %+v", plan.ReviewKicks) + } +} diff --git a/src/pkg/review/dispatch.go b/src/pkg/review/dispatch.go index cfa87691eb..9dd09c527b 100644 --- a/src/pkg/review/dispatch.go +++ b/src/pkg/review/dispatch.go @@ -184,6 +184,10 @@ type DispatchOptions struct { // replaces the reviewer's review_models pool for the named perspective. // The reviewer's exclusions and fallback still apply to it. PerspectiveModels map[string]config.ReviewModelPoolEntry + // BotAuthorRoute is review.bot_authors / bot_author_model / + // bot_author_full_review_paths: the cheaper model a review of a PR opened + // by a listed bot runs with. Off when its Model is empty. + BotAuthorRoute BotAuthorRoute // AllAuthors is a no-op kept for configs that still set // review.all_authors. Every open PR is reviewed regardless of author // (hivecommons/hive#12974); the planner never reads this field. @@ -391,6 +395,9 @@ type DispatchKick struct { ReviewBackend string ReviewModel string ReviewModelFallback bool + // BotAuthorRouted is true when ReviewModel came from review.bot_author_model + // because the PR's author is a listed bot. + BotAuthorRouted bool } // SkippedReviewModel is a perspective kick the planner withheld because the @@ -668,6 +675,7 @@ func PlanDispatch(prs []PullRequest, artifact Artifact, state DispatchState, opt overridePerspective = missing[0] } choice := chooseReviewModel(reviewer, overridePerspective, pr.AuthorModel, opts.PerspectiveModels) + choice, botRouted := applyBotAuthorRoute(choice, pr, opts) if choice.Skip { for _, p := range missing { plan.SkippedReviewModels = append(plan.SkippedReviewModels, SkippedReviewModel{Repo: pr.Repo, Number: pr.Number, HeadSHA: pr.HeadSHA, Perspective: p, Agent: agent, AuthorModel: pr.AuthorModel}) @@ -690,7 +698,7 @@ func PlanDispatch(prs []PullRequest, artifact Artifact, state DispatchState, opt ReviewBackend: choice.Backend, ReviewModel: choice.Model, }) - plan.ReviewKicks = append(plan.ReviewKicks, DispatchKick{Agent: agent, Message: msg, PRRef: fmt.Sprintf("%s#%d", pr.Repo, pr.Number), Kind: "review", Repo: pr.Repo, Number: pr.Number, HeadSHA: pr.HeadSHA, BodyDigest: pr.BodyDigest, Perspective: missing[0], Perspectives: missing, AuthorAgent: pr.AuthorAgent, AuthorModel: pr.AuthorModel, ReviewBackend: choice.Backend, ReviewModel: choice.Model, ReviewModelFallback: choice.Fallback}) + plan.ReviewKicks = append(plan.ReviewKicks, DispatchKick{Agent: agent, Message: msg, PRRef: fmt.Sprintf("%s#%d", pr.Repo, pr.Number), Kind: "review", Repo: pr.Repo, Number: pr.Number, HeadSHA: pr.HeadSHA, BodyDigest: pr.BodyDigest, Perspective: missing[0], Perspectives: missing, AuthorAgent: pr.AuthorAgent, AuthorModel: pr.AuthorModel, ReviewBackend: choice.Backend, ReviewModel: choice.Model, ReviewModelFallback: choice.Fallback, BotAuthorRouted: botRouted}) for _, p := range missing { plan.State.Pending = append(plan.State.Pending, PendingReview{Repo: pr.Repo, Number: pr.Number, HeadSHA: pr.HeadSHA, BodyDigest: pr.BodyDigest, Perspective: p, Agent: agent, AuthorAgent: pr.AuthorAgent, AuthorModel: pr.AuthorModel, ReviewModel: stampedReviewModel(reviewer, choice), Dispatched: now}) } @@ -735,6 +743,7 @@ func PlanDispatch(prs []PullRequest, artifact Artifact, state DispatchState, opt agent := reviewer.Name perspective := missing[i] choice := chooseReviewModel(reviewer, perspective, pr.AuthorModel, opts.PerspectiveModels) + choice, botRouted := applyBotAuthorRoute(choice, pr, opts) if choice.Skip { plan.SkippedReviewModels = append(plan.SkippedReviewModels, SkippedReviewModel{Repo: pr.Repo, Number: pr.Number, HeadSHA: pr.HeadSHA, Perspective: perspective, Agent: agent, AuthorModel: pr.AuthorModel}) continue @@ -755,7 +764,7 @@ func PlanDispatch(prs []PullRequest, artifact Artifact, state DispatchState, opt ReviewBackend: choice.Backend, ReviewModel: choice.Model, }) - plan.ReviewKicks = append(plan.ReviewKicks, DispatchKick{Agent: agent, Message: msg, PRRef: fmt.Sprintf("%s#%d", pr.Repo, pr.Number), Kind: "review", Repo: pr.Repo, Number: pr.Number, HeadSHA: pr.HeadSHA, BodyDigest: pr.BodyDigest, Perspective: perspective, AuthorAgent: pr.AuthorAgent, AuthorModel: pr.AuthorModel, ReviewBackend: choice.Backend, ReviewModel: choice.Model, ReviewModelFallback: choice.Fallback}) + plan.ReviewKicks = append(plan.ReviewKicks, DispatchKick{Agent: agent, Message: msg, PRRef: fmt.Sprintf("%s#%d", pr.Repo, pr.Number), Kind: "review", Repo: pr.Repo, Number: pr.Number, HeadSHA: pr.HeadSHA, BodyDigest: pr.BodyDigest, Perspective: perspective, AuthorAgent: pr.AuthorAgent, AuthorModel: pr.AuthorModel, ReviewBackend: choice.Backend, ReviewModel: choice.Model, ReviewModelFallback: choice.Fallback, BotAuthorRouted: botRouted}) plan.State.Pending = append(plan.State.Pending, PendingReview{Repo: pr.Repo, Number: pr.Number, HeadSHA: pr.HeadSHA, BodyDigest: pr.BodyDigest, Perspective: perspective, Agent: agent, AuthorAgent: pr.AuthorAgent, AuthorModel: pr.AuthorModel, ReviewModel: stampedReviewModel(reviewer, choice), Dispatched: now}) availableSlots-- } diff --git a/src/pkg/review/prompts.go b/src/pkg/review/prompts.go index 64c4c4ef27..7a99460632 100644 --- a/src/pkg/review/prompts.go +++ b/src/pkg/review/prompts.go @@ -12,13 +12,18 @@ import ( ) type PullRequest struct { - Repo string - Number int - Title string - Author string - Labels []string - HeadSHA string - UpdatedAt time.Time + Repo string + Number int + Title string + Author string + Labels []string + // ChangedFiles are the PR's changed paths when the caller fetched them + // (ChangedFilesKnown), for the bot-author model route's path exceptions. + // Unknown means no downgrade. + ChangedFiles []string + ChangedFilesKnown bool + HeadSHA string + UpdatedAt time.Time // BodyDigest fingerprints the PR title/body that the reviewer judged. // The body itself deliberately stays out of review.PullRequest (see // grounding tests); this opaque digest lets a body-only remediation make From c2d9ca96858bd81141ba1a7418490709af1425b7 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Tue, 27 Oct 2026 13:42:11 -0400 Subject: [PATCH 2/2] test: allowlist review.bot_author_* in settings parity until a Settings control exists Signed-off-by: Andrew Anderson --- src/pkg/dashboard/settings_parity_allowlist.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/pkg/dashboard/settings_parity_allowlist.txt b/src/pkg/dashboard/settings_parity_allowlist.txt index f02c8b8942..bd8554290b 100644 --- a/src/pkg/dashboard/settings_parity_allowlist.txt +++ b/src/pkg/dashboard/settings_parity_allowlist.txt @@ -393,6 +393,9 @@ project.repos[] # TODO #11854 providers.status.enabled # TODO #11854 removed_agents[] # TODO #11854 review.backlog.labels[] # TODO #11854 +review.bot_author_full_review_paths[] # TODO #14625 +review.bot_author_model # TODO #14625 +review.bot_authors[] # TODO #14625 review.perspective_models..backend # TODO #13243 review.perspective_models..model # TODO #13243 review.perspective_prompts. # TODO #11854