diff --git a/changelog.d/added-review-verdict-survives-base-sync.md b/changelog.d/added-review-verdict-survives-base-sync.md new file mode 100644 index 0000000000..d911db1334 --- /dev/null +++ b/changelog.d/added-review-verdict-survives-base-sync.md @@ -0,0 +1 @@ +- **An approved PR keeps its swarm approve when the hive merges the base into it.** The hive's own update-branch (or merge-forward) gave an approved PR a new head with no verdict, so it fell back to `review-gate:no-review-bundle` and waited for a reviewer to read base code already reviewed and merged upstream (projectbluefin/fsdk-containers#482, projectbluefin/finpilot#599). The review planner now re-stamps the prior approve onto the merge head at any delta size, with `delta.base_sync: true`, when every commit since the reviewed head is a GitHub-made merge of the base authored by the hive App and the base's changes miss every file the PR changes. A merge by anyone else, any other commit, a base change to a PR file, or a prior `changes_requested`/`requires_human` keeps the delta review. CI still runs on the merge head, and a negative `review.delta_trivial_lines` turns the carry off. The proof costs two compare calls, made only for a head whose delta is all hive merges. diff --git a/src/cmd/hive/main.go b/src/cmd/hive/main.go index c2ca7483a4..70989462f5 100644 --- a/src/cmd/hive/main.go +++ b/src/cmd/hive/main.go @@ -11186,10 +11186,11 @@ func parseReviseCutoff(raw string, logger *slog.Logger) time.Time { // 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). +// (review.delta_rereview), which also proves a hive base-sync against the +// PR's base branch. type reviewPlannerGitHub interface { ReviewPRChangedFiles(ctx context.Context, repo string, number int) ([]string, bool) - ReviewDeltaDiff(ctx context.Context, repo, previousHead, head string) review.DeltaDiff + ReviewDeltaDiff(ctx context.Context, repo, baseRef, 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 { @@ -11224,8 +11225,9 @@ func reviewBotRouteFilesFor(ctx context.Context, cfg *config.Config, lister revi // 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 { +// nil otherwise, which the planner reads as "not fetched". baseRef lets the +// compare recognise the hive's own merge of the base (DeltaDiff.BaseSync). +func reviewDeltaDiffFor(ctx context.Context, cfg *config.Config, differ reviewPlannerGitHub, artifact review.Artifact, state review.DispatchState, repo string, number int, baseRef, headSHA, bodyDigest string) *review.DeltaDiff { if differ == nil || !cfg.Review.DeltaRereviewEnabled() || strings.TrimSpace(headSHA) == "" { return nil } @@ -11239,7 +11241,7 @@ func reviewDeltaDiffFor(ctx context.Context, cfg *config.Config, differ reviewPl if !ok { return nil } - diff := differ.ReviewDeltaDiff(ctx, repo, prior.HeadSHA, headSHA) + diff := differ.ReviewDeltaDiff(ctx, repo, baseRef, prior.HeadSHA, headSHA) return &diff } @@ -11333,8 +11335,9 @@ func planReviewDispatchWith(ctx context.Context, cfg *config.Config, actionable // 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), + // re-review, its trivial skip and the hive base-sync carry + // (review.delta_rereview). + DeltaDiff: reviewDeltaDiffFor(ctx, cfg, ghr, artifact, state, fullRepo, pr.Number, pr.BaseRef, 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 @@ -11451,7 +11454,8 @@ func recordReviewDeltaRestamps(plan review.DispatchPlan, logger *slog.Logger) { 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) + baseSync := r.Aggregate.Delta != nil && r.Aggregate.Delta.BaseSync + 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, "base_sync", baseSync, "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)) diff --git a/src/cmd/hive/review_bot_route_wiring_test.go b/src/cmd/hive/review_bot_route_wiring_test.go index f3cd588321..4fcc0b41ec 100644 --- a/src/cmd/hive/review_bot_route_wiring_test.go +++ b/src/cmd/hive/review_bot_route_wiring_test.go @@ -18,7 +18,7 @@ func (f *fakeChangedFilesLister) ReviewPRChangedFiles(context.Context, string, i return f.files, f.known } -func (f *fakeChangedFilesLister) ReviewDeltaDiff(_ context.Context, _, previousHead, _ string) review.DeltaDiff { +func (f *fakeChangedFilesLister) ReviewDeltaDiff(_ context.Context, _, _, previousHead, _ string) review.DeltaDiff { return review.DeltaDiff{PreviousHead: previousHead} } diff --git a/src/cmd/hive/review_delta_wiring_test.go b/src/cmd/hive/review_delta_wiring_test.go index fd31862631..f9a611ccd4 100644 --- a/src/cmd/hive/review_delta_wiring_test.go +++ b/src/cmd/hive/review_delta_wiring_test.go @@ -10,12 +10,14 @@ import ( ) type fakeDeltaDiffer struct { - calls []string - diff review.DeltaDiff + calls []string + baseRefs []string + diff review.DeltaDiff } -func (f *fakeDeltaDiffer) ReviewDeltaDiff(_ context.Context, repo, previousHead, head string) review.DeltaDiff { +func (f *fakeDeltaDiffer) ReviewDeltaDiff(_ context.Context, repo, baseRef, previousHead, head string) review.DeltaDiff { f.calls = append(f.calls, repo+"#"+previousHead+".."+head) + f.baseRefs = append(f.baseRefs, baseRef) out := f.diff out.PreviousHead = previousHead return out @@ -101,3 +103,43 @@ func TestPlanReviewDispatch_DeltaNotFetchedWithoutPriorOrWhenOff(t *testing.T) { t.Fatalf("nil differ: kicks=%+v restamps=%d", plan.ReviewKicks, len(plan.DeltaRestamps)) } } + +// The hive's own update-branch of an approved PR (projectbluefin/fsdk-containers#482): +// the planner hands the PR's base branch to the compare, and a base-sync whose +// files miss the PR's carries the approve onto the merge head at any size. +func TestPlanReviewDispatch_HiveBaseSyncCarriesApprove(t *testing.T) { + redirectReviewPaths(t) + writePriorVerdict(t, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", review.VerdictApprove) + actionable := actionableWithPR("hive-bot") + actionable.PRs.Items[0].BaseRef = "main" + differ := &fakeDeltaDiffer{diff: review.DeltaDiff{ + Known: true, BaseSync: true, + Files: []review.DeltaFile{{Path: "pkg/base/b.go", Additions: 400}}, + BaseFiles: []string{"pkg/base/b.go"}, + PRFiles: []string{"pkg/x/x.go"}, + }} + plan := planReviewDispatchWith(context.Background(), reviewSwarmConfig(), actionable, nil, nil, differ, restoreTestLogger()) + if len(differ.baseRefs) != 1 || differ.baseRefs[0] != "main" { + t.Fatalf("base refs passed to the compare = %v, want [main]", differ.baseRefs) + } + 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.BaseSync { + t.Fatalf("base-sync restamp not recorded: %+v ok=%v", got, ok) + } + + // The base changed one of the PR's files: a delta review, not a carry. + redirectReviewPaths(t) + writePriorVerdict(t, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", review.VerdictApprove) + differ.diff.PRFiles = append(differ.diff.PRFiles, "pkg/base/b.go") + plan = planReviewDispatchWith(context.Background(), reviewSwarmConfig(), actionable, nil, nil, differ, restoreTestLogger()) + if len(plan.ReviewKicks) != 1 || len(plan.DeltaRestamps) != 0 || plan.ReviewKicks[0].DeltaPreviousHead == "" { + t.Fatalf("intersecting base-sync: kicks=%+v restamps=%d, want one delta kick", plan.ReviewKicks, len(plan.DeltaRestamps)) + } +} diff --git a/src/docs/hive-merge.md b/src/docs/hive-merge.md index 3138235f94..c781b55c9d 100644 --- a/src/docs/hive-merge.md +++ b/src/docs/hive-merge.md @@ -325,7 +325,11 @@ The same section's **Per-repository merge policy** block edits hive's `project.repos`. Saving writes each key in full as `owner/repo` (the form the sweep matches); an empty selection clears it, and a repo outside `project.repos` or a malformed glob is refused with a 400. The sweep reads all -three live, so a change applies on the next pass without a restart. +three live, so a change applies on the next pass without a restart. An +update-branch of an approved PR keeps its swarm approve when the base's changes +miss every PR file: the review planner re-stamps the verdict onto the merge +head instead of holding it at `review-gate:no-review-bundle`, and CI still +re-runs on that head (see [Delta re-review](review-swarm.md#delta-re-review-reviewdelta_rereview-reviewdelta_trivial_lines-reviewdelta_max_age)). ### Event-driven sweeps on the ForgeState cache diff --git a/src/docs/review-swarm.md b/src/docs/review-swarm.md index 2431785ea0..77ce6de66b 100644 --- a/src/docs/review-swarm.md +++ b/src/docs/review-swarm.md @@ -459,6 +459,12 @@ A re-review is a review of a PR the hive already judged at an earlier head SHA. - 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. +- An **approve** also survives the hive's own merge of the base into the PR, at any delta size. When the hive brings an approved PR up to date (update-branch for `auto_merge.require_current_base`, a `behind` sweep, the hold guard's mergeability upkeep or `review.contributor_prs.base_sync`; GitHub's merges API for merge-forward), the new head used to have no verdict and the PR fell back to `review-gate:no-review-bundle` until a reviewer "delta reviewed" base code that had already been reviewed and merged upstream ([projectbluefin/fsdk-containers#482](https://github.com/projectbluefin/fsdk-containers/pull/482)). The approve is re-stamped (`delta.base_sync: true`, note `hive base-sync; the base's changes … are disjoint from the PR's files`) when all of these hold: + - every commit between the two heads is a two-parent merge whose first parent is the commit before it, authored by the hive App and committed by GitHub itself (`web-flow`): what update-branch and the merges API make. A merge by a person or another bot, a merge pushed with git (agents may commit under the App-linked identity), or any non-merge commit in the delta does not count; + - the last merge's second parent is the base branch's merge base with the head, so the base contains it, and any earlier merge's second parent is on the base below it (the hold guard's hygiene-merge shape, [#14535](https://github.com/hivecommons/hive/issues/14535)); + - the files the delta changes are exactly the files the base changed since the reviewed head's merge base, and none of them is one of the PR's own files. + + Together those mean every file at the new head is either the reviewed head's or the base's, as a clean merge of a base that touched none of the PR's files produces. GitHub refuses a conflicting update-branch or merge rather than resolve it, and a merge whose tree is anything else fails the file checks, unless all it did was take the base's version of a file both sides changed. That is the same result as a PR whose change already landed on the base. When the base touched a PR file, the planner falls back to the delta review above. A prior `changes_requested` or `requires_human` is never carried this way: its delta review re-checks what the reviewer or a person still has open. A negative `delta_trivial_lines` turns this re-stamp off too. Only the review verdict is carried: CI still runs on the merge head, which is what `auto_merge.require_current_base` asks for ([#13634](https://github.com/hivecommons/hive/issues/13634)). The proof costs two more compare calls (the PR's files against the base branch, and the base's side of the merge), and only for a head whose delta is all hive merges. - 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. diff --git a/src/pkg/github/review_delta.go b/src/pkg/github/review_delta.go index 3328282a43..cf61fcad54 100644 --- a/src/pkg/github/review_delta.go +++ b/src/pkg/github/review_delta.go @@ -5,9 +5,15 @@ import ( "strings" gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/holdguard" "github.com/hivecommons/hive/pkg/review" ) +// githubWebFlowLogin is the committer GitHub stamps on a commit it makes +// server-side: update-branch, the merges API, createCommitOnBranch. A commit +// pushed with git names whoever pushed it instead. +const githubWebFlowLogin = "web-flow" + // 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 @@ -16,23 +22,148 @@ import ( // 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 { +// +// When the commits between the heads are all the hive's own merges of the +// base (reviewBaseSyncParents), it also reads the PR's own files and the +// base's side of the merge, so the planner can carry an approve across the +// hive's update-branch (DeltaDiff.BaseSync, +// projectbluefin/fsdk-containers#482). That is two more compares, only for +// such a head. baseRef is the branch the PR targets; "" skips that step. +func (c *Client) ReviewDeltaDiff(ctx context.Context, repo, baseRef, 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 { + restCtx := WithRESTCaller(ctx, "hive:review_delta") + comparison, allCommits, ok := c.reviewCompare(restCtx, owner, name, out.PreviousHead, head, githubCompareFileLimit) + if !ok || !allCommits { 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()}) } + baseRef = strings.TrimSpace(baseRef) + if len(out.Files) == 0 || baseRef == "" { + return out + } + baseParents, ok := reviewBaseSyncParents(comparison, out.PreviousHead, head, c.AppBotLogin()) + if !ok { + return out + } + merged := baseParents[len(baseParents)-1] + // The PR's own files, and the proof the last merged parent is on the + // base: the base branch's merge base with the head is that parent exactly + // when the base contains it (it is an ancestor of the head, and the head + // holds no newer base commit). Three-dot, like the PR's Files tab. The + // file list is complete on the first page whatever the commit paging, so + // one commit per page is enough. + prSide, _, ok := c.reviewCompare(restCtx, owner, name, baseRef, head, 1) + if !ok || !review.SameHeadSHA(prSide.GetMergeBaseCommit().GetSHA(), merged) { + return out + } + // The base's side: what it changed since the reviewed head's merge base. + // Earlier merged parents (more than one hive merge since the review) must + // be among its commits, i.e. on the base below the last one, so only + // then is the commit list read in full. + earlier := baseParents[:len(baseParents)-1] + perPage := 1 + if len(earlier) > 0 { + perPage = githubCompareFileLimit + } + baseSide, baseAll, ok := c.reviewCompare(restCtx, owner, name, out.PreviousHead, merged, perPage) + if !ok || (len(earlier) > 0 && !(baseAll && compareHasCommits(baseSide, earlier))) { + return out + } + out.BaseSync = true + out.PRFiles = compareFilePaths(prSide) + out.BaseFiles = compareFilePaths(baseSide) return out } + +// reviewBaseSyncParents reports whether every commit from previousHead to +// head is a merge of the base the hive App asked GitHub for, and returns each +// merge's second (base) parent in order. Each commit must be a two-parent +// merge whose first parent is the commit before it (holdguard.IsHygieneMerge, +// the shape the hold guard already adopts as hygiene, hivecommons/hive#14535), +// authored by the App and committed by GitHub itself (web-flow): +// update-branch and the merges API make exactly that, and refuse a +// conflicting merge rather than resolve it. A merge pushed with git, by a +// person, another bot or an agent (agents may commit under the App-linked +// identity, hivecommons/hive#9478), names its pusher as committer and never +// counts. Proving the base parents are on the base is the caller's job. +func reviewBaseSyncParents(comparison *gh.CommitsComparison, previousHead, head, appLogin string) ([]string, bool) { + appLogin = strings.TrimSpace(appLogin) + if comparison == nil || appLogin == "" || len(comparison.Commits) == 0 || comparison.GetTotalCommits() != len(comparison.Commits) { + return nil, false + } + var parents []string + prev := previousHead + for _, rc := range comparison.Commits { + if !strings.EqualFold(rc.GetAuthor().GetLogin(), appLogin) || rc.GetCommitter().GetLogin() != githubWebFlowLogin { + return nil, false + } + commit := holdguard.Commit{SHA: rc.GetSHA()} + for _, p := range rc.Parents { + commit.Parents = append(commit.Parents, p.GetSHA()) + } + // The reviewed head may be recorded abbreviated (review.SameHeadSHA); + // the merge names it in full. + if len(commit.Parents) > 0 && review.SameHeadSHA(commit.Parents[0], prev) { + prev = commit.Parents[0] + } + baseParent := "" + record := func(sha string) bool { + baseParent = sha + return true + } + if !holdguard.IsHygieneMerge(commit, prev, record) { + return nil, false + } + parents = append(parents, baseParent) + prev = commit.SHA + } + if !review.SameHeadSHA(prev, head) { + return nil, false + } + return parents, true +} + +// reviewCompare is one compare page whose file list is complete: GitHub +// returns every changed file on the first page, up to githubCompareFileLimit, +// so a full list there is reported as unknown (ok false). allCommits is true +// when that page also holds every commit of the comparison. +func (c *Client) reviewCompare(ctx context.Context, owner, name, base, head string, perPage int) (comparison *gh.CommitsComparison, allCommits, ok bool) { + comparison, resp, err := c.client.Repositories.CompareCommits(ctx, owner, name, base, head, &gh.ListOptions{PerPage: perPage}) + if err != nil || comparison == nil || len(comparison.Files) >= githubCompareFileLimit { + return nil, false, false + } + allCommits = (resp == nil || resp.NextPage == 0) && comparison.GetTotalCommits() <= len(comparison.Commits) + return comparison, allCommits, true +} + +func compareFilePaths(comparison *gh.CommitsComparison) []string { + paths := make([]string, 0, len(comparison.Files)) + for _, f := range comparison.Files { + if p := strings.TrimSpace(f.GetFilename()); p != "" { + paths = append(paths, p) + } + } + return paths +} + +// compareHasCommits reports whether every sha is among the compare's commits. +func compareHasCommits(comparison *gh.CommitsComparison, shas []string) bool { + have := map[string]bool{} + for _, rc := range comparison.Commits { + have[rc.GetSHA()] = true + } + for _, sha := range shas { + if !have[sha] { + return false + } + } + return true +} diff --git a/src/pkg/github/review_delta_test.go b/src/pkg/github/review_delta_test.go new file mode 100644 index 0000000000..6defa739ac --- /dev/null +++ b/src/pkg/github/review_delta_test.go @@ -0,0 +1,208 @@ +package github + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" +) + +const ( + rdApp = "hive-app[bot]" + rdPrev = "1111111111111111111111111111111111111111" + rdMerge = "2222222222222222222222222222222222222222" + rdBase = "3333333333333333333333333333333333333333" + rdMerge0 = "4444444444444444444444444444444444444444" + rdBase0 = "5555555555555555555555555555555555555555" +) + +type rdCommit struct { + SHA string + Parents []string + Author string + Committer string +} + +func rdHiveMerge(sha, firstParent, baseParent string) rdCommit { + return rdCommit{SHA: sha, Parents: []string{firstParent, baseParent}, Author: rdApp, Committer: "web-flow"} +} + +func rdCompareJSON(mergeBase string, commits []rdCommit, files ...string) map[string]any { + cs := []map[string]any{} + for _, c := range commits { + ps := []map[string]any{} + for _, p := range c.Parents { + ps = append(ps, map[string]any{"sha": p}) + } + cs = append(cs, map[string]any{"sha": c.SHA, "parents": ps, "author": map[string]any{"login": c.Author}, "committer": map[string]any{"login": c.Committer}}) + } + fs := []map[string]any{} + for _, f := range files { + fs = append(fs, map[string]any{"filename": f, "additions": 100, "deletions": 1}) + } + return map[string]any{"merge_base_commit": map[string]any{"sha": mergeBase}, "total_commits": len(cs), "commits": cs, "files": fs} +} + +// rdServer answers GET .../compare/... from compares and records +// each compare asked for. +func rdServer(t *testing.T, compares map[string]map[string]any) (*Client, *[]string) { + t.Helper() + var mu sync.Mutex + var calls []string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + const prefix = "/repos/acme/app/compare/" + if r.Method != http.MethodGet || !strings.HasPrefix(r.URL.Path, prefix) { + http.NotFound(w, r) + return + } + key := strings.TrimPrefix(r.URL.Path, prefix) + mu.Lock() + calls = append(calls, key) + mu.Unlock() + body, ok := compares[key] + if !ok { + http.NotFound(w, r) + return + } + _ = json.NewEncoder(w).Encode(body) + })) + t.Cleanup(srv.Close) + c := newTestClient(t, srv, "acme", []string{"acme/app"}) + c.appBotLogin = rdApp + return c, &calls +} + +// rdSingleMerge is the projectbluefin/fsdk-containers#482 shape: one +// update-branch merge of main (rdBase) onto the approved head (rdPrev), the +// base changing two files the PR does not touch. +func rdSingleMerge() map[string]map[string]any { + return map[string]map[string]any{ + rdPrev + "..." + rdMerge: rdCompareJSON(rdPrev, []rdCommit{rdHiveMerge(rdMerge, rdPrev, rdBase)}, "elements/valkey/valkey.bst", "docs/skills/index.md"), + "main..." + rdMerge: rdCompareJSON(rdBase, nil, "elements/cups/libcupsfilters.bst"), + rdPrev + "..." + rdBase: rdCompareJSON("0000000000000000000000000000000000000000", nil, "elements/valkey/valkey.bst", "docs/skills/index.md"), + } +} + +func TestReviewDeltaDiffRecognisesHiveBaseSync(t *testing.T) { + c, calls := rdServer(t, rdSingleMerge()) + d := c.ReviewDeltaDiff(context.Background(), "acme/app", "main", rdPrev, rdMerge) + if !d.Known || !d.BaseSync || len(d.Files) != 2 { + t.Fatalf("diff = %+v, want a known base-sync with 2 files", d) + } + if strings.Join(d.PRFiles, ",") != "elements/cups/libcupsfilters.bst" || strings.Join(d.BaseFiles, ",") != "elements/valkey/valkey.bst,docs/skills/index.md" { + t.Fatalf("PRFiles=%v BaseFiles=%v", d.PRFiles, d.BaseFiles) + } + if !d.BaseSyncOnly() { + t.Fatal("planner predicate rejects the fixture") + } + if len(*calls) != 3 { + t.Fatalf("compares = %v, want 3 (delta, PR side, base side)", *calls) + } +} + +// A verdict recorded under an abbreviated head (review.SameHeadSHA) still +// anchors the chain. +func TestReviewDeltaDiffBaseSyncAbbreviatedPreviousHead(t *testing.T) { + compares := rdSingleMerge() + short := rdPrev[:7] + compares[short+"..."+rdMerge] = compares[rdPrev+"..."+rdMerge] + compares[short+"..."+rdBase] = compares[rdPrev+"..."+rdBase] + c, _ := rdServer(t, compares) + if d := c.ReviewDeltaDiff(context.Background(), "acme/app", "main", short, rdMerge); !d.BaseSync { + t.Fatalf("abbreviated reviewed head broke the base-sync proof: %+v", d) + } +} + +func TestReviewDeltaDiffBaseSyncTwoMerges(t *testing.T) { + compares := map[string]map[string]any{ + rdPrev + "..." + rdMerge: rdCompareJSON(rdPrev, []rdCommit{rdHiveMerge(rdMerge0, rdPrev, rdBase0), rdHiveMerge(rdMerge, rdMerge0, rdBase)}, "a.txt"), + "main..." + rdMerge: rdCompareJSON(rdBase, nil, "pr.go"), + rdPrev + "..." + rdBase: rdCompareJSON("0000000000000000000000000000000000000000", []rdCommit{{SHA: rdBase0}, {SHA: rdBase}}, "a.txt"), + } + c, _ := rdServer(t, compares) + if d := c.ReviewDeltaDiff(context.Background(), "acme/app", "main", rdPrev, rdMerge); !d.BaseSync { + t.Fatalf("two hive merges of base commits not recognised: %+v", d) + } + // The earlier merged parent is not on the base below the last one. + compares[rdPrev+"..."+rdBase] = rdCompareJSON("0000000000000000000000000000000000000000", []rdCommit{{SHA: rdBase}}, "a.txt") + if d := c.ReviewDeltaDiff(context.Background(), "acme/app", "main", rdPrev, rdMerge); d.BaseSync { + t.Fatalf("earlier merge of a non-base commit counted as a base-sync: %+v", d) + } +} + +func TestReviewDeltaDiffNotBaseSync(t *testing.T) { + cases := map[string]func(map[string]map[string]any){ + "human-authored merge": func(m map[string]map[string]any) { + c := rdHiveMerge(rdMerge, rdPrev, rdBase) + c.Author = "octocat" + m[rdPrev+"..."+rdMerge] = rdCompareJSON(rdPrev, []rdCommit{c}, "elements/valkey/valkey.bst", "docs/skills/index.md") + }, + "other bot's merge": func(m map[string]map[string]any) { + c := rdHiveMerge(rdMerge, rdPrev, rdBase) + c.Author = "renovate[bot]" + m[rdPrev+"..."+rdMerge] = rdCompareJSON(rdPrev, []rdCommit{c}, "elements/valkey/valkey.bst", "docs/skills/index.md") + }, + "App-identity merge pushed with git": func(m map[string]map[string]any) { + c := rdHiveMerge(rdMerge, rdPrev, rdBase) + c.Committer = rdApp + m[rdPrev+"..."+rdMerge] = rdCompareJSON(rdPrev, []rdCommit{c}, "elements/valkey/valkey.bst", "docs/skills/index.md") + }, + "non-merge commit": func(m map[string]map[string]any) { + c := rdHiveMerge(rdMerge, rdPrev, rdBase) + c.Parents = []string{rdPrev} + m[rdPrev+"..."+rdMerge] = rdCompareJSON(rdPrev, []rdCommit{c}, "elements/valkey/valkey.bst", "docs/skills/index.md") + }, + "non-merge commit before the merge": func(m map[string]map[string]any) { + m[rdPrev+"..."+rdMerge] = rdCompareJSON(rdPrev, []rdCommit{ + {SHA: rdMerge0, Parents: []string{rdPrev}, Author: rdApp, Committer: "web-flow"}, + rdHiveMerge(rdMerge, rdMerge0, rdBase), + }, "elements/valkey/valkey.bst", "docs/skills/index.md") + }, + "merge onto another commit than the reviewed head": func(m map[string]map[string]any) { + m[rdPrev+"..."+rdMerge] = rdCompareJSON(rdPrev, []rdCommit{rdHiveMerge(rdMerge, rdMerge0, rdBase)}, "elements/valkey/valkey.bst", "docs/skills/index.md") + }, + "commits stop short of the head": func(m map[string]map[string]any) { + m[rdPrev+"..."+rdMerge] = rdCompareJSON(rdPrev, []rdCommit{rdHiveMerge(rdMerge0, rdPrev, rdBase)}, "elements/valkey/valkey.bst", "docs/skills/index.md") + }, + "merged parent not on the base": func(m map[string]map[string]any) { + m["main..."+rdMerge] = rdCompareJSON(rdBase0, nil, "elements/cups/libcupsfilters.bst") + }, + "PR side unreadable": func(m map[string]map[string]any) { + delete(m, "main..."+rdMerge) + }, + "base side unreadable": func(m map[string]map[string]any) { + delete(m, rdPrev+"..."+rdBase) + }, + } + for name, mutate := range cases { + t.Run(name, func(t *testing.T) { + compares := rdSingleMerge() + mutate(compares) + c, _ := rdServer(t, compares) + d := c.ReviewDeltaDiff(context.Background(), "acme/app", "main", rdPrev, rdMerge) + if !d.Known || d.BaseSync || d.PRFiles != nil || d.BaseFiles != nil { + t.Fatalf("diff = %+v, want known, not a base-sync", d) + } + }) + } + t.Run("no App login or no base ref: no extra compare", func(t *testing.T) { + c, calls := rdServer(t, rdSingleMerge()) + if d := c.ReviewDeltaDiff(context.Background(), "acme/app", "", rdPrev, rdMerge); d.BaseSync || len(*calls) != 1 { + t.Fatalf("no base ref: BaseSync=%v compares=%v", d.BaseSync, *calls) + } + // A merge whose author GitHub could not link to any account has an + // empty login; an unset App login must not match it. + unlinked := rdSingleMerge() + m := rdHiveMerge(rdMerge, rdPrev, rdBase) + m.Author = "" + unlinked[rdPrev+"..."+rdMerge] = rdCompareJSON(rdPrev, []rdCommit{m}, "elements/valkey/valkey.bst", "docs/skills/index.md") + c, calls = rdServer(t, unlinked) + c.appBotLogin = "" + if d := c.ReviewDeltaDiff(context.Background(), "acme/app", "main", rdPrev, rdMerge); d.BaseSync || len(*calls) != 1 { + t.Fatalf("no App login: BaseSync=%v compares=%v", d.BaseSync, *calls) + } + }) +} diff --git a/src/pkg/review/delta.go b/src/pkg/review/delta.go index 24b1441e32..62dd5aefbd 100644 --- a/src/pkg/review/delta.go +++ b/src/pkg/review/delta.go @@ -48,6 +48,20 @@ type DeltaDiff struct { // delta instruction, since that needs no file list. Known bool Files []DeltaFile + // BaseSync is true when every commit between the two heads is a merge of + // the base branch into the PR that GitHub made for the hive App + // (update-branch for review.contributor_prs.base_sync, + // auto_merge.require_current_base and the hold guard's mergeability upkeep; + // the merges API for merge-forward), proven by pkg/github ReviewDeltaDiff + // from the compare's commits (projectbluefin/fsdk-containers#482). When it is true + // PRFiles and BaseFiles are complete lists; when it is false both are nil. + BaseSync bool + // PRFiles are the PR's own changed paths at the current head (the base + // branch's merge base with the head, three-dot, like the PR's Files tab). + PRFiles []string + // BaseFiles are the paths the base branch changed between the reviewed + // head's merge base and the base commit the sync merged in. + BaseFiles []string } // ChangedLines is the delta size the trivial threshold is judged against. @@ -65,6 +79,51 @@ func (d DeltaDiff) Identical() bool { return d.Known && len(d.Files) == 0 } +// BaseSyncOnly reports a known base-sync delta that brought in nothing but +// the base's own changes, none of them in a file the PR changes: +// +// - the delta's files are exactly the files the base changed (BaseFiles), so +// no base change was dropped and nothing beyond the base was added; and +// - no delta file is one of the PR's own files (PRFiles), so each delta file +// now holds the base's content, and no PR file moved. +// +// Together these mean every file at the new head is either byte-for-byte the +// reviewed head's or the base's, which is what a clean merge of a base that +// touched none of the PR's files produces. GitHub's update-branch and merges +// API refuse a conflicting merge, so a hive base-sync never carries a conflict +// resolution, and a merge whose result differs from that (a hand-resolved or +// fabricated tree) fails one of the two checks unless all it did was take +// the base's version of a file both sides changed: the same result as a PR +// whose change already landed on the base, which is base code, reviewed and +// merged there. +func (d DeltaDiff) BaseSyncOnly() bool { + if !d.Known || !d.BaseSync || len(d.Files) == 0 { + return false + } + delta := map[string]bool{} + for _, f := range d.Files { + delta[strings.TrimSpace(f.Path)] = true + } + base := map[string]bool{} + for _, p := range d.BaseFiles { + base[strings.TrimSpace(p)] = true + } + if len(base) != len(delta) { + return false + } + for p := range delta { + if !base[p] { + return false + } + } + for _, p := range d.PRFiles { + if delta[strings.TrimSpace(p)] { + return false + } + } + return true +} + // 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. @@ -85,6 +144,9 @@ type DeltaContext struct { // ChangedLines is the known delta size, 0 when unknown. ChangedLines int DiffKnown bool + // BaseSync is set when the planner carries an approve across a hive + // base-sync (DeltaDiff.BaseSyncOnly) rather than a trivial delta. + BaseSync bool } // DeltaReviewInfo is recorded on an aggregate produced by a delta re-review, @@ -95,7 +157,11 @@ type DeltaReviewInfo struct { // 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"` + Trivial bool `json:"trivial,omitempty"` + // BaseSync is true when the re-stamp crossed a hive merge of the base + // into the PR whose changes miss every PR file (projectbluefin/fsdk-containers#482), + // at any delta size. + BaseSync bool `json:"base_sync,omitempty"` ChangedLines int `json:"changed_lines,omitempty"` Note string `json:"note,omitempty"` } @@ -172,6 +238,17 @@ func (o DispatchOptions) effectiveDeltaMaxAge() time.Duration { // - 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. +// - a prior approve across a hive base-sync that brought in only base +// changes outside the PR's files (DeltaDiff.BaseSyncOnly) is re-stamped +// at any delta size (projectbluefin/fsdk-containers#482). The hive's own update-branch +// used to drop an approved PR back to review-gate:no-review-bundle, and +// then pay a model to "delta review" base code that was reviewed and +// merged upstream. CI still re-runs on the merge head (the point of +// auto_merge.require_current_base, hivecommons/hive#13634), so only the +// review verdict is carried. Only approve: a changes_requested or +// requires_human verdict keeps its delta review, so a base-sync never +// settles what a reviewer or a person still has open. A negative +// delta_trivial_lines, which opts out of re-stamps, opts out of this too. func planDelta(pr PullRequest, artifact Artifact, opts DispatchOptions, now time.Time) (deltaMode, *DeltaContext, Aggregate) { if !opts.DeltaRereview { return deltaModeFull, nil, Aggregate{} @@ -205,6 +282,10 @@ func planDelta(pr PullRequest, artifact Artifact, opts DispatchOptions, now time ctx.Files = append(ctx.Files, f.Path) } sort.Strings(ctx.Files) + if prior.Verdict == VerdictApprove && opts.DeltaTrivialLines >= 0 && diff.BaseSyncOnly() { + ctx.BaseSync = true + return deltaModeTrivial, ctx, prior + } if deltaTrivial(*diff, prior, opts.effectiveDeltaTrivialLines()) { return deltaModeTrivial, ctx, prior } @@ -248,10 +329,13 @@ func restampAggregate(prior Aggregate, pr PullRequest, ctx DeltaContext, now tim 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 { + switch { + case ctx.BaseSync: + note = fmt.Sprintf("re-stamped from %s: hive base-sync; the base's changes (%d file(s), %d line(s)) are disjoint from the PR's files", shortSHA(ctx.PreviousHead), len(ctx.Files), ctx.ChangedLines) + case 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.Delta = &DeltaReviewInfo{PreviousHead: ctx.PreviousHead, Trivial: true, BaseSync: ctx.BaseSync, ChangedLines: ctx.ChangedLines, Note: note} agg.Reasons = append(append([]string(nil), prior.Reasons...), "delta re-review: "+note) return agg } diff --git a/src/pkg/review/delta_base_sync_test.go b/src/pkg/review/delta_base_sync_test.go new file mode 100644 index 0000000000..2f92e56a0d --- /dev/null +++ b/src/pkg/review/delta_base_sync_test.go @@ -0,0 +1,116 @@ +package review + +import ( + "strings" + "testing" + "time" +) + +// baseSyncDelta is a large known delta that the hive's own merge of the base +// produced: 400 lines in two base files, neither of them a PR file +// (projectbluefin/fsdk-containers#482). +func baseSyncDelta(prev string) *DeltaDiff { + d := knownDelta(prev, + DeltaFile{Path: "elements/valkey/valkey.bst", Additions: 250, Deletions: 50}, + DeltaFile{Path: "docs/skills/index.md", Additions: 100}, + ) + d.BaseSync = true + d.BaseFiles = []string{"docs/skills/index.md", "elements/valkey/valkey.bst"} + d.PRFiles = []string{"elements/cups/libcupsfilters.bst", "docs/fsdk.md"} + return d +} + +func planBaseSync(t *testing.T, prior Aggregate, diff *DeltaDiff, opts DispatchOptions) DispatchPlan { + t.Helper() + pr := dispatchPR("sha2") + pr.BodyDigest = "body2" + pr.DeltaDiff = diff + return PlanDispatch([]PullRequest{pr}, Artifact{Items: []Aggregate{prior}}, DispatchState{}, opts) +} + +func TestPlanDeltaBaseSyncDisjointRestampsAtAnySize(t *testing.T) { + prior := priorAggregate("sha1", VerdictApprove, deltaNow.Add(-2*time.Hour)) + diff := baseSyncDelta("sha1") + if diff.ChangedLines() < DefaultDeltaTrivialLines { + t.Fatalf("fixture delta is %d lines; it must be over the trivial threshold to test the base-sync rule", diff.ChangedLines()) + } + plan := planBaseSync(t, prior, diff, deltaOpts(reviewer("r1"))) + if len(plan.ReviewKicks) != 0 || len(plan.State.Pending) != 0 || len(plan.DeltaRestamps) != 1 { + t.Fatalf("kicks=%d pending=%d restamps=%d, want 0/0/1", len(plan.ReviewKicks), len(plan.State.Pending), len(plan.DeltaRestamps)) + } + agg := plan.DeltaRestamps[0].Aggregate + if agg.HeadSHA != "sha2" || agg.BodyDigest != "body2" || agg.Verdict != VerdictApprove || !agg.MergeEligible { + t.Fatalf("restamped aggregate = %+v", agg) + } + if agg.Delta == nil || !agg.Delta.Trivial || !agg.Delta.BaseSync || agg.Delta.ChangedLines != 400 || !strings.Contains(agg.Delta.Note, "hive base-sync") || !strings.Contains(agg.Delta.Note, "disjoint from the PR's files") { + t.Fatalf("delta info = %+v", agg.Delta) + } +} + +func TestPlanDeltaBaseSyncFallsThroughToDeltaReview(t *testing.T) { + approve := priorAggregate("sha1", VerdictApprove, deltaNow.Add(-2*time.Hour)) + cases := map[string]struct { + prior Aggregate + mutate func(*DeltaDiff) + opts func(*DispatchOptions) + }{ + "base changed a PR file": {prior: approve, mutate: func(d *DeltaDiff) { + d.PRFiles = append(d.PRFiles, "elements/valkey/valkey.bst") + }}, + "not a hive base-sync (human, other bot or non-merge commit)": {prior: approve, mutate: func(d *DeltaDiff) { + d.BaseSync = false + }}, + "delta has a file the base did not change": {prior: approve, mutate: func(d *DeltaDiff) { + d.Files = append(d.Files, DeltaFile{Path: "elements/cups/extra.bst", Additions: 3}) + }}, + "merge dropped a base change": {prior: approve, mutate: func(d *DeltaDiff) { + d.BaseFiles = append(d.BaseFiles, "elements/base-only.bst") + }}, + "unknown compare": {prior: approve, mutate: func(d *DeltaDiff) { + d.Known = false + }}, + "prior changes_requested": {prior: priorAggregate("sha1", VerdictChangesRequested, deltaNow.Add(-2*time.Hour), flagged("elements/cups/libcupsfilters.bst", 3, "bug"))}, + "prior requires_human": {prior: priorAggregate("sha1", VerdictRequiresHuman, deltaNow.Add(-2*time.Hour))}, + "re-stamps opted out": {prior: approve, opts: func(o *DispatchOptions) { o.DeltaTrivialLines = -1 }}, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + diff := baseSyncDelta("sha1") + if tc.mutate != nil { + tc.mutate(diff) + } + opts := deltaOpts(reviewer("r1")) + if tc.opts != nil { + tc.opts(&opts) + } + plan := planBaseSync(t, tc.prior, diff, opts) + if len(plan.DeltaRestamps) != 0 || len(plan.ReviewKicks) != 1 { + t.Fatalf("kicks=%d restamps=%+v, want a delta review and no restamp", len(plan.ReviewKicks), plan.DeltaRestamps) + } + if plan.ReviewKicks[0].DeltaPreviousHead != "sha1" { + t.Fatalf("not a delta kick: %+v", plan.ReviewKicks[0]) + } + }) + } +} + +func TestDeltaDiffBaseSyncOnly(t *testing.T) { + if !baseSyncDelta("sha1").BaseSyncOnly() { + t.Fatal("disjoint base-sync not recognised") + } + d := baseSyncDelta("sha1") + d.Files[0].Path = " " + d.Files[0].Path + " " + if !d.BaseSyncOnly() { + t.Fatal("whitespace around a path broke the match") + } + // An empty delta is the identical-tree rule's, not this one's. + if (&DeltaDiff{Known: true, BaseSync: true}).BaseSyncOnly() { + t.Fatal("empty delta counted as a base-sync") + } + // The same file count but a different file is not the base's change. + d = baseSyncDelta("sha1") + d.BaseFiles = []string{"docs/skills/index.md", "elements/other.bst"} + if d.BaseSyncOnly() { + t.Fatal("delta file the base did not change accepted") + } +}