diff --git a/changelog.d/fixed-review-fix-missing-body-digest.md b/changelog.d/fixed-review-fix-missing-body-digest.md new file mode 100644 index 0000000000..5b6e6a2046 --- /dev/null +++ b/changelog.d/fixed-review-fix-missing-body-digest.md @@ -0,0 +1 @@ +- review-fix: a `changes_requested` verdict whose report carries no `body_digest` is now treated as "digest unknown → body unchanged" instead of "PR body edited → re-review", so PRs at `stage/070-fixing` get a fix kick instead of being re-asked for review every cycle (#14665). Reports are now stamped with the PR body digest at collect time and when the relay records advisory verdicts; a starvation guard forces the fix path after 3 consecutive revisit-suppressed cycles, each suppressed cycle logs a WARN with its reason, and the planner summary line reports `fix_excluded` / `fix_excluded_reasons`. diff --git a/src/cmd/hive/main.go b/src/cmd/hive/main.go index 3dc9143e9a..31cbbe4abb 100644 --- a/src/cmd/hive/main.go +++ b/src/cmd/hive/main.go @@ -10783,12 +10783,38 @@ func planReviewDispatchWith(ctx context.Context, cfg *config.Config, actionable for _, h := range plan.Exhausted { logger.Warn("review dispatch exhausted; holding PR for a human", "repo", h.Repo, "pr", h.Number, "head", h.HeadSHA, "reason", h.Reason) } - if len(plan.ReviewKicks)+len(plan.FixKicks) > 0 { - logger.Info("review swarm dispatch planned", "review_kicks", len(plan.ReviewKicks), "fix_kicks", len(plan.FixKicks)) + for _, f := range plan.ForcedFixes { + logger.Warn("review fix forced: changes_requested head starved by the revisit path", "repo", f.Repo, "pr", f.Number, "head", f.HeadSHA, "reason", f.Reason, "cycles", f.Cycles, "limit", review.RevisitStarvationCycles) + } + if len(plan.ReviewKicks)+len(plan.FixKicks)+len(plan.FixExclusions) > 0 { + logger.Info("review swarm dispatch planned", "review_kicks", len(plan.ReviewKicks), "fix_kicks", len(plan.FixKicks), "fix_excluded", len(plan.FixExclusions), "fix_excluded_reasons", fixExclusionReasonSummary(plan.FixExclusions)) } return plan } +// fixExclusionReasonSummary renders the cycle's fix exclusions as +// "reason=count,..." (sorted) for the dispatch summary line, so fix_kicks: 0 +// is never printed without saying why (hivecommons/hive#14665). +func fixExclusionReasonSummary(exclusions []review.FixDispatchExclusion) string { + if len(exclusions) == 0 { + return "" + } + counts := map[string]int{} + for _, x := range exclusions { + counts[x.Reason]++ + } + reasons := make([]string, 0, len(counts)) + for r := range counts { + reasons = append(reasons, r) + } + sort.Strings(reasons) + parts := make([]string, 0, len(reasons)) + for _, r := range reasons { + parts = append(parts, fmt.Sprintf("%s=%d", r, counts[r])) + } + return strings.Join(parts, ",") +} + // 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, @@ -11001,8 +11027,11 @@ type reviewFixAuditor interface { // hivecommons/hive#12897). The planner reports each head once, so // this does not repeat on every eval cycle. func auditWithheldReviewFixes(plan review.DispatchPlan, auditor reviewFixAuditor, logger *slog.Logger) { + // One WARN per changes_requested head that ends the cycle without a fix + // kick: a PR shown as "Fixing" that nobody is fixing must be loud + // (hivecommons/hive#14665). for _, x := range plan.FixExclusions { - logger.Info("review fix dispatch excluded", + logger.Warn("review fix dispatch excluded", "repo", x.Repo, "pr", x.Number, "head_sha", x.HeadSHA, "author", x.Author, "reason", x.Reason, "attempts", x.Attempts) } for _, w := range plan.WithheldFixes { diff --git a/src/pkg/github/review_verdict_relay.go b/src/pkg/github/review_verdict_relay.go index ae4d71a99f..b186ce7dbf 100644 --- a/src/pkg/github/review_verdict_relay.go +++ b/src/pkg/github/review_verdict_relay.go @@ -89,7 +89,8 @@ func (c *Client) recordReviewVerdict(ctx context.Context, req ReviewRequest, dir if decisionRequested || reviewReportsHoldForHuman(reports) { tightening = c.workflowPermissionsTighteningOnly(ctx, req) } - prHead := c.reviewedPRHeadFunc(ctx, req) + prSnapshot := c.reviewedPRSnapshotFunc(ctx, req) + prHead := func() string { return prSnapshot().Head } var recorded []review.PerspectiveReport for _, report := range reports { if decisionRequested { @@ -99,7 +100,7 @@ func (c *Client) recordReviewVerdict(ctx context.Context, req ReviewRequest, dir report.Verdict = review.VerdictRequiresHuman } report.PermissionsTighteningOnly = tightening - if written, ok := c.writeOneVerdict(ctx, req, report, dir, prHead); ok { + if written, ok := c.writeOneVerdict(ctx, req, report, dir, prHead, prSnapshot); ok { recorded = append(recorded, written) } } @@ -145,34 +146,44 @@ func (c *Client) canonicalVerdictHead(req ReviewRequest, reported, dispatchHead return head } -// reviewedPRHeadFunc returns a memoized lookup of req's PR head SHA: one -// GitHub GET per relay pass at most, only when a verdict actually needs it, -// and "" when the client is unavailable or the fetch fails. -func (c *Client) reviewedPRHeadFunc(ctx context.Context, req ReviewRequest) func() string { +// reviewedPRSnapshot is what the relay reads from the PR itself: its head +// SHA, and the digest of its current title and body. +type reviewedPRSnapshot struct { + Head string + BodyDigest string +} + +// reviewedPRSnapshotFunc returns a memoized lookup of req's PR head SHA and +// body digest: one REST call per relayed request at most, and none when +// nothing asks. A fetch failure is logged once and yields an empty snapshot. +func (c *Client) reviewedPRSnapshotFunc(ctx context.Context, req ReviewRequest) func() reviewedPRSnapshot { var ( - head string + snap reviewedPRSnapshot fetched bool ) - return func() string { + return func() reviewedPRSnapshot { if fetched { - return head + return snap } fetched = true if c == nil || c.client == nil { - return "" + return snap } owner, name := c.splitRepo(strings.TrimSpace(req.Repo)) if owner == "" || name == "" || req.Number <= 0 { - return "" + return snap } pr, _, err := c.client.PullRequests.Get(WithRESTCaller(ctx, "hive:review_verdict_relay"), owner, name, req.Number) if err != nil || pr == nil { - c.logger.Warn("review-request watcher: could not fetch PR head to widen abbreviated verdict head", + c.logger.Warn("review-request watcher: could not fetch PR to bind verdict head and body digest", slog.String("repo", req.Repo), slog.Int("number", req.Number)) - return "" + return snap } - head = strings.TrimSpace(pr.GetHead().GetSHA()) - return head + snap = reviewedPRSnapshot{ + Head: strings.TrimSpace(pr.GetHead().GetSHA()), + BodyDigest: review.PullRequestBodyDigest(pr.GetTitle(), pr.GetBody()), + } + return snap } } @@ -329,7 +340,7 @@ func maintainerCallQuestion(body string) string { // always read — a combined review changes how verdicts arrive, not how they // are stored, so nothing downstream needs to know the difference. It returns // the report as stored and whether it was. -func (c *Client) writeOneVerdict(ctx context.Context, req ReviewRequest, report review.PerspectiveReport, dir string, prHead func() string) (review.PerspectiveReport, bool) { +func (c *Client) writeOneVerdict(ctx context.Context, req ReviewRequest, report review.PerspectiveReport, dir string, prHead func() string, prSnapshot func() reviewedPRSnapshot) (review.PerspectiveReport, bool) { // The verdict must be about the PR that was actually reviewed. Without this // an agent authorized to comment on one PR could record a binding // requires_human/reject verdict against any other PR in the fleet. @@ -393,7 +404,10 @@ func (c *Client) writeOneVerdict(ctx context.Context, req ReviewRequest, report // with no digest stamps an empty one, so every head's verdict for that // PR and head SHA shares one aggregate instead of each landing under // whatever the model wrote (hivecommons/hive#14594). An advisory verdict - // has no dispatch record and keeps the digest it reported. + // has no dispatch record and keeps the digest it reported; when it + // reported none, the digest of the PR's current title and body is + // stamped, so the planner can tell a later body edit from an unknown + // one (hivecommons/hive#14665). if dispatchBodyDigest := strings.TrimSpace(auth.BodyDigest); auth.Dispatched { if strings.TrimSpace(report.BodyDigest) != dispatchBodyDigest { c.logger.Debug("review-request watcher: verdict body digest replaced by the dispatch digest", @@ -402,6 +416,8 @@ func (c *Client) writeOneVerdict(ctx context.Context, req ReviewRequest, report slog.String("agent", req.Agent)) } report.BodyDigest = dispatchBodyDigest + } else if strings.TrimSpace(report.BodyDigest) == "" && prSnapshot != nil { + report.BodyDigest = prSnapshot().BodyDigest } // Re-marshalled from the validated struct rather than written through from diff --git a/src/pkg/review/dispatch.go b/src/pkg/review/dispatch.go index fff6c9cd43..d817fbf77e 100644 --- a/src/pkg/review/dispatch.go +++ b/src/pkg/review/dispatch.go @@ -275,6 +275,32 @@ type DispatchState struct { HeadRetries []HeadRetryCount `json:"head_retries,omitempty"` MissingHeads []MissingHead `json:"missing_heads,omitempty"` Acknowledged []AcknowledgedHead `json:"acknowledged,omitempty"` + // RevisitStarved counts, per head, the consecutive cycles in which a + // changes_requested verdict was sent down the revisit path instead of + // the fix path. At RevisitStarvationCycles the planner forces the fix + // (hivecommons/hive#14665). Cleared when the head is settled normally. + RevisitStarved []RevisitStarvation `json:"revisit_starved,omitempty"` +} + +// RevisitStarvation is the per-head counter behind RevisitStarvationCycles. +type RevisitStarvation struct { + Repo string `json:"repo"` + Number int `json:"number"` + HeadSHA string `json:"head_sha,omitempty"` + Reason string `json:"reason"` + Cycles int `json:"cycles"` + LastAt time.Time `json:"last_at"` +} + +// ForcedFix is a fix kick the planner took despite a revisit being due, +// because the head had been starved of its fix for RevisitStarvationCycles +// consecutive cycles. The caller logs each one. +type ForcedFix struct { + Repo string + Number int + HeadSHA string + Reason string + Cycles int } // RedispatchCount is the per-head expiry counter behind review.max_redispatch. @@ -471,7 +497,10 @@ type DispatchPlan struct { // trivial delta). The caller records them in the verdict artifact // (RecordDeltaRestamps) and logs each one. DeltaRestamps []DeltaRestamp - State DispatchState + // ForcedFixes are the changes_requested heads whose fix kick was forced + // this cycle after RevisitStarvationCycles revisit-suppressed cycles. + ForcedFixes []ForcedFix + State DispatchState } type FixDispatchExclusion struct { @@ -490,6 +519,10 @@ const ( FixDispatchExcludedNotHiveAuthored = "not-hive-authored" FixDispatchExcludedNotHiveAuthoredDCO = "not-hive-authored-squash-signoff" FixAttemptsExhaustedHumanDecisionReason = "fix-attempts-exhausted" + // The revisit reasons name why a changes_requested verdict was sent back + // to the reviewers instead of the fixer this cycle (hivecommons/hive#14665). + FixDispatchExcludedRevisitBodyEdited = "verdict-revisit:body-edited" + FixDispatchExcludedRevisitStale = "verdict-revisit:stale-reviewer" ) func LoadDispatchState(path string) (DispatchState, error) { @@ -596,7 +629,26 @@ func PlanDispatch(prs []PullRequest, artifact Artifact, state DispatchState, opt // reviewer-side defect is found: without this, verdicts produced // by a known-broken reviewer stay frozen until someone happens to // push a commit. - if !staleVerdictRevisitable(pr.Repo, agg, opts) && !bodyEditedVerdictRevisitable(pr, agg) { + revisitReason := "" + if staleVerdictRevisitable(pr.Repo, agg, opts) { + revisitReason = FixDispatchExcludedRevisitStale + } else if bodyEditedVerdictRevisitable(pr, agg) { + revisitReason = FixDispatchExcludedRevisitBodyEdited + } + // A revisit re-asks the reviewers; it must not keep the fixer + // away from a changes_requested head indefinitely. After + // RevisitStarvationCycles consecutive revisit cycles with no fix + // kick, the fix path is taken regardless (hivecommons/hive#14665). + forcedFix := false + if revisitReason != "" && agg.Verdict == VerdictChangesRequested { + if starved := revisitStarvedCycles(plan.State, pr); starved >= RevisitStarvationCycles { + plan.ForcedFixes = append(plan.ForcedFixes, ForcedFix{Repo: pr.Repo, Number: pr.Number, HeadSHA: pr.HeadSHA, Reason: revisitReason, Cycles: starved}) + revisitReason = "" + forcedFix = true + } + } + if revisitReason == "" { + plan.State.RevisitStarved = removeRevisitStarvedForHead(plan.State.RevisitStarved, pr) if agg.Verdict == VerdictIncomplete { // Every perspective that reported approved; some never // reported. The head is not settled, it is under-asked: @@ -619,7 +671,11 @@ func PlanDispatch(prs []PullRequest, artifact Artifact, state DispatchState, opt } plan.expirePending(pr, opts, now) } else { - plan.State.Pending = removePendingForHead(plan.State.Pending, pr) + // A forced fix leaves the in-flight revisit's pending + // entries alone: the relay binds its verdicts to them. + if !forcedFix { + plan.State.Pending = removePendingForHead(plan.State.Pending, pr) + } if headsMode { plan.acknowledgeSkippedDecision(pr, agg, opts, now) } @@ -636,6 +692,9 @@ func PlanDispatch(prs []PullRequest, artifact Artifact, state DispatchState, opt continue } } else { + if agg.Verdict == VerdictChangesRequested { + plan.noteRevisitStarvation(pr, revisitReason, now) + } // Pending entries are the lifetime record of what this head has // already been asked, and a PR with a verdict always has them — // so left in place they make every perspective look covered and @@ -1051,19 +1110,81 @@ func (p *DispatchPlan) holdForHuman(pr PullRequest, agg Aggregate, now time.Time p.State.Human = upsertHuman(p.State.Human, HumanReviewHold{Repo: pr.Repo, Number: pr.Number, HeadSHA: pr.HeadSHA, Reason: reason, UpdatedAt: now}) } +// bodyEditedVerdictRevisitable reports whether a non-approve verdict was +// judged against an earlier PR title/body than the one the PR carries now. +// Only a present-and-different digest proves an edit. A verdict with no +// digest is unknown, not edited: perspective reports may omit body_digest +// (the prompt allows it), and reading the gap as "edited" sent every such +// changes_requested verdict down the revisit path instead of the fix path, +// forever (hivecommons/hive#14665). The UpdatedAt fallback is for PR +// snapshots that predate digests altogether. func bodyEditedVerdictRevisitable(pr PullRequest, agg Aggregate) bool { if agg.Verdict == VerdictApprove || agg.Verdict == VerdictIncomplete { return false } - if strings.TrimSpace(pr.BodyDigest) != "" && strings.TrimSpace(agg.BodyDigest) != "" { - return strings.TrimSpace(pr.BodyDigest) != strings.TrimSpace(agg.BodyDigest) - } - if strings.TrimSpace(pr.BodyDigest) != "" && strings.TrimSpace(agg.BodyDigest) == "" { - return true + prDigest := strings.TrimSpace(pr.BodyDigest) + aggDigest := strings.TrimSpace(agg.BodyDigest) + if prDigest != "" { + if aggDigest == "" { + return false + } + return prDigest != aggDigest } return !pr.UpdatedAt.IsZero() && !agg.RecordedAt.IsZero() && pr.UpdatedAt.After(agg.RecordedAt) } +// RevisitStarvationCycles is how many consecutive planner cycles a +// changes_requested head may be sent down the revisit path with no fix kick +// before the planner forces the fix path. The revisit exists to re-ask the +// reviewers; it must never become a reason the fixer is not asked at all. +const RevisitStarvationCycles = 3 + +// revisitStarvedCycles returns how many consecutive cycles this head's +// changes_requested verdict was suppressed by the revisit path so far. +func revisitStarvedCycles(state DispatchState, pr PullRequest) int { + for _, s := range state.RevisitStarved { + if samePRHead(s.Repo, s.Number, s.HeadSHA, pr.Repo, pr.Number, pr.HeadSHA) { + return s.Cycles + } + } + return 0 +} + +// noteRevisitStarvation counts one more cycle in which the revisit path +// suppressed this head's fix kick, and records the exclusion so the caller +// logs why fix_kicks stayed at zero. +func (p *DispatchPlan) noteRevisitStarvation(pr PullRequest, reason string, now time.Time) { + cycles := 0 + found := false + for i := range p.State.RevisitStarved { + s := &p.State.RevisitStarved[i] + if samePRHead(s.Repo, s.Number, s.HeadSHA, pr.Repo, pr.Number, pr.HeadSHA) { + s.Cycles++ + s.Reason = reason + s.LastAt = now + cycles = s.Cycles + found = true + break + } + } + if !found { + cycles = 1 + p.State.RevisitStarved = append(p.State.RevisitStarved, RevisitStarvation{Repo: pr.Repo, Number: pr.Number, HeadSHA: pr.HeadSHA, Reason: reason, Cycles: cycles, LastAt: now}) + } + p.recordFixExclusion(pr, reason, cycles) +} + +func removeRevisitStarvedForHead(items []RevisitStarvation, pr PullRequest) []RevisitStarvation { + kept := items[:0] + for _, s := range items { + if samePRHead(s.Repo, s.Number, s.HeadSHA, pr.Repo, pr.Number, pr.HeadSHA) { + continue + } + kept = append(kept, s) + } + return kept +} + // effectivePendingTTL resolves PendingTTL: zero is the default, negative // turns expiry off. func (o DispatchOptions) effectivePendingTTL() time.Duration { @@ -1730,6 +1851,13 @@ func pruneState(state DispatchState, prs []PullRequest, org string) DispatchStat } } state.Redispatches = redispatches + var starved []RevisitStarvation + for _, s := range state.RevisitStarved { + if openHeads[reviewKey(s.Repo, s.Number, s.HeadSHA, "")] { + starved = append(starved, s) + } + } + state.RevisitStarved = starved state = pruneReviewHeadState(state, openHeads) return state } diff --git a/src/pkg/review/model_stamps.go b/src/pkg/review/model_stamps.go index 756dc2336e..bd5ad2f032 100644 --- a/src/pkg/review/model_stamps.go +++ b/src/pkg/review/model_stamps.go @@ -12,6 +12,13 @@ type ModelStamp struct { // 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 + // BodyDigest is the PR title/body digest the dispatch recorded for this + // head; it fills a report that omitted body_digest so the aggregate can + // be compared against the PR's current digest (hivecommons/hive#14665). + // Empty, and never applied, when the head was dispatched under more + // than one digest: a digestless report cannot be attributed to either. + BodyDigest string + bodyDigestAmbiguous bool } // perspectiveStampKey is one review_model stamp's slot: the perspective, and @@ -25,8 +32,9 @@ type perspectiveStampKey struct { } // ModelStamps is keyed by reviewKey(repo, number, head, "") — the body digest -// is deliberately left out, since a report may omit it (foldDigestlessReports) -// while the models it should carry do not depend on it. +// is deliberately left out of the key, since a report may omit it +// (foldDigestlessReports) while the models it should carry do not depend on +// it; the digest itself is stamped from the dispatch record instead. type ModelStamps map[string]ModelStamp // ModelStampsFromState collects stamps from the dispatch state's pending and @@ -35,10 +43,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, r.DeltaPreviousHead) + out.record(r.Repo, r.Number, r.HeadSHA, r.Perspective, r.Head, r.AuthorModel, r.ReviewModel, r.DeltaPreviousHead, r.BodyDigest) } for _, p := range state.Pending { - out.record(p.Repo, p.Number, p.HeadSHA, p.Perspective, p.Head, p.AuthorModel, p.ReviewModel, p.DeltaPreviousHead) + out.record(p.Repo, p.Number, p.HeadSHA, p.Perspective, p.Head, p.AuthorModel, p.ReviewModel, p.DeltaPreviousHead, p.BodyDigest) } if len(out) == 0 { return nil @@ -46,9 +54,9 @@ func ModelStampsFromState(state DispatchState) ModelStamps { return out } -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 == "" { +func (m ModelStamps) record(repo string, number int, head string, perspective Perspective, reviewHead, authorModel, reviewModel, deltaPreviousHead, bodyDigest string) { + authorModel, reviewModel, deltaPreviousHead, bodyDigest = strings.TrimSpace(authorModel), strings.TrimSpace(reviewModel), strings.TrimSpace(deltaPreviousHead), strings.TrimSpace(bodyDigest) + if authorModel == "" && reviewModel == "" && deltaPreviousHead == "" && bodyDigest == "" { return } key := reviewKey(repo, number, head, "") @@ -56,6 +64,12 @@ func (m ModelStamps) record(repo string, number int, head string, perspective Pe if authorModel != "" { stamp.AuthorModel = authorModel } + if bodyDigest != "" { + if stamp.BodyDigest != "" && stamp.BodyDigest != bodyDigest { + stamp.bodyDigestAmbiguous = true + } + stamp.BodyDigest = bodyDigest + } if deltaPreviousHead != "" { stamp.DeltaPreviousHead = deltaPreviousHead } @@ -89,4 +103,7 @@ func (m ModelStamps) apply(report *PerspectiveReport) { if strings.TrimSpace(report.DeltaPreviousHead) == "" { report.DeltaPreviousHead = stamp.DeltaPreviousHead } + if strings.TrimSpace(report.BodyDigest) == "" && !stamp.bodyDigestAmbiguous { + report.BodyDigest = stamp.BodyDigest + } } diff --git a/src/pkg/review/revisit_body_digest_14665_test.go b/src/pkg/review/revisit_body_digest_14665_test.go new file mode 100644 index 0000000000..8c4d23ed57 --- /dev/null +++ b/src/pkg/review/revisit_body_digest_14665_test.go @@ -0,0 +1,202 @@ +package review + +import ( + "os" + "path/filepath" + "testing" + "time" +) + +// hivecommons/hive#14665: perspective reports may omit body_digest, and the +// planner read the gap as "the PR body changed since the verdict" — so every +// changes_requested verdict with digestless reports went back to the +// reviewers instead of the fixer, every cycle, with fix_kicks: 0. + +func revisitFixPR(t *testing.T, sha string) PullRequest { + t.Helper() + pr := dispatchPR(sha) + pr.Title = "fix: guard nil input" + pr.BodyDigest = PullRequestBodyDigest(pr.Title, "Closes #1") + pr.UpdatedAt = time.Date(2026, 10, 27, 18, 0, 0, 0, time.UTC) + return pr +} + +// qualifiedRevisitPR mirrors the planner's org-qualification of pr.Repo so +// state lookups keyed by repo match what PlanDispatch recorded. +func qualifiedRevisitPR(pr PullRequest, opts DispatchOptions) PullRequest { + pr.Repo = fullRepoName(pr.Repo, opts.ProjectOrg) + return pr +} + +func revisitFixOpts(now time.Time) DispatchOptions { + opts := fixGateOpts(false) + opts.Now = now + return opts +} + +func revisitFixAggregate(pr PullRequest, bodyDigest string, recordedAt time.Time) Aggregate { + agg := changesRequestedFor(pr) + agg.BodyDigest = bodyDigest + agg.RecordedAt = recordedAt + agg.Perspectives = map[Perspective]Verdict{PerspectiveCorrectness: VerdictChangesRequested} + return agg +} + +func TestMissingBodyDigestIsUnknownNotEdited(t *testing.T) { + pr := revisitFixPR(t, "sha1") + now := pr.UpdatedAt.Add(time.Hour) + // The verdict predates the PR's updated_at (a label change bumps it), so + // the timestamp fallback alone would also have called this an edit. + agg := revisitFixAggregate(pr, "", pr.UpdatedAt.Add(-time.Hour)) + + if bodyEditedVerdictRevisitable(pr, agg) { + t.Fatal("a verdict with no body digest must not read as body-edited") + } + plan := PlanDispatch([]PullRequest{pr}, Artifact{Items: []Aggregate{agg}}, DispatchState{}, revisitFixOpts(now)) + if len(plan.FixKicks) != 1 || len(plan.ReviewKicks) != 0 { + t.Fatalf("digestless changes_requested: fix=%d review=%d, want 1 / 0 (exclusions %+v)", len(plan.FixKicks), len(plan.ReviewKicks), plan.FixExclusions) + } + if len(plan.FixExclusions) != 0 { + t.Fatalf("fix exclusions = %+v, want none", plan.FixExclusions) + } +} + +func TestMatchingBodyDigestDispatchesFix(t *testing.T) { + pr := revisitFixPR(t, "sha1") + now := pr.UpdatedAt.Add(time.Hour) + agg := revisitFixAggregate(pr, pr.BodyDigest, pr.UpdatedAt.Add(-time.Hour)) + + plan := PlanDispatch([]PullRequest{pr}, Artifact{Items: []Aggregate{agg}}, DispatchState{}, revisitFixOpts(now)) + if len(plan.FixKicks) != 1 || len(plan.ReviewKicks) != 0 { + t.Fatalf("matching digest: fix=%d review=%d, want 1 / 0", len(plan.FixKicks), len(plan.ReviewKicks)) + } +} + +func TestDifferingBodyDigestRevisitsAndIsVisible(t *testing.T) { + pr := revisitFixPR(t, "sha1") + now := pr.UpdatedAt.Add(time.Hour) + agg := revisitFixAggregate(pr, PullRequestBodyDigest(pr.Title, "Refs #1"), pr.UpdatedAt.Add(-time.Hour)) + + plan := PlanDispatch([]PullRequest{pr}, Artifact{Items: []Aggregate{agg}}, DispatchState{}, revisitFixOpts(now)) + if len(plan.FixKicks) != 0 || len(plan.ReviewKicks) == 0 { + t.Fatalf("edited body: fix=%d review=%d, want 0 / >0", len(plan.FixKicks), len(plan.ReviewKicks)) + } + // The suppressed fix is not silent: the cycle names the PR and why. + if len(plan.FixExclusions) != 1 || plan.FixExclusions[0].Reason != FixDispatchExcludedRevisitBodyEdited || plan.FixExclusions[0].Attempts != 1 { + t.Fatalf("fix exclusions = %+v, want one %s at cycle 1", plan.FixExclusions, FixDispatchExcludedRevisitBodyEdited) + } + if len(plan.State.RevisitStarved) != 1 || plan.State.RevisitStarved[0].Cycles != 1 { + t.Fatalf("revisit starvation = %+v, want one head at 1 cycle", plan.State.RevisitStarved) + } +} + +// After RevisitStarvationCycles consecutive revisit cycles with no fix kick, +// the planner forces the fix path and resets the counter. +func TestRevisitStarvationForcesFixAfterNCycles(t *testing.T) { + pr := revisitFixPR(t, "sha1") + now := pr.UpdatedAt.Add(time.Hour) + agg := revisitFixAggregate(pr, PullRequestBodyDigest(pr.Title, "Refs #1"), pr.UpdatedAt.Add(-time.Hour)) + artifact := Artifact{Items: []Aggregate{agg}} + + state := DispatchState{} + for cycle := 1; cycle <= RevisitStarvationCycles; cycle++ { + opts := revisitFixOpts(now.Add(time.Duration(cycle) * time.Minute)) + plan := PlanDispatch([]PullRequest{pr}, artifact, state, opts) + if len(plan.FixKicks) != 0 { + t.Fatalf("cycle %d: fix kicked before the starvation limit", cycle) + } + if len(plan.ForcedFixes) != 0 { + t.Fatalf("cycle %d: forced fix before the starvation limit: %+v", cycle, plan.ForcedFixes) + } + if got := revisitStarvedCycles(plan.State, qualifiedRevisitPR(pr, opts)); got != cycle { + t.Fatalf("cycle %d: starved counter = %d", cycle, got) + } + if len(plan.FixExclusions) != 1 || plan.FixExclusions[0].Attempts != cycle { + t.Fatalf("cycle %d: exclusions = %+v", cycle, plan.FixExclusions) + } + state = plan.State + } + + plan := PlanDispatch([]PullRequest{pr}, artifact, state, revisitFixOpts(now.Add(time.Hour))) + if len(plan.FixKicks) != 1 { + t.Fatalf("after %d starved cycles the fix must be forced: fix=%d exclusions=%+v", RevisitStarvationCycles, len(plan.FixKicks), plan.FixExclusions) + } + if len(plan.ForcedFixes) != 1 || plan.ForcedFixes[0].Reason != FixDispatchExcludedRevisitBodyEdited || plan.ForcedFixes[0].Cycles != RevisitStarvationCycles { + t.Fatalf("forced fixes = %+v", plan.ForcedFixes) + } + if len(plan.State.RevisitStarved) != 0 { + t.Fatalf("starvation counter not reset after the forced fix: %+v", plan.State.RevisitStarved) + } + // The forced fix leaves the in-flight revisit's pending entries alone so + // the relay can still bind the re-review's verdicts. + if len(plan.State.Pending) == 0 { + t.Fatal("forced fix dropped the in-flight revisit's pending entries") + } +} + +// A head whose revisit is interrupted by a normal settle does not carry its +// count over; "consecutive" means consecutive. +func TestRevisitStarvationResetsOnSettledCycle(t *testing.T) { + pr := revisitFixPR(t, "sha1") + now := pr.UpdatedAt.Add(time.Hour) + edited := revisitFixAggregate(pr, PullRequestBodyDigest(pr.Title, "Refs #1"), pr.UpdatedAt.Add(-time.Hour)) + + opts := revisitFixOpts(now) + plan := PlanDispatch([]PullRequest{pr}, Artifact{Items: []Aggregate{edited}}, DispatchState{}, opts) + if revisitStarvedCycles(plan.State, qualifiedRevisitPR(pr, opts)) != 1 { + t.Fatalf("starved = %+v, want 1 cycle", plan.State.RevisitStarved) + } + settled := revisitFixAggregate(pr, pr.BodyDigest, now) + plan = PlanDispatch([]PullRequest{pr}, Artifact{Items: []Aggregate{settled}}, plan.State, revisitFixOpts(now.Add(time.Minute))) + if len(plan.FixKicks) != 1 || len(plan.State.RevisitStarved) != 0 { + t.Fatalf("settled cycle: fix=%d starved=%+v, want 1 / none", len(plan.FixKicks), plan.State.RevisitStarved) + } +} + +func TestRevisitStarvationPrunedWhenHeadMoves(t *testing.T) { + pr := revisitFixPR(t, "sha2") + state := DispatchState{RevisitStarved: []RevisitStarvation{{Repo: "acme/hive", Number: pr.Number, HeadSHA: "sha1", Cycles: 2}}} + plan := PlanDispatch([]PullRequest{pr}, Artifact{}, state, revisitFixOpts(pr.UpdatedAt)) + if len(plan.State.RevisitStarved) != 0 { + t.Fatalf("old head's starvation survived a push: %+v", plan.State.RevisitStarved) + } +} + +// Collect stamps a digestless report with the digest its dispatch recorded, +// so the verdict artifact carries what the reviewer was shown even when the +// model left body_digest out. +func TestCollectStampsBodyDigestFromDispatch(t *testing.T) { + dir := t.TempDir() + report := `{"lane":"review-swarm","kind":"review","findings":[],"prs_opened":[],"beads_filed":[],"summary":"ok","perspective":"security","verdict":"changes_requested","repo":"acme/hive","number":7,"head_sha":"sha1"}` + if err := os.WriteFile(filepath.Join(dir, ReviewReportFilePrefix+"security"+ReviewReportFileSuffix), []byte(report), 0o644); err != nil { + t.Fatal(err) + } + digest := PullRequestBodyDigest("fix: guard nil input", "Closes #1") + stamps := ModelStampsFromState(DispatchState{Pending: []PendingReview{{Repo: "acme/hive", Number: 7, HeadSHA: "sha1", BodyDigest: digest, Perspective: PerspectiveSecurity, Agent: "r1", Dispatched: time.Now()}}}) + art, err := Collect(dir, AggregateOptions{ModelStamps: stamps}) + if err != nil { + t.Fatal(err) + } + if len(art.Items) != 1 || art.Items[0].BodyDigest != digest { + t.Fatalf("artifact digest not stamped from dispatch: %+v", art.Items) + } + + // A report that wrote its own digest keeps it. + written := &PerspectiveReport{Repo: "acme/hive", Number: 7, HeadSHA: "sha1", BodyDigest: "own", Perspective: PerspectiveSecurity} + stamps.apply(written) + if written.BodyDigest != "own" { + t.Fatalf("reviewer-written digest overwritten: %q", written.BodyDigest) + } + + // A head dispatched under two digests cannot attribute a digestless + // report to either; it is left as written. + ambiguous := ModelStampsFromState(DispatchState{Pending: []PendingReview{ + {Repo: "acme/hive", Number: 7, HeadSHA: "sha1", BodyDigest: "d1", Perspective: PerspectiveSecurity, Agent: "r1"}, + {Repo: "acme/hive", Number: 7, HeadSHA: "sha1", BodyDigest: "d2", Perspective: PerspectiveCorrectness, Agent: "r1"}, + }}) + loose := &PerspectiveReport{Repo: "acme/hive", Number: 7, HeadSHA: "sha1", Perspective: PerspectiveSecurity} + ambiguous.apply(loose) + if loose.BodyDigest != "" { + t.Fatalf("ambiguous head stamped a digest: %q", loose.BodyDigest) + } +}