From f558cd0f2e7d4eee91d0eeaa968b23247d3fc6f6 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Tue, 27 Oct 2026 14:18:54 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20review:=20delta=20re-review=20mode?= =?UTF-8?q?=20for=20PRs=20already=20judged=20at=20an=20earlier=20head?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A re-review of a PR the hive already judged at another head SHA now carries the previous verdict and findings and asks the reviewer to read only the compare between the two heads. An empty or trivial delta (under review.delta_trivial_lines, touching no previously flagged file) re-stamps the previous verdict onto the new head without a model call. Full review stays for first reviews, prior reject/incomplete, and stale changes_requested findings older than review.delta_max_age. Aggregates record delta.previous_head and delta.trivial so the cost dashboard can split delta reviews from full ones. The new review.delta_* keys are allowlisted in settings parity until a Settings control exists. Fixes #14621 Signed-off-by: Andrew Anderson --- .../added-14621-review-delta-rereview.md | 1 + src/cmd/hive/main.go | 76 +++- src/cmd/hive/review_bot_route_wiring_test.go | 6 + src/cmd/hive/review_delta_wiring_test.go | 103 +++++ src/docs/review-swarm.md | 12 + src/pkg/config/config.go | 23 ++ src/pkg/config/review_dispatch.go | 57 +++ src/pkg/config/review_dispatch_test.go | 29 ++ src/pkg/config/validate.go | 3 + .../dashboard/settings_parity_allowlist.txt | 3 + src/pkg/github/review_delta.go | 38 ++ src/pkg/review/delta.go | 372 +++++++++++++++++ src/pkg/review/delta_test.go | 375 ++++++++++++++++++ src/pkg/review/dispatch.go | 115 ++++-- src/pkg/review/model_stamps.go | 19 +- src/pkg/review/prompts.go | 12 + src/pkg/review/review.go | 13 + src/pkg/review/review_heads.go | 7 +- 18 files changed, 1219 insertions(+), 45 deletions(-) create mode 100644 changelog.d/added-14621-review-delta-rereview.md create mode 100644 src/cmd/hive/review_delta_wiring_test.go create mode 100644 src/pkg/github/review_delta.go create mode 100644 src/pkg/review/delta.go create mode 100644 src/pkg/review/delta_test.go diff --git a/changelog.d/added-14621-review-delta-rereview.md b/changelog.d/added-14621-review-delta-rereview.md new file mode 100644 index 0000000000..cb6380b989 --- /dev/null +++ b/changelog.d/added-14621-review-delta-rereview.md @@ -0,0 +1 @@ +- `review.delta_rereview` (default on), `review.delta_trivial_lines` (default 20) and `review.delta_max_age` (default 72h): a PR already judged at an earlier head is re-reviewed by its diff since that head, with the previous verdict and findings in the kick; an empty or trivial delta in files the previous verdict passed re-stamps that verdict onto the new head without a model call. Aggregates record `delta.previous_head` / `delta.trivial` so the cost dashboard can split delta reviews from full ones ([#14621](https://github.com/hivecommons/hive/issues/14621)). diff --git a/src/cmd/hive/main.go b/src/cmd/hive/main.go index 00a2e889d0..3dc9143e9a 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 := planReviewDispatchWith(ctx, cfg, actionable, agentMgr, beadStores, reviewChangedFilesListerFor(ghClient), logger) + reviewPlan := planReviewDispatchWith(ctx, cfg, actionable, agentMgr, beadStores, reviewPlannerGitHubFor(ghClient), logger) auditWithheldReviewFixes(reviewPlan, dashSrv, logger) if ghClient != nil { postReviewAcknowledgements(ctx, ghClient, reviewPlan, logger) @@ -10561,11 +10561,14 @@ 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 { +// reviewPlannerGitHub is the slice of the GitHub client the review planner +// reads: a PR's changed paths for the bot-author model route +// (review.bot_author_full_review_paths) and the compare between the head +// the hive last judged and the current one for the delta re-review +// (review.delta_rereview). +type reviewPlannerGitHub interface { ReviewPRChangedFiles(ctx context.Context, repo string, number int) ([]string, bool) + ReviewDeltaDiff(ctx context.Context, repo, previousHead, head string) review.DeltaDiff } func planReviewDispatch(cfg *config.Config, actionable *github.ActionableResult, agentMgr crux.Runtime, beadStores map[string]*beads.Store, logger *slog.Logger) review.DispatchPlan { @@ -10583,7 +10586,7 @@ func reviewBotAuthorRoute(cfg *config.Config) review.BotAuthorRoute { // 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) { +func reviewBotRouteFilesFor(ctx context.Context, cfg *config.Config, lister reviewPlannerGitHub, 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 } @@ -10596,7 +10599,30 @@ func reviewBotRouteFilesFor(ctx context.Context, cfg *config.Config, lister revi 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 { +// reviewDeltaDiffFor fetches the delta compare for one PR when it is a +// delta re-review candidate: delta mode on, no verdict yet at this head, no +// dispatch entry for it (a compare per cycle for a review in flight would be +// API spend for nothing), and a prior verdict at another head to diff from. +// nil otherwise, which the planner reads as "not fetched". +func reviewDeltaDiffFor(ctx context.Context, cfg *config.Config, differ reviewPlannerGitHub, artifact review.Artifact, state review.DispatchState, repo string, number int, headSHA, bodyDigest string) *review.DeltaDiff { + if differ == nil || !cfg.Review.DeltaRereviewEnabled() || strings.TrimSpace(headSHA) == "" { + return nil + } + if _, judged := artifact.AggregateForBody(repo, number, headSHA, bodyDigest); judged { + return nil + } + if !state.PendingSince(repo, number, headSHA).IsZero() { + return nil + } + prior, ok := artifact.PriorAggregateFor(repo, number, headSHA) + if !ok { + return nil + } + diff := differ.ReviewDeltaDiff(ctx, repo, prior.HeadSHA, headSHA) + return &diff +} + +func planReviewDispatchWith(ctx context.Context, cfg *config.Config, actionable *github.ActionableResult, agentMgr crux.Runtime, beadStores map[string]*beads.Store, ghr reviewPlannerGitHub, logger *slog.Logger) review.DispatchPlan { if cfg == nil || actionable == nil || !cfg.Review.RequireApproval || !cfg.Review.FanOut { return review.DispatchPlan{} } @@ -10685,11 +10711,14 @@ func planReviewDispatchWith(ctx context.Context, cfg *config.Config, actionable // 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), + // The compare from the last judged head, for the delta + // re-review and its trivial skip (review.delta_rereview). + DeltaDiff: reviewDeltaDiffFor(ctx, cfg, ghr, artifact, state, fullRepo, pr.Number, pr.HeadSHA, pr.BodyDigest), } // 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) + item.ChangedFiles, item.ChangedFilesKnown = reviewBotRouteFilesFor(ctx, cfg, ghr, 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 @@ -10730,7 +10759,11 @@ func planReviewDispatchWith(ctx context.Context, cfg *config.Config, actionable ReviewBotMinPriority: reviewBots.MinPriority, PerspectiveModels: cfg.Review.PerspectiveModels, BotAuthorRoute: botRoute, + DeltaRereview: cfg.Review.DeltaRereviewEnabled(), + DeltaTrivialLines: cfg.Review.EffectiveDeltaTrivialLines(), + DeltaMaxAge: cfg.Review.EffectiveDeltaMaxAge(), }) + recordReviewDeltaRestamps(plan, logger) 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) @@ -10756,10 +10789,29 @@ func planReviewDispatchWith(ctx context.Context, cfg *config.Config, actionable 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 { +// recordReviewDeltaRestamps writes the verdicts the planner carried forward +// onto a new head without a session (trivial delta, review.delta_rereview) +// into the verdict artifact, and logs each one. A write failure is logged, +// not fatal: the next cycle plans the same restamp again. +func recordReviewDeltaRestamps(plan review.DispatchPlan, logger *slog.Logger) { + if len(plan.DeltaRestamps) == 0 { + return + } + for _, r := range plan.DeltaRestamps { + note := "" + if r.Aggregate.Delta != nil { + note = r.Aggregate.Delta.Note + } + logger.Info("review delta re-stamped verdict without a session", "repo", r.Repo, "pr", r.Number, "head", r.HeadSHA, "previous_head", r.PreviousHead, "verdict", r.Aggregate.Verdict, "note", note) + } + if _, err := review.RecordDeltaRestamps("", plan.DeltaRestamps, time.Now().UTC()); err != nil { + logger.Warn("failed to record review delta re-stamps", "error", err, "count", len(plan.DeltaRestamps)) + } +} + +// reviewPlannerGitHubFor returns the GitHub client as a reviewPlannerGitHub, +// or nil for a nil client: a typed nil in the interface would not read as nil. +func reviewPlannerGitHubFor(c *github.Client) reviewPlannerGitHub { if c == nil { return nil } diff --git a/src/cmd/hive/review_bot_route_wiring_test.go b/src/cmd/hive/review_bot_route_wiring_test.go index 21ee7134ab..f3cd588321 100644 --- a/src/cmd/hive/review_bot_route_wiring_test.go +++ b/src/cmd/hive/review_bot_route_wiring_test.go @@ -3,6 +3,8 @@ package main import ( "context" "testing" + + "github.com/hivecommons/hive/pkg/review" ) type fakeChangedFilesLister struct { @@ -16,6 +18,10 @@ func (f *fakeChangedFilesLister) ReviewPRChangedFiles(context.Context, string, i return f.files, f.known } +func (f *fakeChangedFilesLister) ReviewDeltaDiff(_ context.Context, _, previousHead, _ string) review.DeltaDiff { + return review.DeltaDiff{PreviousHead: previousHead} +} + func TestPlanReviewDispatch_BotAuthorRouteUsesCheapModel(t *testing.T) { redirectReviewPaths(t) cfg := reviewSwarmConfig() diff --git a/src/cmd/hive/review_delta_wiring_test.go b/src/cmd/hive/review_delta_wiring_test.go new file mode 100644 index 0000000000..fd31862631 --- /dev/null +++ b/src/cmd/hive/review_delta_wiring_test.go @@ -0,0 +1,103 @@ +package main + +import ( + "context" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/review" +) + +type fakeDeltaDiffer struct { + calls []string + diff review.DeltaDiff +} + +func (f *fakeDeltaDiffer) ReviewDeltaDiff(_ context.Context, repo, previousHead, head string) review.DeltaDiff { + f.calls = append(f.calls, repo+"#"+previousHead+".."+head) + out := f.diff + out.PreviousHead = previousHead + return out +} + +func (f *fakeDeltaDiffer) ReviewPRChangedFiles(context.Context, string, int) ([]string, bool) { + return nil, false +} + +func writePriorVerdict(t *testing.T, headSHA string, verdict review.Verdict) { + t.Helper() + art := review.Artifact{Items: []review.Aggregate{{ + Repo: "hivecommons/hive", Number: 4321, HeadSHA: headSHA, Verdict: verdict, + MergeEligible: verdict == review.VerdictApprove, RecordedAt: time.Now().UTC().Add(-time.Hour), + Perspectives: map[review.Perspective]review.Verdict{review.PerspectiveCorrectness: verdict}, + }}} + if err := review.WriteArtifact(review.ReviewVerdictsPath, art); err != nil { + t.Fatal(err) + } +} + +func TestPlanReviewDispatch_DeltaReReviewFetchesCompareAndScopesKick(t *testing.T) { + redirectReviewPaths(t) + writePriorVerdict(t, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", review.VerdictApprove) + differ := &fakeDeltaDiffer{diff: review.DeltaDiff{Known: true, Files: []review.DeltaFile{{Path: "pkg/x/x.go", Additions: 40}}}} + plan := planReviewDispatchWith(context.Background(), reviewSwarmConfig(), actionableWithPR("hive-bot"), nil, nil, differ, restoreTestLogger()) + if len(differ.calls) != 1 || differ.calls[0] != "hivecommons/hive#bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.."+reviewTestSHA { + t.Fatalf("compare calls = %v", differ.calls) + } + if len(plan.ReviewKicks) != 1 || len(plan.DeltaRestamps) != 0 { + t.Fatalf("kicks=%d restamps=%d, want 1/0", len(plan.ReviewKicks), len(plan.DeltaRestamps)) + } + if k := plan.ReviewKicks[0]; k.DeltaPreviousHead == "" || !strings.Contains(k.Message, "DELTA RE-REVIEW") || !strings.Contains(k.Message, "pkg/x/x.go") { + t.Fatalf("kick is not a delta re-review: %+v", k) + } +} + +func TestPlanReviewDispatch_DeltaTrivialRestampIsRecorded(t *testing.T) { + redirectReviewPaths(t) + writePriorVerdict(t, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", review.VerdictApprove) + differ := &fakeDeltaDiffer{diff: review.DeltaDiff{Known: true}} + plan := planReviewDispatchWith(context.Background(), reviewSwarmConfig(), actionableWithPR("hive-bot"), nil, nil, differ, restoreTestLogger()) + if len(plan.ReviewKicks) != 0 || len(plan.DeltaRestamps) != 1 { + t.Fatalf("kicks=%d restamps=%d, want 0/1", len(plan.ReviewKicks), len(plan.DeltaRestamps)) + } + art, err := review.LoadArtifact("") + if err != nil { + t.Fatal(err) + } + got, ok := art.AggregateForBody("hivecommons/hive", 4321, reviewTestSHA, "") + if !ok || got.Verdict != review.VerdictApprove || got.Delta == nil || !got.Delta.Trivial { + t.Fatalf("restamp not recorded in the artifact: %+v ok=%v", got, ok) + } + // The next cycle sees the head as judged: no compare, no kick, no restamp. + differ.calls = nil + again := planReviewDispatchWith(context.Background(), reviewSwarmConfig(), actionableWithPR("hive-bot"), nil, nil, differ, restoreTestLogger()) + if len(differ.calls) != 0 || len(again.ReviewKicks) != 0 || len(again.DeltaRestamps) != 0 { + t.Fatalf("judged head re-planned: calls=%v kicks=%d restamps=%d", differ.calls, len(again.ReviewKicks), len(again.DeltaRestamps)) + } +} + +func TestPlanReviewDispatch_DeltaNotFetchedWithoutPriorOrWhenOff(t *testing.T) { + redirectReviewPaths(t) + differ := &fakeDeltaDiffer{diff: review.DeltaDiff{Known: true}} + plan := planReviewDispatchWith(context.Background(), reviewSwarmConfig(), actionableWithPR("hive-bot"), nil, nil, differ, restoreTestLogger()) + if len(differ.calls) != 0 || len(plan.ReviewKicks) != 1 || plan.ReviewKicks[0].DeltaPreviousHead != "" { + t.Fatalf("first review touched the compare API or became a delta: calls=%v kicks=%+v", differ.calls, plan.ReviewKicks) + } + + writePriorVerdict(t, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", review.VerdictApprove) + cfg := reviewSwarmConfig() + off := false + cfg.Review.DeltaRereview = &off + plan = planReviewDispatchWith(context.Background(), cfg, actionableWithPR("hive-bot"), nil, nil, differ, restoreTestLogger()) + if len(differ.calls) != 0 || len(plan.ReviewKicks) != 1 || plan.ReviewKicks[0].DeltaPreviousHead != "" || len(plan.DeltaRestamps) != 0 { + t.Fatalf("delta_rereview: false still planned a delta: calls=%v kicks=%+v restamps=%d", differ.calls, plan.ReviewKicks, len(plan.DeltaRestamps)) + } + + // A nil differ (no GitHub client) falls back to a delta kick without a + // file list rather than a full review: the prompt alone needs no compare. + plan = planReviewDispatch(reviewSwarmConfig(), actionableWithPR("hive-bot"), nil, nil, restoreTestLogger()) + if len(plan.ReviewKicks) != 1 || plan.ReviewKicks[0].DeltaPreviousHead == "" || len(plan.DeltaRestamps) != 0 { + t.Fatalf("nil differ: kicks=%+v restamps=%d", plan.ReviewKicks, len(plan.DeltaRestamps)) + } +} diff --git a/src/docs/review-swarm.md b/src/docs/review-swarm.md index 3f0baa4c68..9264bc1354 100644 --- a/src/docs/review-swarm.md +++ b/src/docs/review-swarm.md @@ -186,6 +186,9 @@ review: 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 + delta_rereview: true # optional; re-review a PR already judged at an earlier head by its diff since that head (default on) + delta_trivial_lines: 20 # optional; a delta under this many lines in files the prior verdict passed re-stamps it without a model call (default 20; negative disables) + delta_max_age: 72h # optional; a changes_requested verdict with findings older than this anchors no delta, the PR is reviewed in full (default 72h) severity: block_at: P2 # optional; P1 | P2 | P3, see "Blocking line" below comment_below: true # default true @@ -415,6 +418,15 @@ PRs opened by other bots (Dependabot, Renovate, github-actions) are mostly lockf - `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. +### Delta re-review (`review.delta_rereview`, `review.delta_trivial_lines`, `review.delta_max_age`) + +A re-review is a review of a PR the hive already judged at an earlier head SHA. Over Oct 13-27 2026 re-reviews were 28% of hive's attributed reviewer spend and 53% of Bluefin's, because every one re-read the whole PR. With `review.delta_rereview` on (the default) the planner looks for the most recent verdict for the same PR at another head within the artifact's retention and, when it finds one, builds the kick differently: + +- The prompt opens with a **`DELTA RE-REVIEW`** block naming the previous head, when it was judged, the aggregate and per-perspective verdicts, and the exact `gh api repos/O/R/compare/...` commands for the file list and the unified diff. It tells the reviewer to read only the hunks in that compare (and enough of their surroundings to judge them), to re-check each previous finding by file and line and say whether it is fixed, still present or no longer applies, and that its verdict is still for the whole PR at the new head. When the governor fetched the compare itself, the block also lists the changed files and line count. +- When the compare is known and the delta is **empty** (a force-push or rebase with the same tree), or under `review.delta_trivial_lines` changed lines (default 20) touching no file the previous verdict recorded a finding in, no reviewer is kicked: the previous aggregate is **re-stamped** onto the new head with the new `head_sha`, `body_digest` and `recorded_at`, a `delta` block (`previous_head`, `trivial: true`, `changed_lines`, `note`) and a `delta re-review: …` reason, and the governor logs `review delta re-stamped verdict without a session`. A negative `delta_trivial_lines` keeps the re-stamp for identical trees only. A head that already has a review in flight is never re-stamped. +- The **full** review stays for a first review (no prior verdict at another head), when the prior verdict was `reject` or `incomplete`, and when it was `changes_requested` with findings recorded more than `review.delta_max_age` ago (default `72h`): the flagged code has had time to move on and the record of it is stale. A verdict at the current head is never treated as a prior. + +Every delta aggregate records `delta.previous_head` (and `delta_previous_head` on each perspective report, stamped from the dispatch state like `review_model`), so the cost dashboard can split delta reviews, re-stamps and full reviews. The compare is fetched once per candidate head (no verdict, no pending entry, a prior at another head) with one 300-file page; a bigger or failed compare still gets the delta instruction, just without the file list and never the re-stamp. ## Revising a recorded verdict diff --git a/src/pkg/config/config.go b/src/pkg/config/config.go index d412e7f53c..24eb5b5947 100644 --- a/src/pkg/config/config.go +++ b/src/pkg/config/config.go @@ -7406,6 +7406,29 @@ type ReviewConfig struct { // 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"` + // DeltaRereview scopes a re-review to what changed since the head the + // hive last judged. When a PR already carries a verdict for an earlier + // head SHA, the new head's kick carries that verdict and its findings + // and tells the reviewer to read `git diff ..` plus + // re-check the flagged items, not the whole PR again. Re-reviews were + // 53% of one hosted hive's reviewer spend over two weeks (Oct 13-27 + // 2026). nil means on; `delta_rereview: false` turns it off. First + // reviews, a prior `reject`, and a prior `changes_requested` whose + // findings are older than delta_max_age stay full reviews. + DeltaRereview *bool `yaml:"delta_rereview,omitempty" json:"delta_rereview,omitempty"` + // DeltaTrivialLines is the delta size (added plus deleted lines) under + // which a re-review skips the model call: when the diff between the + // reviewed head and the new head is empty, or touches only files the + // previous verdict had no finding in and is smaller than this, the + // previous verdict is re-stamped onto the new head with a note. 0 means + // DefaultReviewDeltaTrivialLines; negative disables the skip. + DeltaTrivialLines int `yaml:"delta_trivial_lines,omitempty" json:"delta_trivial_lines,omitempty"` + // DeltaMaxAge is how old a `changes_requested` verdict with findings may + // be and still anchor a delta re-review (Go duration string). Older than + // this the new head gets a full review, since the flagged code and the + // reviewer's memory of it have both moved on. Empty means + // DefaultReviewDeltaMaxAge. + DeltaMaxAge string `yaml:"delta_max_age,omitempty" json:"delta_max_age,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 3a7bad96c9..2e256df99f 100644 --- a/src/pkg/config/review_dispatch.go +++ b/src/pkg/config/review_dispatch.go @@ -149,3 +149,60 @@ func trimmedNonEmpty(in []string) []string { } return out } + +const ( + // DefaultReviewDeltaTrivialLines is review.delta_trivial_lines unset: a + // re-review delta under this many changed lines, touching only files the + // previous verdict found nothing in, re-stamps that verdict without a + // model call. + DefaultReviewDeltaTrivialLines = 20 + // DefaultReviewDeltaMaxAge is review.delta_max_age unset: a + // changes_requested verdict with findings older than this anchors no + // delta re-review. + DefaultReviewDeltaMaxAge = 72 * time.Hour +) + +// DeltaRereviewEnabled resolves review.delta_rereview; unset means on. +func (r ReviewConfig) DeltaRereviewEnabled() bool { + return r.DeltaRereview == nil || *r.DeltaRereview +} + +// EffectiveDeltaTrivialLines resolves review.delta_trivial_lines: 0 is the +// default, a negative value disables the trivial-delta skip (returned as 0). +func (r ReviewConfig) EffectiveDeltaTrivialLines() int { + switch { + case r.DeltaTrivialLines == 0: + return DefaultReviewDeltaTrivialLines + case r.DeltaTrivialLines < 0: + return 0 + default: + return r.DeltaTrivialLines + } +} + +// EffectiveDeltaMaxAge resolves review.delta_max_age; empty or unparsable +// means DefaultReviewDeltaMaxAge. +func (r ReviewConfig) EffectiveDeltaMaxAge() time.Duration { + raw := strings.TrimSpace(r.DeltaMaxAge) + if raw == "" { + return DefaultReviewDeltaMaxAge + } + d, err := time.ParseDuration(raw) + if err != nil || d <= 0 { + return DefaultReviewDeltaMaxAge + } + return d +} + +// ValidateDeltaMaxAge rejects a review.delta_max_age that is not a Go +// duration string. +func (r ReviewConfig) ValidateDeltaMaxAge() error { + raw := strings.TrimSpace(r.DeltaMaxAge) + if raw == "" { + return nil + } + if _, err := time.ParseDuration(raw); err != nil { + return fmt.Errorf("review: delta_max_age %q is not a duration (e.g. 72h); unset means the default %s", r.DeltaMaxAge, DefaultReviewDeltaMaxAge) + } + return nil +} diff --git a/src/pkg/config/review_dispatch_test.go b/src/pkg/config/review_dispatch_test.go index 1cbe6e5944..146f570284 100644 --- a/src/pkg/config/review_dispatch_test.go +++ b/src/pkg/config/review_dispatch_test.go @@ -136,3 +136,32 @@ func TestReviewBotAuthorRouteConfig(t *testing.T) { } } } + +func TestReviewDeltaRereviewConfig(t *testing.T) { + off := false + if !(ReviewConfig{}).DeltaRereviewEnabled() { + t.Fatal("delta_rereview unset should be on") + } + if (ReviewConfig{DeltaRereview: &off}).DeltaRereviewEnabled() { + t.Fatal("delta_rereview: false still on") + } + for in, want := range map[int]int{0: DefaultReviewDeltaTrivialLines, 5: 5, -1: 0} { + if got := (ReviewConfig{DeltaTrivialLines: in}).EffectiveDeltaTrivialLines(); got != want { + t.Fatalf("delta_trivial_lines %d -> %d, want %d", in, got, want) + } + } + for in, want := range map[string]time.Duration{"": DefaultReviewDeltaMaxAge, "24h": 24 * time.Hour, "garbage": DefaultReviewDeltaMaxAge, "-1h": DefaultReviewDeltaMaxAge} { + if got := (ReviewConfig{DeltaMaxAge: in}).EffectiveDeltaMaxAge(); got != want { + t.Fatalf("delta_max_age %q -> %s, want %s", in, got, want) + } + } + if err := (ReviewConfig{DeltaMaxAge: "garbage"}).ValidateDeltaMaxAge(); err == nil { + t.Fatal("ValidateDeltaMaxAge accepted a non-duration") + } + if err := (ReviewConfig{DeltaMaxAge: "36h"}).ValidateDeltaMaxAge(); err != nil { + t.Fatalf("ValidateDeltaMaxAge rejected 36h: %v", err) + } + if err := (ReviewConfig{}).ValidateDeltaMaxAge(); err != nil { + t.Fatalf("ValidateDeltaMaxAge rejected unset: %v", err) + } +} diff --git a/src/pkg/config/validate.go b/src/pkg/config/validate.go index be04f5450e..8990fdb178 100644 --- a/src/pkg/config/validate.go +++ b/src/pkg/config/validate.go @@ -146,6 +146,9 @@ func (c *Config) ValidateWithOptions(opts ValidateOptions) error { if err := c.Review.ValidatePendingReviewTTL(); err != nil { return err } + if err := c.Review.ValidateDeltaMaxAge(); err != nil { + return err + } if err := c.validateReviewPerspectiveModels(); err != nil { return err } diff --git a/src/pkg/dashboard/settings_parity_allowlist.txt b/src/pkg/dashboard/settings_parity_allowlist.txt index bd8554290b..7915ffbd42 100644 --- a/src/pkg/dashboard/settings_parity_allowlist.txt +++ b/src/pkg/dashboard/settings_parity_allowlist.txt @@ -396,6 +396,9 @@ 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.delta_max_age # TODO #14621 +review.delta_rereview # TODO #14621 +review.delta_trivial_lines # TODO #14621 review.perspective_models..backend # TODO #13243 review.perspective_models..model # TODO #13243 review.perspective_prompts. # TODO #11854 diff --git a/src/pkg/github/review_delta.go b/src/pkg/github/review_delta.go new file mode 100644 index 0000000000..3328282a43 --- /dev/null +++ b/src/pkg/github/review_delta.go @@ -0,0 +1,38 @@ +package github + +import ( + "context" + "strings" + + gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/review" +) + +// ReviewDeltaDiff fetches the compare between a PR's previously reviewed head +// and its current head for the review planner's delta re-review +// (review.delta_rereview). Both are immutable SHAs, so the answer cannot +// describe a newer push. One page of files is read: GitHub caps a compare's +// file list at githubCompareFileLimit per page, and a delta that large is not +// a candidate for the trivial skip anyway, so a full page is reported as +// unknown rather than paged. Any failure is also unknown, never an error: the +// planner then sends the delta instruction without a file list. +func (c *Client) ReviewDeltaDiff(ctx context.Context, repo, previousHead, head string) review.DeltaDiff { + out := review.DeltaDiff{PreviousHead: strings.TrimSpace(previousHead)} + head = strings.TrimSpace(head) + if c == nil || c.client == nil || out.PreviousHead == "" || head == "" || review.SameHeadSHA(out.PreviousHead, head) { + return out + } + owner, name := c.splitRepo(strings.TrimSpace(repo)) + comparison, resp, err := c.client.Repositories.CompareCommits(WithRESTCaller(ctx, "hive:review_delta"), owner, name, out.PreviousHead, head, &gh.ListOptions{PerPage: githubCompareFileLimit}) + if err != nil || comparison == nil { + return out + } + if (resp != nil && resp.NextPage != 0) || len(comparison.Files) >= githubCompareFileLimit { + return out + } + out.Known = true + for _, f := range comparison.Files { + out.Files = append(out.Files, review.DeltaFile{Path: f.GetFilename(), Additions: f.GetAdditions(), Deletions: f.GetDeletions()}) + } + return out +} diff --git a/src/pkg/review/delta.go b/src/pkg/review/delta.go new file mode 100644 index 0000000000..24b1441e32 --- /dev/null +++ b/src/pkg/review/delta.go @@ -0,0 +1,372 @@ +package review + +import ( + "fmt" + "sort" + "strings" + "time" +) + +// Delta re-review (hivecommons/hive: reviewer-cost analysis, Oct 13-27 2026). +// +// A re-review is a review of a PR the hive has already judged at an earlier +// head SHA. Across three hosted hives re-reviews were 28% of attributed +// reviewer spend, and 53% on the busiest one, because each one re-read the +// whole PR: the diff against the base, the touched files, their callers. +// Almost all of that was already read and judged at the previous head. The +// delta mode hands the reviewer its previous verdict and asks it to read the +// diff between the two heads instead, re-checking what it flagged before; +// a delta that is empty (a force-push with the same tree) or trivial skips +// the model call and re-stamps the previous verdict onto the new head. + +// DefaultDeltaTrivialLines and DefaultDeltaMaxAge mirror the config defaults +// (config.DefaultReviewDeltaTrivialLines, config.DefaultReviewDeltaMaxAge) for +// callers that build DispatchOptions without a config. +const ( + DefaultDeltaTrivialLines = 20 + DefaultDeltaMaxAge = 72 * time.Hour +) + +// DeltaFile is one file in the compare between the reviewed head and the +// current head. +type DeltaFile struct { + Path string `json:"path"` + Additions int `json:"additions,omitempty"` + Deletions int `json:"deletions,omitempty"` +} + +// DeltaDiff is the compare between a PR's previously reviewed head and its +// current head, fetched by the caller (pkg/github ReviewDeltaDiff) for a PR +// the planner may delta-review. The planner never calls GitHub itself. +type DeltaDiff struct { + // PreviousHead is the base of the compare: the head the prior verdict + // stands against. It must match the prior aggregate the planner found, + // or the diff is ignored. + PreviousHead string + // Known is true when the compare succeeded and the file list is + // complete. Unknown means no trivial skip: the reviewer still gets the + // delta instruction, since that needs no file list. + Known bool + Files []DeltaFile +} + +// ChangedLines is the delta size the trivial threshold is judged against. +func (d DeltaDiff) ChangedLines() int { + n := 0 + for _, f := range d.Files { + n += f.Additions + f.Deletions + } + return n +} + +// Identical reports an empty known delta: the new head's tree matches the +// reviewed head's (a rebase or an amended message). +func (d DeltaDiff) Identical() bool { + return d.Known && len(d.Files) == 0 +} + +// DeltaContext is what a delta re-review kick carries about the previous +// verdict. It goes into the prompt, so it holds only what the planner wrote +// or the reviewer itself reported before: never the PR author's text. +type DeltaContext struct { + PreviousHead string + PreviousVerdict Verdict + PreviousRecordedAt time.Time + // PreviousPerspectives is the per-perspective verdict at the previous + // head, so a reviewer covering several perspectives knows which ones + // withheld approval. + PreviousPerspectives map[Perspective]Verdict + // PreviousFindings are the findings recorded at the previous head, which + // the reviewer must re-check at the new head. + PreviousFindings []PerspectiveFinding + // Files are the paths the delta touches when the compare is known; + // empty when it is not, and the reviewer reads the diff itself. + Files []string + // ChangedLines is the known delta size, 0 when unknown. + ChangedLines int + DiffKnown bool +} + +// DeltaReviewInfo is recorded on an aggregate produced by a delta re-review, +// so the cost dashboard and the verdict reader can tell a delta from a full +// review and find the head it was judged against. +type DeltaReviewInfo struct { + PreviousHead string `json:"previous_head"` + // Trivial is true when no model call was made: the previous verdict was + // re-stamped onto the new head because the delta was empty or under + // review.delta_trivial_lines in files the previous verdict passed. + Trivial bool `json:"trivial,omitempty"` + ChangedLines int `json:"changed_lines,omitempty"` + Note string `json:"note,omitempty"` +} + +// DeltaRestamp is a verdict the planner carried from a previous head onto the +// current head without a review session. The caller records it in the +// verdict artifact (RecordDeltaRestamps); the planner itself only plans. +type DeltaRestamp struct { + Repo string + Number int + HeadSHA string + PreviousHead string + Aggregate Aggregate +} + +// deltaMode is the planner's decision for one PR head. +type deltaMode int + +const ( + deltaModeFull deltaMode = iota + deltaModeDelta + deltaModeTrivial +) + +// PriorAggregateFor returns the most recently recorded aggregate for this PR +// at a head OTHER than currentHead: the verdict a delta re-review is judged +// against. ok is false when the PR has never been reviewed at another head +// within the artifact's retention. +func (a Artifact) PriorAggregateFor(repo string, number int, currentHead string) (Aggregate, bool) { + wantRepo := strings.TrimSpace(repo) + var match Aggregate + var ok bool + for _, item := range a.Items { + if item.Number != number || strings.TrimSpace(item.Repo) != wantRepo { + continue + } + if strings.TrimSpace(item.HeadSHA) == "" || SameHeadSHA(item.HeadSHA, currentHead) { + continue + } + if !ok || item.RecordedAt.After(match.RecordedAt) { + match = item.Normalize() + ok = true + } + } + return match, ok +} + +func (o DispatchOptions) effectiveDeltaTrivialLines() int { + if o.DeltaTrivialLines < 0 { + return 0 + } + if o.DeltaTrivialLines == 0 { + return DefaultDeltaTrivialLines + } + return o.DeltaTrivialLines +} + +func (o DispatchOptions) effectiveDeltaMaxAge() time.Duration { + if o.DeltaMaxAge <= 0 { + return DefaultDeltaMaxAge + } + return o.DeltaMaxAge +} + +// planDelta decides whether this head is reviewed in full, as a delta against +// the prior verdict, or re-stamped without a session. The rules: +// +// - delta_rereview off, no prior verdict at another head, or a prior verdict +// that is incomplete or reject: full review. A first review has nothing +// to diff against; a reject judged the whole approach, not lines. +// - a prior changes_requested with findings older than delta_max_age: +// full review. The flagged code has had time to move on and the +// reviewer's record of it is stale. +// - otherwise delta. With a known compare that is empty, or under +// delta_trivial_lines and touching no file the prior verdict flagged, the +// prior verdict is re-stamped instead of asked again. +func planDelta(pr PullRequest, artifact Artifact, opts DispatchOptions, now time.Time) (deltaMode, *DeltaContext, Aggregate) { + if !opts.DeltaRereview { + return deltaModeFull, nil, Aggregate{} + } + prior, ok := artifact.PriorAggregateFor(pr.Repo, pr.Number, pr.HeadSHA) + if !ok { + return deltaModeFull, nil, Aggregate{} + } + switch prior.Verdict { + case VerdictApprove, VerdictChangesRequested, VerdictRequiresHuman: + default: + return deltaModeFull, nil, Aggregate{} + } + if prior.Verdict == VerdictChangesRequested && len(prior.Findings) > 0 && !prior.RecordedAt.IsZero() && now.Sub(prior.RecordedAt) > opts.effectiveDeltaMaxAge() { + return deltaModeFull, nil, Aggregate{} + } + ctx := &DeltaContext{ + PreviousHead: prior.HeadSHA, + PreviousVerdict: prior.Verdict, + PreviousRecordedAt: prior.RecordedAt, + PreviousPerspectives: prior.Perspectives, + PreviousFindings: prior.Findings, + } + diff := pr.DeltaDiff + if diff == nil || !diff.Known || !SameHeadSHA(diff.PreviousHead, prior.HeadSHA) { + return deltaModeDelta, ctx, prior + } + ctx.DiffKnown = true + ctx.ChangedLines = diff.ChangedLines() + for _, f := range diff.Files { + ctx.Files = append(ctx.Files, f.Path) + } + sort.Strings(ctx.Files) + if deltaTrivial(*diff, prior, opts.effectiveDeltaTrivialLines()) { + return deltaModeTrivial, ctx, prior + } + return deltaModeDelta, ctx, prior +} + +// deltaTrivial reports whether the delta can carry the prior verdict without +// a review: an empty delta always can; a delta under threshold can when none +// of its files holds a prior finding, since a change in a file the reviewer +// flagged may be the fix (or not) and has to be read. +func deltaTrivial(diff DeltaDiff, prior Aggregate, threshold int) bool { + if !diff.Known { + return false + } + if diff.Identical() { + return true + } + if threshold <= 0 || diff.ChangedLines() >= threshold { + return false + } + flagged := map[string]bool{} + for _, f := range prior.Findings { + if p := strings.TrimSpace(f.Finding.File); p != "" { + flagged[p] = true + } + } + for _, f := range diff.Files { + if flagged[strings.TrimSpace(f.Path)] { + return false + } + } + return true +} + +// restampAggregate copies the prior verdict onto the new head. The verdict, +// perspectives, findings and confidence are the prior's; the head, body +// digest and timestamp are the new head's; Delta says why. +func restampAggregate(prior Aggregate, pr PullRequest, ctx DeltaContext, now time.Time) Aggregate { + agg := prior + agg.HeadSHA = pr.HeadSHA + agg.BodyDigest = pr.BodyDigest + agg.RecordedAt = now + note := fmt.Sprintf("re-stamped from %s: delta touches %d file(s), %d line(s), none previously flagged", shortSHA(ctx.PreviousHead), len(ctx.Files), ctx.ChangedLines) + if ctx.DiffKnown && len(ctx.Files) == 0 { + note = fmt.Sprintf("re-stamped from %s: identical tree", shortSHA(ctx.PreviousHead)) + } + agg.Delta = &DeltaReviewInfo{PreviousHead: ctx.PreviousHead, Trivial: true, ChangedLines: ctx.ChangedLines, Note: note} + agg.Reasons = append(append([]string(nil), prior.Reasons...), "delta re-review: "+note) + return agg +} + +func shortSHA(sha string) string { + sha = strings.TrimSpace(sha) + if len(sha) > 12 { + return sha[:12] + } + return sha +} + +// deltaSection is the prompt block for a delta re-review. It names the +// previous head and verdict, lists the previous findings for re-checking, +// and scopes the reading to the diff between the two heads. +func deltaSection(pr PullRequest, ctx *DeltaContext) string { + if ctx == nil || strings.TrimSpace(ctx.PreviousHead) == "" { + return "" + } + var b strings.Builder + b.WriteString("\nDELTA RE-REVIEW. ") + fmt.Fprintf(&b, "You (the hive) already reviewed this PR at head %s", ctx.PreviousHead) + if !ctx.PreviousRecordedAt.IsZero() { + fmt.Fprintf(&b, " on %s", ctx.PreviousRecordedAt.UTC().Format("2006-01-02 15:04 UTC")) + } + fmt.Fprintf(&b, "; the verdict was %s", ctx.PreviousVerdict) + if len(ctx.PreviousPerspectives) > 0 { + names := make([]string, 0, len(ctx.PreviousPerspectives)) + for p := range ctx.PreviousPerspectives { + names = append(names, string(p)) + } + sort.Strings(names) + parts := make([]string, 0, len(names)) + for _, n := range names { + parts = append(parts, fmt.Sprintf("%s=%s", n, ctx.PreviousPerspectives[Perspective(n)])) + } + fmt.Fprintf(&b, " (%s)", strings.Join(parts, ", ")) + } + b.WriteString(".\n") + b.WriteString("Review ONLY what changed since that head, not the whole PR again:\n") + fmt.Fprintf(&b, " gh api repos/%s/compare/%s...%s --jq '.files[] | \"\\(.status) \\(.filename) +\\(.additions) -\\(.deletions)\"'\n", pr.Repo, ctx.PreviousHead, pr.HeadSHA) + fmt.Fprintf(&b, " gh api repos/%s/compare/%s...%s -H 'Accept: application/vnd.github.diff'\n", pr.Repo, ctx.PreviousHead, pr.HeadSHA) + if ctx.DiffKnown { + fmt.Fprintf(&b, "The delta is %d changed line(s) across %d file(s)", ctx.ChangedLines, len(ctx.Files)) + if len(ctx.Files) > 0 { + fmt.Fprintf(&b, ": %s", strings.Join(ctx.Files, ", ")) + } + b.WriteString(".\n") + } + b.WriteString("Read the new code in those hunks and enough of their surroundings to judge them. Code outside the delta was judged at the previous head and stands unless the delta changes what it does.\n") + if len(ctx.PreviousFindings) > 0 { + b.WriteString("Re-check each finding from the previous review at the new head and say whether it is fixed, still present, or no longer applies:\n") + for _, f := range ctx.PreviousFindings { + loc := strings.TrimSpace(f.Finding.File) + if loc != "" && f.Finding.Line > 0 { + loc = fmt.Sprintf("%s:%d", loc, f.Finding.Line) + } + title := strings.TrimSpace(f.Finding.Title) + if title == "" { + title = strings.TrimSpace(f.Finding.Summary) + } + if loc == "" && title == "" { + continue + } + fmt.Fprintf(&b, " - [%s] %s %s\n", f.Perspective, loc, title) + } + } else { + b.WriteString("The previous review recorded no findings, so there is nothing to re-check beyond the delta itself.\n") + } + b.WriteString("Your verdict is for the WHOLE PR at the new head: a previous finding that is still present keeps its verdict; a clean delta on a previously approved PR is approve. Record it with the usual head_sha above.\n") + return b.String() +} + +// RecordDeltaRestamps writes the planner's re-stamped verdicts into the +// durable artifact at path ("" for the default), so the next cycle, the merge +// gate and the dashboard see the new head as judged. An existing entry for the +// same head and body digest is replaced; nothing else in the artifact moves. +func RecordDeltaRestamps(path string, restamps []DeltaRestamp, now time.Time) (int, error) { + if len(restamps) == 0 { + return 0, nil + } + if now.IsZero() { + now = time.Now().UTC() + } + target := path + if target == "" { + target = ReviewVerdictsPath + } + artifact, err := LoadArtifact(path) + if err != nil { + artifact = Artifact{} + } + written := 0 + for _, r := range restamps { + agg := r.Aggregate + if agg.RecordedAt.IsZero() { + agg.RecordedAt = now + } + key := reviewKey(agg.Repo, agg.Number, agg.HeadSHA, agg.BodyDigest) + replaced := false + for i, item := range artifact.Items { + if reviewKey(item.Repo, item.Number, item.HeadSHA, item.BodyDigest) == key { + artifact.Items[i] = agg + replaced = true + break + } + } + if !replaced { + artifact.Items = append(artifact.Items, agg) + } + written++ + } + artifact.GeneratedAt = now + if err := WriteArtifact(target, artifact); err != nil { + return 0, err + } + return written, nil +} diff --git a/src/pkg/review/delta_test.go b/src/pkg/review/delta_test.go new file mode 100644 index 0000000000..2e2ce055f2 --- /dev/null +++ b/src/pkg/review/delta_test.go @@ -0,0 +1,375 @@ +package review + +import ( + "path/filepath" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/outputschema" +) + +var deltaNow = time.Date(2026, 10, 28, 12, 0, 0, 0, time.UTC) + +func deltaOpts(agents ...AgentCapability) DispatchOptions { + o := swarmOpts(agents...) + o.DeltaRereview = true + o.Now = deltaNow + return o +} + +func priorAggregate(sha string, verdict Verdict, recorded time.Time, findings ...PerspectiveFinding) Aggregate { + return Aggregate{ + Repo: "acme/hive", Number: 7, HeadSHA: sha, Verdict: verdict, RecordedAt: recorded, + MergeEligible: verdict == VerdictApprove, + Perspectives: map[Perspective]Verdict{PerspectiveCorrectness: verdict, PerspectiveSecurity: VerdictApprove}, + Findings: findings, + } +} + +func flagged(file string, line int, title string) PerspectiveFinding { + return PerspectiveFinding{Perspective: PerspectiveCorrectness, Finding: outputschema.Finding{Title: title, Severity: outputschema.SeverityMedium, File: file, Line: line}} +} + +func knownDelta(prev string, files ...DeltaFile) *DeltaDiff { + return &DeltaDiff{PreviousHead: prev, Known: true, Files: files} +} + +func TestPriorAggregateForPicksLatestOtherHead(t *testing.T) { + art := Artifact{Items: []Aggregate{ + priorAggregate("sha1", VerdictChangesRequested, deltaNow.Add(-48*time.Hour)), + priorAggregate("sha2", VerdictApprove, deltaNow.Add(-2*time.Hour)), + priorAggregate("sha3", VerdictApprove, deltaNow), + {Repo: "acme/other", Number: 7, HeadSHA: "sha9", Verdict: VerdictApprove, RecordedAt: deltaNow}, + }} + got, ok := art.PriorAggregateFor("acme/hive", 7, "sha3") + if !ok || got.HeadSHA != "sha2" { + t.Fatalf("prior = %+v ok=%v, want sha2", got, ok) + } + if _, ok := art.PriorAggregateFor("acme/hive", 8, "sha3"); ok { + t.Fatal("unreviewed PR reported a prior aggregate") + } + one := Artifact{Items: []Aggregate{priorAggregate("sha1", VerdictApprove, deltaNow)}} + if _, ok := one.PriorAggregateFor("acme/hive", 7, "sha1"); ok { + t.Fatal("the current head counted as its own prior") + } +} + +func TestPlanDeltaFirstReviewIsFull(t *testing.T) { + pr := dispatchPR("sha1") + plan := PlanDispatch([]PullRequest{pr}, Artifact{}, DispatchState{}, deltaOpts(reviewer("r1"))) + if len(plan.ReviewKicks) != 1 || len(plan.DeltaRestamps) != 0 { + t.Fatalf("kicks=%d restamps=%d, want 1/0", len(plan.ReviewKicks), len(plan.DeltaRestamps)) + } + k := plan.ReviewKicks[0] + if k.DeltaPreviousHead != "" || strings.Contains(k.Message, "DELTA RE-REVIEW") { + t.Fatalf("first review carried delta context: %+v", k) + } + if plan.State.Pending[0].DeltaPreviousHead != "" { + t.Fatalf("pending stamped a previous head on a first review: %+v", plan.State.Pending[0]) + } +} + +func TestPlanDeltaReReviewPromptScopesToDiff(t *testing.T) { + pr := dispatchPR("sha2") + pr.DeltaDiff = knownDelta("sha1", DeltaFile{Path: "pkg/a/a.go", Additions: 30, Deletions: 4}, DeltaFile{Path: "pkg/b/b.go", Additions: 10}) + art := Artifact{Items: []Aggregate{priorAggregate("sha1", VerdictChangesRequested, deltaNow.Add(-time.Hour), flagged("pkg/a/a.go", 12, "nil deref on empty input"))}} + plan := PlanDispatch([]PullRequest{pr}, art, DispatchState{}, deltaOpts(reviewer("r1"))) + if len(plan.ReviewKicks) != 1 || len(plan.DeltaRestamps) != 0 { + t.Fatalf("kicks=%d restamps=%d, want 1/0", len(plan.ReviewKicks), len(plan.DeltaRestamps)) + } + k := plan.ReviewKicks[0] + if k.DeltaPreviousHead != "sha1" { + t.Fatalf("kick previous head = %q, want sha1", k.DeltaPreviousHead) + } + for _, want := range []string{ + "DELTA RE-REVIEW", + "already reviewed this PR at head sha1", + "verdict was changes_requested", + "correctness=changes_requested", + "compare/sha1...sha2", + "44 changed line(s) across 2 file(s): pkg/a/a.go, pkg/b/b.go", + "[correctness] pkg/a/a.go:12 nil deref on empty input", + "Review ONLY what changed since that head", + } { + if !strings.Contains(k.Message, want) { + t.Fatalf("delta prompt lacks %q:\n%s", want, k.Message) + } + } + if plan.State.Pending[0].DeltaPreviousHead != "sha1" { + t.Fatalf("pending did not record the previous head: %+v", plan.State.Pending[0]) + } +} + +func TestPlanDeltaUnknownDiffStillDeltaButNoRestamp(t *testing.T) { + pr := dispatchPR("sha2") + art := Artifact{Items: []Aggregate{priorAggregate("sha1", VerdictApprove, deltaNow.Add(-time.Hour))}} + for name, diff := range map[string]*DeltaDiff{ + "nil": nil, + "unknown": {PreviousHead: "sha1"}, + "wrong previous": knownDelta("sha0"), + } { + pr.DeltaDiff = diff + plan := PlanDispatch([]PullRequest{pr}, art, DispatchState{}, deltaOpts(reviewer("r1"))) + if len(plan.ReviewKicks) != 1 || len(plan.DeltaRestamps) != 0 { + t.Fatalf("%s: kicks=%d restamps=%d, want 1/0", name, len(plan.ReviewKicks), len(plan.DeltaRestamps)) + } + p := plan.ReviewKicks[0].Message + if !strings.Contains(p, "DELTA RE-REVIEW") || strings.Contains(p, "changed line(s) across") { + t.Fatalf("%s: unknown diff prompt wrong:\n%s", name, p) + } + if !strings.Contains(p, "recorded no findings") { + t.Fatalf("%s: prompt lacks the no-findings note:\n%s", name, p) + } + } +} + +func TestPlanDeltaTrivialSkipRestampsWithoutKick(t *testing.T) { + prior := priorAggregate("sha1", VerdictApprove, deltaNow.Add(-time.Hour)) + prior.Reasons = []string{"all perspectives approved"} + art := Artifact{Items: []Aggregate{prior}} + cases := []struct { + name string + diff *DeltaDiff + wantNote string + }{ + {"identical tree", knownDelta("sha1"), "identical tree"}, + {"under threshold", knownDelta("sha1", DeltaFile{Path: "README.md", Additions: 3, Deletions: 1}), "delta touches 1 file(s), 4 line(s)"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + pr := dispatchPR("sha2") + pr.BodyDigest = "body2" + pr.DeltaDiff = tc.diff + plan := PlanDispatch([]PullRequest{pr}, art, DispatchState{}, deltaOpts(reviewer("r1"))) + if len(plan.ReviewKicks) != 0 || len(plan.State.Pending) != 0 { + t.Fatalf("trivial delta still dispatched: kicks=%d pending=%d", len(plan.ReviewKicks), len(plan.State.Pending)) + } + if len(plan.DeltaRestamps) != 1 { + t.Fatalf("restamps = %+v, want one", plan.DeltaRestamps) + } + r := plan.DeltaRestamps[0] + if r.Repo != "acme/hive" || r.Number != 7 || r.HeadSHA != "sha2" || r.PreviousHead != "sha1" { + t.Fatalf("restamp keyed wrong: %+v", r) + } + agg := r.Aggregate + if agg.HeadSHA != "sha2" || agg.BodyDigest != "body2" || agg.Verdict != VerdictApprove || !agg.MergeEligible || !agg.RecordedAt.Equal(deltaNow) { + t.Fatalf("restamped aggregate = %+v", agg) + } + if agg.Delta == nil || !agg.Delta.Trivial || agg.Delta.PreviousHead != "sha1" || !strings.Contains(agg.Delta.Note, tc.wantNote) { + t.Fatalf("delta info = %+v, want trivial from sha1 with %q", agg.Delta, tc.wantNote) + } + if len(agg.Reasons) != 2 || agg.Reasons[0] != "all perspectives approved" || !strings.HasPrefix(agg.Reasons[1], "delta re-review: ") { + t.Fatalf("reasons = %v", agg.Reasons) + } + }) + } +} + +func TestPlanDeltaTrivialNotTakenWhenFlaggedFileOrOverThreshold(t *testing.T) { + art := Artifact{Items: []Aggregate{priorAggregate("sha1", VerdictChangesRequested, deltaNow.Add(-time.Hour), flagged("pkg/a/a.go", 12, "bug"))}} + cases := map[string]*DeltaDiff{ + "flagged file touched": knownDelta("sha1", DeltaFile{Path: "pkg/a/a.go", Additions: 1}), + "at threshold": knownDelta("sha1", DeltaFile{Path: "docs/x.md", Additions: 20}), + } + for name, diff := range cases { + pr := dispatchPR("sha2") + pr.DeltaDiff = diff + plan := PlanDispatch([]PullRequest{pr}, art, DispatchState{}, deltaOpts(reviewer("r1"))) + if len(plan.ReviewKicks) != 1 || len(plan.DeltaRestamps) != 0 { + t.Fatalf("%s: kicks=%d restamps=%d, want 1/0", name, len(plan.ReviewKicks), len(plan.DeltaRestamps)) + } + if plan.ReviewKicks[0].DeltaPreviousHead != "sha1" { + t.Fatalf("%s: not a delta kick: %+v", name, plan.ReviewKicks[0]) + } + } + // A negative threshold disables the trivial skip even for a one-line delta. + pr := dispatchPR("sha2") + pr.DeltaDiff = knownDelta("sha1", DeltaFile{Path: "docs/x.md", Additions: 1}) + opts := deltaOpts(reviewer("r1")) + opts.DeltaTrivialLines = -1 + plan := PlanDispatch([]PullRequest{pr}, art, DispatchState{}, opts) + if len(plan.ReviewKicks) != 1 || len(plan.DeltaRestamps) != 0 { + t.Fatalf("disabled threshold: kicks=%d restamps=%d, want 1/0", len(plan.ReviewKicks), len(plan.DeltaRestamps)) + } + // Identical trees always re-stamp, threshold or not. + pr.DeltaDiff = knownDelta("sha1") + plan = PlanDispatch([]PullRequest{pr}, art, DispatchState{}, opts) + if len(plan.ReviewKicks) != 0 || len(plan.DeltaRestamps) != 1 { + t.Fatalf("identical with disabled threshold: kicks=%d restamps=%d, want 0/1", len(plan.ReviewKicks), len(plan.DeltaRestamps)) + } +} + +func TestPlanDeltaTrivialSkipNotWhilePending(t *testing.T) { + art := Artifact{Items: []Aggregate{priorAggregate("sha1", VerdictApprove, deltaNow.Add(-time.Hour))}} + pr := dispatchPR("sha2") + pr.DeltaDiff = knownDelta("sha1") + state := DispatchState{Pending: []PendingReview{{Repo: "acme/hive", Number: 7, HeadSHA: "sha2", Perspective: PerspectiveCorrectness, Agent: "r1", Dispatched: deltaNow.Add(-time.Minute)}}} + plan := PlanDispatch([]PullRequest{pr}, art, state, deltaOpts(reviewer("r1"))) + if len(plan.DeltaRestamps) != 0 { + t.Fatalf("restamped a head with a review in flight: %+v", plan.DeltaRestamps) + } +} + +func TestPlanDeltaFallsBackToFullReview(t *testing.T) { + fresh := deltaNow.Add(-time.Hour) + stale := deltaNow.Add(-DefaultDeltaMaxAge - time.Hour) + cases := []struct { + name string + prior Aggregate + opts func(DispatchOptions) DispatchOptions + }{ + {"option off", priorAggregate("sha1", VerdictApprove, fresh), func(o DispatchOptions) DispatchOptions { o.DeltaRereview = false; return o }}, + {"prior reject", priorAggregate("sha1", VerdictReject, fresh), nil}, + {"prior incomplete", priorAggregate("sha1", VerdictIncomplete, fresh), nil}, + {"stale changes_requested with findings", priorAggregate("sha1", VerdictChangesRequested, stale, flagged("pkg/a/a.go", 1, "bug")), nil}, + {"custom max age exceeded", priorAggregate("sha1", VerdictChangesRequested, deltaNow.Add(-3*time.Hour), flagged("pkg/a/a.go", 1, "bug")), func(o DispatchOptions) DispatchOptions { o.DeltaMaxAge = 2 * time.Hour; return o }}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + pr := dispatchPR("sha2") + pr.DeltaDiff = knownDelta("sha1") + opts := deltaOpts(reviewer("r1")) + if tc.opts != nil { + opts = tc.opts(opts) + } + plan := PlanDispatch([]PullRequest{pr}, Artifact{Items: []Aggregate{tc.prior}}, DispatchState{}, opts) + if len(plan.ReviewKicks) != 1 || len(plan.DeltaRestamps) != 0 { + t.Fatalf("kicks=%d restamps=%d, want a full review", len(plan.ReviewKicks), len(plan.DeltaRestamps)) + } + k := plan.ReviewKicks[0] + if k.DeltaPreviousHead != "" || strings.Contains(k.Message, "DELTA RE-REVIEW") { + t.Fatalf("expected full review, got delta: %+v", k) + } + }) + } + // A stale changes_requested WITHOUT findings has nothing to go stale, so + // it still delta-reviews; a stale approve likewise. + for _, prior := range []Aggregate{priorAggregate("sha1", VerdictChangesRequested, stale), priorAggregate("sha1", VerdictApprove, stale)} { + pr := dispatchPR("sha2") + pr.DeltaDiff = &DeltaDiff{PreviousHead: "sha1"} + plan := PlanDispatch([]PullRequest{pr}, Artifact{Items: []Aggregate{prior}}, DispatchState{}, deltaOpts(reviewer("r1"))) + if len(plan.ReviewKicks) != 1 || plan.ReviewKicks[0].DeltaPreviousHead != "sha1" { + t.Fatalf("stale %s without findings did not delta-review: %+v", prior.Verdict, plan.ReviewKicks) + } + } +} + +func TestPlanDeltaSameHeadRevisitIsNotDelta(t *testing.T) { + // A verdict at this very head means the PR is judged; the delta machinery + // must not treat an older head's verdict as a prior to diff against. + art := Artifact{Items: []Aggregate{ + priorAggregate("sha1", VerdictApprove, deltaNow.Add(-2*time.Hour)), + priorAggregate("sha2", VerdictApprove, deltaNow.Add(-time.Hour)), + }} + pr := dispatchPR("sha2") + pr.DeltaDiff = knownDelta("sha1") + plan := PlanDispatch([]PullRequest{pr}, art, DispatchState{}, deltaOpts(reviewer("r1"))) + if len(plan.ReviewKicks) != 0 || len(plan.DeltaRestamps) != 0 { + t.Fatalf("judged head re-planned: kicks=%d restamps=%d", len(plan.ReviewKicks), len(plan.DeltaRestamps)) + } +} + +func TestDeltaSectionInBothPromptBuilders(t *testing.T) { + pr := dispatchPR("sha2") + pr.Repo = "acme/hive" + ctx := &DeltaContext{PreviousHead: "sha1", PreviousVerdict: VerdictApprove, PreviousRecordedAt: deltaNow.Add(-time.Hour), DiffKnown: true, Files: []string{"a.go"}, ChangedLines: 25} + single := BuildPerspectivePromptWith(PerspectiveCorrectness, pr, PromptOptions{Delta: ctx}) + combined := BuildCombinedPrompt(pr, DefaultPerspectives, PromptOptions{Delta: ctx}) + for name, p := range map[string]string{"perspective": single, "combined": combined} { + if !strings.Contains(p, "DELTA RE-REVIEW") || !strings.Contains(p, "repos/acme/hive/compare/sha1...sha2") || !strings.Contains(p, "2026-10-28 11:00 UTC") { + t.Fatalf("%s prompt lacks delta section:\n%s", name, p) + } + } + if s := deltaSection(pr, nil); s != "" { + t.Fatalf("nil context produced a section: %q", s) + } + if s := deltaSection(pr, &DeltaContext{}); s != "" { + t.Fatalf("empty previous head produced a section: %q", s) + } +} + +func TestDeltaTrivialRules(t *testing.T) { + prior := Aggregate{Findings: []PerspectiveFinding{flagged("pkg/a/a.go", 1, "bug")}} + if !deltaTrivial(DeltaDiff{Known: true}, prior, 20) { + t.Fatal("identical known delta not trivial") + } + if deltaTrivial(DeltaDiff{}, prior, 20) { + t.Fatal("unknown delta counted as trivial") + } + if !deltaTrivial(DeltaDiff{Known: true, Files: []DeltaFile{{Path: "x.go", Additions: 19}}}, prior, 20) { + t.Fatal("19 lines in an unflagged file not trivial") + } + if deltaTrivial(DeltaDiff{Known: true, Files: []DeltaFile{{Path: "x.go", Additions: 20}}}, prior, 20) { + t.Fatal("20 lines at threshold counted as trivial") + } + if deltaTrivial(DeltaDiff{Known: true, Files: []DeltaFile{{Path: " pkg/a/a.go ", Additions: 1}}}, prior, 20) { + t.Fatal("flagged file (with whitespace) counted as trivial") + } + if deltaTrivial(DeltaDiff{Known: true, Files: []DeltaFile{{Path: "x.go", Additions: 1}}}, prior, 0) { + t.Fatal("zero threshold allowed a non-empty trivial delta") + } + d := DeltaDiff{Known: true, Files: []DeltaFile{{Path: "a", Additions: 2, Deletions: 3}, {Path: "b", Deletions: 4}}} + if d.ChangedLines() != 9 || d.Identical() { + t.Fatalf("ChangedLines=%d Identical=%v", d.ChangedLines(), d.Identical()) + } +} + +func TestModelStampsCarryDeltaPreviousHead(t *testing.T) { + stamps := ModelStampsFromState(DispatchState{Pending: []PendingReview{{Repo: "acme/hive", Number: 7, HeadSHA: "sha2", Perspective: PerspectiveCorrectness, Agent: "r1", DeltaPreviousHead: "sha1"}}}) + r := PerspectiveReport{Repo: "acme/hive", Number: 7, HeadSHA: "sha2", Perspective: PerspectiveCorrectness, Verdict: VerdictApprove} + stamps.apply(&r) + if r.DeltaPreviousHead != "sha1" { + t.Fatalf("report delta previous head = %q, want sha1", r.DeltaPreviousHead) + } + kept := PerspectiveReport{Repo: "acme/hive", Number: 7, HeadSHA: "sha2", Perspective: PerspectiveCorrectness, DeltaPreviousHead: "sha0"} + stamps.apply(&kept) + if kept.DeltaPreviousHead != "sha0" { + t.Fatalf("stamp overwrote the report's own value: %q", kept.DeltaPreviousHead) + } + agg := aggregateReports([]PerspectiveReport{ + r, + {Repo: "acme/hive", Number: 7, HeadSHA: "sha2", Perspective: PerspectiveSecurity, Verdict: VerdictApprove}, + }, AggregateOptions{}) + if agg.Delta == nil || agg.Delta.PreviousHead != "sha1" || agg.Delta.Trivial { + t.Fatalf("aggregate delta = %+v, want non-trivial from sha1", agg.Delta) + } + plain := aggregateReports([]PerspectiveReport{{Repo: "acme/hive", Number: 7, HeadSHA: "sha2", Perspective: PerspectiveSecurity, Verdict: VerdictApprove}}, AggregateOptions{}) + if plain.Delta != nil { + t.Fatalf("full review aggregate carries delta info: %+v", plain.Delta) + } +} + +func TestRecordDeltaRestampsRoundTrip(t *testing.T) { + path := filepath.Join(t.TempDir(), "review-verdicts.json") + existing := Artifact{Items: []Aggregate{priorAggregate("sha1", VerdictApprove, deltaNow.Add(-time.Hour))}} + if err := WriteArtifact(path, existing); err != nil { + t.Fatal(err) + } + if n, err := RecordDeltaRestamps(path, nil, deltaNow); err != nil || n != 0 { + t.Fatalf("empty restamps: n=%d err=%v", n, err) + } + stamped := restampAggregate(existing.Items[0], PullRequest{Repo: "acme/hive", Number: 7, HeadSHA: "sha2", BodyDigest: "b"}, DeltaContext{PreviousHead: "sha1", DiffKnown: true}, deltaNow) + restamp := DeltaRestamp{Repo: "acme/hive", Number: 7, HeadSHA: "sha2", PreviousHead: "sha1", Aggregate: stamped} + if n, err := RecordDeltaRestamps(path, []DeltaRestamp{restamp}, deltaNow); err != nil || n != 1 { + t.Fatalf("record: n=%d err=%v", n, err) + } + // Recording the same head again replaces rather than duplicates. + if _, err := RecordDeltaRestamps(path, []DeltaRestamp{restamp}, deltaNow.Add(time.Minute)); err != nil { + t.Fatal(err) + } + art, err := LoadArtifact(path) + if err != nil { + t.Fatal(err) + } + if len(art.Items) != 2 { + t.Fatalf("items = %d, want prior + restamp", len(art.Items)) + } + got, ok := art.AggregateForBody("acme/hive", 7, "sha2", "b") + if !ok || got.Delta == nil || !got.Delta.Trivial || got.Delta.PreviousHead != "sha1" || got.Verdict != VerdictApprove { + t.Fatalf("restamped verdict did not round-trip: %+v ok=%v", got, ok) + } + if !strings.Contains(got.Delta.Note, "identical tree") { + t.Fatalf("note = %q", got.Delta.Note) + } +} diff --git a/src/pkg/review/dispatch.go b/src/pkg/review/dispatch.go index ed7b66ecf1..fff6c9cd43 100644 --- a/src/pkg/review/dispatch.go +++ b/src/pkg/review/dispatch.go @@ -236,8 +236,18 @@ type DispatchOptions struct { // perspective is re-sent (review.head_max_retries). Zero means // MaxRedispatch. HeadMaxRetries int - Agents []AgentCapability - Now time.Time + // DeltaRereview is review.delta_rereview: a PR with a verdict at an + // earlier head is re-reviewed against the diff between that head and the + // current one (planDelta), and a trivial delta re-stamps the earlier + // verdict instead of running a session. DeltaTrivialLines is the + // threshold for that skip (0 means DefaultDeltaTrivialLines, negative + // disables it); DeltaMaxAge is how old a changes_requested verdict with + // findings may be and still anchor a delta (0 means DefaultDeltaMaxAge). + DeltaRereview bool + DeltaTrivialLines int + DeltaMaxAge time.Duration + Agents []AgentCapability + Now time.Time } type DispatchState struct { @@ -330,25 +340,30 @@ type PendingReview struct { // the PR's attribution-trailer model, and the model the kick told the // reviewer to run with (empty when no pool applied). Collect uses them // to stamp a verdict whose report omitted either (ModelStamps). - AuthorModel string `json:"author_model,omitempty"` - ReviewModel string `json:"review_model,omitempty"` - Dispatched time.Time `json:"dispatched_at"` + AuthorModel string `json:"author_model,omitempty"` + ReviewModel string `json:"review_model,omitempty"` + // DeltaPreviousHead is the head this dispatch was a delta re-review + // against (review.delta_rereview); empty for a full review. Collect + // stamps it onto the verdict (ModelStamps) so the artifact says so. + DeltaPreviousHead string `json:"delta_previous_head,omitempty"` + Dispatched time.Time `json:"dispatched_at"` } type RecentReview struct { - Repo string `json:"repo"` - Number int `json:"number"` - HeadSHA string `json:"head_sha,omitempty"` - BodyDigest string `json:"body_digest,omitempty"` - Perspective Perspective `json:"perspective"` - Agent string `json:"agent"` - Head string `json:"head,omitempty"` - Decision bool `json:"decision,omitempty"` - AuthorAgent string `json:"author_agent,omitempty"` - AuthorModel string `json:"author_model,omitempty"` - ReviewModel string `json:"review_model,omitempty"` - Dispatched time.Time `json:"dispatched_at,omitempty"` - Confirmed time.Time `json:"confirmed_at"` + Repo string `json:"repo"` + Number int `json:"number"` + HeadSHA string `json:"head_sha,omitempty"` + BodyDigest string `json:"body_digest,omitempty"` + Perspective Perspective `json:"perspective"` + Agent string `json:"agent"` + Head string `json:"head,omitempty"` + Decision bool `json:"decision,omitempty"` + AuthorAgent string `json:"author_agent,omitempty"` + AuthorModel string `json:"author_model,omitempty"` + ReviewModel string `json:"review_model,omitempty"` + DeltaPreviousHead string `json:"delta_previous_head,omitempty"` + Dispatched time.Time `json:"dispatched_at,omitempty"` + Confirmed time.Time `json:"confirmed_at"` } type PendingFix struct { @@ -408,6 +423,9 @@ type DispatchKick struct { // BotAuthorRouted is true when ReviewModel came from review.bot_author_model // because the PR's author is a listed bot. BotAuthorRouted bool + // DeltaPreviousHead is set when this kick is a delta re-review against + // that head (review.delta_rereview); empty for a full review. + DeltaPreviousHead string } // SkippedReviewModel is a perspective kick the planner withheld because the @@ -448,7 +466,12 @@ type DispatchPlan struct { // heads the review-heads skip rule settled (every head clean), when // review.acknowledge_no_findings is on. The caller posts them. Acknowledgements []ReviewAcknowledgement - State DispatchState + // DeltaRestamps are verdicts carried from an earlier head onto the + // current head without a session this cycle (review.delta_rereview, + // trivial delta). The caller records them in the verdict artifact + // (RecordDeltaRestamps) and logs each one. + DeltaRestamps []DeltaRestamp + State DispatchState } type FixDispatchExclusion struct { @@ -644,12 +667,32 @@ func PlanDispatch(prs []PullRequest, artifact Artifact, state DispatchState, opt } plan.expirePending(pr, opts, now) } + // Delta re-review: a head whose PR was judged at an earlier head is + // reviewed against the diff from that head, and a trivial delta + // carries the earlier verdict forward without a session. Not on a + // same-head revisit (the prior verdict is the one being redone) and + // not once this head already has dispatch entries (a review is in + // flight or partly delivered; the restamp would race it). + var delta *DeltaContext + if !revisiting && delivered == nil { + mode, ctx, prior := planDelta(pr, artifact, opts, now) + switch mode { + case deltaModeTrivial: + if !plan.hasPendingForHead(pr) { + plan.DeltaRestamps = append(plan.DeltaRestamps, DeltaRestamp{Repo: pr.Repo, Number: pr.Number, HeadSHA: pr.HeadSHA, PreviousHead: ctx.PreviousHead, Aggregate: restampAggregate(prior, pr, *ctx, now)}) + continue + } + delta = ctx + case deltaModeDelta: + delta = ctx + } + } if availableSlots <= 0 { continue } authorBase := prAuthorBase(pr, opts.Agents) if prHeads := reviewersForPR(heads, pr.AuthorAgent, authorBase); len(prHeads) > 0 { - availableSlots = plan.dispatchHeads(pr, prHeads, opts, delivered, deliveredHeads, revisiting, availableSlots, now) + availableSlots = plan.dispatchHeads(pr, prHeads, opts, delivered, deliveredHeads, revisiting, delta, availableSlots, now) // Once every head has settled, the base reviews the heads' // reports and records the decided verdict (#13542). if awaiting != nil { @@ -707,10 +750,11 @@ func PlanDispatch(prs []PullRequest, artifact Artifact, state DispatchState, opt ReviewBotMinPriority: opts.ReviewBotMinPriority, ReviewBackend: choice.Backend, ReviewModel: choice.Model, + Delta: delta, }) - 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}) + 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, DeltaPreviousHead: deltaPreviousHead(delta)}) 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}) + 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), DeltaPreviousHead: deltaPreviousHead(delta), Dispatched: now}) } availableSlots-- continue @@ -773,15 +817,36 @@ func PlanDispatch(prs []PullRequest, artifact Artifact, state DispatchState, opt ReviewBotMinPriority: opts.ReviewBotMinPriority, ReviewBackend: choice.Backend, ReviewModel: choice.Model, + Delta: delta, }) - 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}) + 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, DeltaPreviousHead: deltaPreviousHead(delta)}) + 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), DeltaPreviousHead: deltaPreviousHead(delta), Dispatched: now}) availableSlots-- } } return plan } +// deltaPreviousHead is the head a delta kick is judged against, "" for a +// full review. +func deltaPreviousHead(d *DeltaContext) string { + if d == nil { + return "" + } + return strings.TrimSpace(d.PreviousHead) +} + +// hasPendingForHead reports whether any dispatch entry exists for this PR +// head: a review in flight, or one already answered in part. +func (p *DispatchPlan) hasPendingForHead(pr PullRequest) bool { + for _, e := range p.State.Pending { + if samePRHead(e.Repo, e.Number, e.HeadSHA, pr.Repo, pr.Number, pr.HeadSHA) { + return true + } + } + return false +} + // stampedReviewModel is the model a pending review is attributed to when its // report arrives without one: the chosen pool model, else the reviewer's own // launch model — the same fallback pkg/github's evidence writer applies. @@ -1292,7 +1357,7 @@ func ConfirmDelivered(state DispatchState, planned, delivered []DispatchKick) Di // first perspective alone would leave every other verdict in the // array with nothing to bind to once its pending entry is gone. for _, p := range kickPerspectives(k) { - state.Recent = upsertRecentReview(state.Recent, RecentReview{Repo: k.Repo, Number: k.Number, HeadSHA: k.HeadSHA, BodyDigest: k.BodyDigest, Perspective: p, Agent: k.Agent, Head: k.Head, Decision: k.Decision, AuthorAgent: k.AuthorAgent, AuthorModel: k.AuthorModel, ReviewModel: k.ReviewModel, Confirmed: now}) + state.Recent = upsertRecentReview(state.Recent, RecentReview{Repo: k.Repo, Number: k.Number, HeadSHA: k.HeadSHA, BodyDigest: k.BodyDigest, Perspective: p, Agent: k.Agent, Head: k.Head, Decision: k.Decision, AuthorAgent: k.AuthorAgent, AuthorModel: k.AuthorModel, ReviewModel: k.ReviewModel, DeltaPreviousHead: k.DeltaPreviousHead, Confirmed: now}) } } } diff --git a/src/pkg/review/model_stamps.go b/src/pkg/review/model_stamps.go index e49a94de33..756dc2336e 100644 --- a/src/pkg/review/model_stamps.go +++ b/src/pkg/review/model_stamps.go @@ -9,6 +9,9 @@ import "strings" type ModelStamp struct { AuthorModel string ReviewModels map[perspectiveStampKey]string + // DeltaPreviousHead is the head a delta re-review of this PR head was + // judged against (review.delta_rereview); empty for a full review. + DeltaPreviousHead string } // perspectiveStampKey is one review_model stamp's slot: the perspective, and @@ -32,10 +35,10 @@ type ModelStamps map[string]ModelStamp func ModelStampsFromState(state DispatchState) ModelStamps { out := ModelStamps{} for _, r := range state.Recent { - out.record(r.Repo, r.Number, r.HeadSHA, r.Perspective, r.Head, r.AuthorModel, r.ReviewModel) + out.record(r.Repo, r.Number, r.HeadSHA, r.Perspective, r.Head, r.AuthorModel, r.ReviewModel, r.DeltaPreviousHead) } for _, p := range state.Pending { - out.record(p.Repo, p.Number, p.HeadSHA, p.Perspective, p.Head, p.AuthorModel, p.ReviewModel) + out.record(p.Repo, p.Number, p.HeadSHA, p.Perspective, p.Head, p.AuthorModel, p.ReviewModel, p.DeltaPreviousHead) } if len(out) == 0 { return nil @@ -43,9 +46,9 @@ func ModelStampsFromState(state DispatchState) ModelStamps { return out } -func (m ModelStamps) record(repo string, number int, head string, perspective Perspective, reviewHead, authorModel, reviewModel string) { - authorModel, reviewModel = strings.TrimSpace(authorModel), strings.TrimSpace(reviewModel) - if authorModel == "" && reviewModel == "" { +func (m ModelStamps) record(repo string, number int, head string, perspective Perspective, reviewHead, authorModel, reviewModel, deltaPreviousHead string) { + authorModel, reviewModel, deltaPreviousHead = strings.TrimSpace(authorModel), strings.TrimSpace(reviewModel), strings.TrimSpace(deltaPreviousHead) + if authorModel == "" && reviewModel == "" && deltaPreviousHead == "" { return } key := reviewKey(repo, number, head, "") @@ -53,6 +56,9 @@ func (m ModelStamps) record(repo string, number int, head string, perspective Pe if authorModel != "" { stamp.AuthorModel = authorModel } + if deltaPreviousHead != "" { + stamp.DeltaPreviousHead = deltaPreviousHead + } if reviewModel != "" { if stamp.ReviewModels == nil { stamp.ReviewModels = map[perspectiveStampKey]string{} @@ -80,4 +86,7 @@ func (m ModelStamps) apply(report *PerspectiveReport) { if strings.TrimSpace(report.ReviewModel) == "" { report.ReviewModel = stamp.ReviewModels[perspectiveStampKey{report.Perspective, strings.TrimSpace(report.Head)}] } + if strings.TrimSpace(report.DeltaPreviousHead) == "" { + report.DeltaPreviousHead = stamp.DeltaPreviousHead + } } diff --git a/src/pkg/review/prompts.go b/src/pkg/review/prompts.go index 7a99460632..95988577f8 100644 --- a/src/pkg/review/prompts.go +++ b/src/pkg/review/prompts.go @@ -82,6 +82,11 @@ type PullRequest struct { // reviews nothing on that head. It is the maintainer's text, not the // author's rationale, so it is allowed into the fix prompt verbatim. HumanChangeRequest *HumanChangeRequest + // DeltaDiff is the compare between the head the hive last judged and + // HeadSHA, fetched by the caller when review.delta_rereview is on and the + // PR has a prior verdict at another head. nil means not fetched: the + // reviewer still gets the delta instruction, but no trivial skip. + DeltaDiff *DeltaDiff } func PullRequestBodyDigest(title, body string) string { @@ -266,6 +271,11 @@ type PromptOptions struct { // posting anything on the PR and must not read other reviews of this // head; PostComments and AcknowledgeNoFindings are ignored. HeadRecordOnly string + // Delta is set when this kick is a delta re-review (review.delta_rereview): + // the PR already has a verdict at an earlier head, and the reviewer is + // to read the diff between that head and this one, re-check the earlier + // findings, and leave the rest of the PR as judged. nil is a full review. + Delta *DeltaContext } // BuildPerspectivePromptWith is BuildPerspectivePromptOpts with the full set @@ -296,6 +306,7 @@ func BuildPerspectivePromptWith(p Perspective, pr PullRequest, opts PromptOption writeIndependentReviewSection(&b, opts) b.WriteString(scopeContractSection(pr)) b.WriteString(buildReadInstruction(pr)) + b.WriteString(deltaSection(pr, opts.Delta)) b.WriteString(buildReviewBotFindingsInstruction(pr, opts.ReviewBotLogins, opts.ReviewBotMinPriority)) b.WriteString(groundingSection(pr)) if p == PerspectivePlanMatch { @@ -623,6 +634,7 @@ func BuildCombinedPrompt(pr PullRequest, perspectives []Perspective, opts Prompt b.WriteString(combinedSharedHeader) b.WriteString(scopeContractSection(pr)) b.WriteString(buildReadInstruction(pr)) + b.WriteString(deltaSection(pr, opts.Delta)) b.WriteString(buildReviewBotFindingsInstruction(pr, opts.ReviewBotLogins, opts.ReviewBotMinPriority)) b.WriteString(groundingSectionWithBudget(pr, combinedGroundingBudget(len(perspectives)), fmt.Sprintf("this whole review, across all %d perspectives", len(perspectives)))) writeReviewWritingGuide(&b, opts) diff --git a/src/pkg/review/review.go b/src/pkg/review/review.go index 6c3b0979d9..dd838f84bd 100644 --- a/src/pkg/review/review.go +++ b/src/pkg/review/review.go @@ -173,6 +173,11 @@ type PerspectiveReport struct { // "permission"/"privilege" escalation terms) for this report; every other // risk class still parks the PR (hivecommons/hive#13068). PermissionsTighteningOnly bool `json:"permissions_tightening_only,omitempty"` + // DeltaPreviousHead is the head SHA this report was judged as a delta + // against (review.delta_rereview), stamped by the collector from the + // dispatch record (ModelStamps), never from the reviewer's JSON. Empty + // for a full review. + DeltaPreviousHead string `json:"delta_previous_head,omitempty"` } type AggregateOptions struct { @@ -264,6 +269,11 @@ type Aggregate struct { // PermissionsTighteningOnly mirrors the reports' flag so Normalize judges // risk escalation with the same carve-out AggregateReports did. PermissionsTighteningOnly bool `json:"permissions_tightening_only,omitempty"` + // Delta is set when this verdict came from a delta re-review against an + // earlier head (review.delta_rereview): either a session that read only + // the diff between the two heads, or (Trivial) the earlier verdict + // re-stamped without a session. nil is a full review. + Delta *DeltaReviewInfo `json:"delta,omitempty"` } type PerspectiveFinding struct { @@ -398,6 +408,9 @@ func aggregateReports(reports []PerspectiveReport, opts AggregateOptions) Aggreg if agg.ReviewModel == "" { agg.ReviewModel = r.ReviewModel } + if agg.Delta == nil && strings.TrimSpace(r.DeltaPreviousHead) != "" { + agg.Delta = &DeltaReviewInfo{PreviousHead: strings.TrimSpace(r.DeltaPreviousHead)} + } recordAggregateHead(&agg, r) if r.NotApplicable { // Nothing to judge is not a judgement withheld. Findings on a diff --git a/src/pkg/review/review_heads.go b/src/pkg/review/review_heads.go index db37b120f6..b91b9138e7 100644 --- a/src/pkg/review/review_heads.go +++ b/src/pkg/review/review_heads.go @@ -131,7 +131,7 @@ func pendingMissingHeadPerspectives(state DispatchState, pr PullRequest, perspec // // Heads run with their own backend and model, so no review_models pool or // perspective_models override applies: the head IS the model. -func (p *DispatchPlan) dispatchHeads(pr PullRequest, reviewers []AgentCapability, opts DispatchOptions, delivered map[Perspective]Verdict, deliveredHeads map[Perspective][]string, revisiting bool, availableSlots int, now time.Time) int { +func (p *DispatchPlan) dispatchHeads(pr PullRequest, reviewers []AgentCapability, opts DispatchOptions, delivered map[Perspective]Verdict, deliveredHeads map[Perspective][]string, revisiting bool, delta *DeltaContext, availableSlots int, now time.Time) int { keys, groups := headGroups(reviewers) if len(keys) == 0 { return availableSlots @@ -179,9 +179,10 @@ func (p *DispatchPlan) dispatchHeads(pr PullRequest, reviewers []AgentCapability ReviewBotMinPriority: opts.ReviewBotMinPriority, ReviewBackend: reviewer.Backend, ReviewModel: reviewer.Model, + Delta: delta, }) - p.ReviewKicks = append(p.ReviewKicks, DispatchKick{Agent: reviewer.Name, 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: m.Perspective, Head: m.Head, AuthorAgent: pr.AuthorAgent, AuthorModel: pr.AuthorModel, ReviewBackend: reviewer.Backend, ReviewModel: reviewer.Model}) - p.State.Pending = append(p.State.Pending, PendingReview{Repo: pr.Repo, Number: pr.Number, HeadSHA: pr.HeadSHA, BodyDigest: pr.BodyDigest, Perspective: m.Perspective, Agent: reviewer.Name, Head: m.Head, AuthorAgent: pr.AuthorAgent, AuthorModel: pr.AuthorModel, ReviewModel: strings.TrimSpace(reviewer.Model), Dispatched: now}) + p.ReviewKicks = append(p.ReviewKicks, DispatchKick{Agent: reviewer.Name, 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: m.Perspective, Head: m.Head, AuthorAgent: pr.AuthorAgent, AuthorModel: pr.AuthorModel, ReviewBackend: reviewer.Backend, ReviewModel: reviewer.Model, DeltaPreviousHead: deltaPreviousHead(delta)}) + p.State.Pending = append(p.State.Pending, PendingReview{Repo: pr.Repo, Number: pr.Number, HeadSHA: pr.HeadSHA, BodyDigest: pr.BodyDigest, Perspective: m.Perspective, Agent: reviewer.Name, Head: m.Head, AuthorAgent: pr.AuthorAgent, AuthorModel: pr.AuthorModel, ReviewModel: strings.TrimSpace(reviewer.Model), DeltaPreviousHead: deltaPreviousHead(delta), Dispatched: now}) availableSlots-- } return availableSlots