diff --git a/changelog.d/fixed-review-wait-starvation.md b/changelog.d/fixed-review-wait-starvation.md new file mode 100644 index 0000000000..a2fd5052db --- /dev/null +++ b/changelog.d/fixed-review-wait-starvation.md @@ -0,0 +1 @@ +- Review dispatch now remembers heads it passed over for want of a free reviewer, promotes them ahead of webhook-prioritized PRs after three consecutive cycles, and the review queue plus Activity Monitor say how far the review got and how long the head has waited instead of a bare `stalled` pill (#15229). diff --git a/src/cmd/hive/main.go b/src/cmd/hive/main.go index bef18159f9..bdf8c953d9 100644 --- a/src/cmd/hive/main.go +++ b/src/cmd/hive/main.go @@ -11027,6 +11027,12 @@ func planReviewDispatchWith(ctx context.Context, cfg *config.Config, actionable 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) } + for _, w := range plan.Promoted { + logger.Warn("review dispatch promoted: head starved of a reviewer walked ahead of the queue", "repo", w.Repo, "pr", w.Number, "head", w.HeadSHA, "reason", w.Reason, "cycles", w.Cycles, "since", w.FirstAt, "limit", review.ReviewWaitStarvationCycles) + } + for _, w := range plan.Waiting { + logger.Info("review dispatch waiting: perspectives still owed, no reviewer this cycle", "repo", w.Repo, "pr", w.Number, "head", w.HeadSHA, "reason", w.Reason) + } 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)) } diff --git a/src/pkg/dashboard/stage_tracker.go b/src/pkg/dashboard/stage_tracker.go index 1aa86d53bc..92a238eb7e 100644 --- a/src/pkg/dashboard/stage_tracker.go +++ b/src/pkg/dashboard/stage_tracker.go @@ -1554,6 +1554,39 @@ func stageTrackerCaption(stage stagelabels.Stage, reason, author string, kind st return joinClause("No stage recorded yet", clause), "" } +// stageTrackerReviewWaitClause explains a review-pending head the swarm has +// not finished: how many perspectives have reported, and, when the planner +// has been passing the head over for lack of a reviewer, for how many +// cycles and since when (hivecommons/hive#15229). "" when neither is known. +func stageTrackerReviewWaitClause(agg *review.Aggregate, state review.DispatchState, fullRepo, repo string, number int, head string, now time.Time) string { + var parts []string + if agg != nil && agg.Verdict == review.VerdictIncomplete { + coverage := fmt.Sprintf("%s reported so far", plural(len(agg.Perspectives), "perspective")) + for _, r := range agg.Reasons { + if strings.HasPrefix(r, "review coverage incomplete") { + coverage = r + break + } + } + parts = append(parts, coverage) + } + wait, ok := state.WaitFor(fullRepo, number, head) + if !ok && fullRepo != repo { + wait, ok = state.WaitFor(repo, number, head) + } + if ok { + w := fmt.Sprintf("waiting for a free reviewer for %s", plural(wait.Cycles, "cycle")) + if !wait.FirstAt.IsZero() { + w += " since " + wait.FirstAt.UTC().Format("15:04") + " UTC" + } + if wait.Cycles >= review.ReviewWaitStarvationCycles { + w += "; promoted ahead of the queue" + } + parts = append(parts, w) + } + return strings.Join(parts, "; ") +} + // stageTrackerRegression fills the item's regression narration from the // record: the hop that opened the regression when History still has it, // else the record's fall time and current stage. @@ -1724,6 +1757,15 @@ func (s *Server) buildStageTracker(status *StatusPayload, store *stagelabels.Sto verdictArtifact = &art } verdictFor := stageTrackerVerdictLookup(verdictArtifact) + // The planner's wait records (review.DispatchState.Waits): a review- + // pending head the swarm keeps passing over for lack of a reviewer + // says so in its caption instead of a bare "stalled" pill + // (hivecommons/hive#15229). Unreadable state just leaves the caption + // as it was. + var dispatchState review.DispatchState + if st, err := review.LoadDispatchState(""); err == nil { + dispatchState = st + } sentinelLabel := s.sentinelLabel() lookup := func(repo string, number int) (stagelabels.Record, bool) { if store == nil { @@ -1975,6 +2017,11 @@ func (s *Server) buildStageTracker(status *StatusPayload, store *stagelabels.Sto if a, ok := verdictFor(repoName, pr.Number, pr.HeadSHA); ok { agg = &a } + if it.Stage == trackerNumberReviewPending && it.Superseded == nil { + if clause := stageTrackerReviewWaitClause(agg, dispatchState, qualifyRepo(repoName), repoName, pr.Number, pr.HeadSHA, now); clause != "" { + it.Caption = joinClause(it.Caption, clause) + } + } it.DecisionNeeded = stageTrackerDecisionNeededAt(it, agg, sentinelLabel, now) it.StageActor = stageTrackerStageActor(it, workers, now) add(it) diff --git a/src/pkg/dashboard/stage_tracker_review_wait_test.go b/src/pkg/dashboard/stage_tracker_review_wait_test.go new file mode 100644 index 0000000000..b6033aa96e --- /dev/null +++ b/src/pkg/dashboard/stage_tracker_review_wait_test.go @@ -0,0 +1,44 @@ +package dashboard + +import ( + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/review" +) + +// hivecommons/hive#15229: a review-pending row whose head the swarm keeps +// passing over says how far the review got and how long it has waited, +// instead of a bare "stalled" pill. +func TestStageTrackerReviewWaitClauseNamesCoverageAndWait(t *testing.T) { + now := time.Date(2026, 10, 29, 1, 30, 0, 0, time.UTC) + agg := &review.Aggregate{ + Verdict: review.VerdictIncomplete, + Perspectives: map[review.Perspective]review.Verdict{review.PerspectiveSecurity: review.VerdictApprove, review.PerspectiveIntentAlignment: review.VerdictApprove}, + Reasons: []string{"review coverage incomplete: missing correctness, style, docs-currency"}, + } + state := review.DispatchState{Waits: []review.ReviewWaitRecord{{ + Repo: "hivecommons/hive", Number: 15139, HeadSHA: "cf5adb194", Reason: review.ReviewWaitNoFreeReviewer, + Cycles: review.ReviewWaitStarvationCycles + 1, FirstAt: now.Add(-40 * time.Minute), + }}} + got := stageTrackerReviewWaitClause(agg, state, "hivecommons/hive", "hive", 15139, "cf5adb194", now) + want := "review coverage incomplete: missing correctness, style, docs-currency; waiting for a free reviewer for 4 cycles since 00:50 UTC; promoted ahead of the queue" + if got != want { + t.Fatalf("clause = %q, want %q", got, want) + } + + // The record is keyed by the short repo name on an older state file. + state.Waits[0].Repo = "hive" + if got := stageTrackerReviewWaitClause(nil, state, "hivecommons/hive", "hive", 15139, "cf5adb194", now); !strings.HasPrefix(got, "waiting for a free reviewer for 4 cycles") { + t.Fatalf("short-repo clause = %q", got) + } + + if got := stageTrackerReviewWaitClause(nil, review.DispatchState{}, "hivecommons/hive", "hive", 15139, "cf5adb194", now); got != "" { + t.Fatalf("nothing known: clause = %q, want empty", got) + } + settled := &review.Aggregate{Verdict: review.VerdictApprove} + if got := stageTrackerReviewWaitClause(settled, review.DispatchState{}, "hivecommons/hive", "hive", 15139, "cf5adb194", now); got != "" { + t.Fatalf("settled verdict: clause = %q, want empty", got) + } +} diff --git a/src/pkg/github/review_queue.go b/src/pkg/github/review_queue.go index a71808531d..6a863de493 100644 --- a/src/pkg/github/review_queue.go +++ b/src/pkg/github/review_queue.go @@ -535,13 +535,46 @@ func reviewQueueDispatchReason(e *ReviewQueueEntry, pr PullRequest, full string, if exhausted != "" { return "review dispatch exhausted: " + strings.TrimPrefix(exhausted, review.HoldReasonDispatchExhausted+": ") + "; waiting on a human" } + wait, waiting := opts.Dispatch.WaitFor(full, pr.Number, pr.HeadSHA) + if !waiting && full != pr.Repo { + wait, waiting = opts.Dispatch.WaitFor(pr.Repo, pr.Number, pr.HeadSHA) + } if since.IsZero() { + if waiting { + return reviewQueueWaitReason(wait, now) + } return "" } reason := fmt.Sprintf("review dispatched %s ago, no verdict yet", reviewQueueDispatchAge(now.Sub(since))) if count > 0 { reason += fmt.Sprintf(" (re-dispatched %dx after expiry)", count) } + if waiting { + reason += "; " + reviewQueueWaitReason(wait, now) + } + return reason +} + +// reviewQueueWaitReason explains a head the planner keeps passing over: the +// perspectives it still owes have had no reviewer for this many consecutive +// cycles (hivecommons/hive#15229). Past review.ReviewWaitStarvationCycles the +// planner walks the head ahead of the queue, and the line says so. +func reviewQueueWaitReason(w review.ReviewWaitRecord, now time.Time) string { + cause := "every eligible reviewer busy" + if w.Reason == review.ReviewWaitNoParallelSlot { + cause = "max_parallel_reviews spent" + } + reason := fmt.Sprintf("review waiting for a free reviewer: %d consecutive cycle", w.Cycles) + if w.Cycles != 1 { + reason += "s" + } + if !w.FirstAt.IsZero() { + reason += fmt.Sprintf(" over %s", reviewQueueDispatchAge(now.Sub(w.FirstAt))) + } + reason += " (" + cause + ")" + if w.Cycles >= review.ReviewWaitStarvationCycles { + reason += "; promoted ahead of the queue, takes the next free reviewer" + } return reason } diff --git a/src/pkg/github/review_queue_test.go b/src/pkg/github/review_queue_test.go index 2074ccf753..20ee4cf7fc 100644 --- a/src/pkg/github/review_queue_test.go +++ b/src/pkg/github/review_queue_test.go @@ -690,3 +690,29 @@ func TestReviewQueueStageFollowsStagePolicy(t *testing.T) { t.Fatalf("require_quiet=false: 2h since entry stalls, quiet 30m is not past dwell; got stalled=%v past_dwell=%v", st.Stalled, st.PastDwell) } } + +// hivecommons/hive#15229: a head the planner keeps passing over for lack of +// a reviewer says so, instead of showing no dispatch reason at all. +func TestReviewQueueExplainsAHeadWaitingForAFreeReviewer(t *testing.T) { + opened := reviewQueueTestNow.Add(-2 * time.Hour) + prs := []PullRequest{ + {Repo: "acme/a", Number: 1, Title: "fix: starved", HeadSHA: "h1", CIStatus: "success", CreatedAt: opened}, + {Repo: "acme/a", Number: 2, Title: "fix: first wait", HeadSHA: "h2", CIStatus: "success", CreatedAt: opened}, + } + q := BuildReviewQueue(prs, ReviewQueueOptions{ + Org: "acme", + Now: reviewQueueTestNow, + Dispatch: review.DispatchState{Waits: []review.ReviewWaitRecord{ + {Repo: "acme/a", Number: 1, HeadSHA: "h1", Reason: review.ReviewWaitNoFreeReviewer, Cycles: review.ReviewWaitStarvationCycles + 2, FirstAt: reviewQueueTestNow.Add(-50 * time.Minute)}, + {Repo: "acme/a", Number: 2, HeadSHA: "h2", Reason: review.ReviewWaitNoParallelSlot, Cycles: 1, FirstAt: reviewQueueTestNow.Add(-time.Minute)}, + }}, + }) + starved := findEntry(t, q, "acme/a", 1) + if !hasReason(starved, "review waiting for a free reviewer: 5 consecutive cycles over 50m (every eligible reviewer busy); promoted ahead of the queue, takes the next free reviewer") { + t.Fatalf("starved reasons = %v", starved.Reasons) + } + first := findEntry(t, q, "acme/a", 2) + if !hasReason(first, "review waiting for a free reviewer: 1 consecutive cycle over 1m (max_parallel_reviews spent)") || hasReason(first, "promoted") { + t.Fatalf("first-wait reasons = %v", first.Reasons) + } +} diff --git a/src/pkg/review/dispatch.go b/src/pkg/review/dispatch.go index a3b6e00f67..0a62a09747 100644 --- a/src/pkg/review/dispatch.go +++ b/src/pkg/review/dispatch.go @@ -285,8 +285,37 @@ type DispatchState struct { // 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"` + // Waits records, per head, the consecutive cycles in which the head + // still owed perspectives but got no reviewer (plan.Waiting). Without + // it a half-reviewed head could starve indefinitely behind the webhook + // PRs the caller moves to the front of every cycle, and the stall was + // invisible: nothing recorded that the head had been passed over. At + // ReviewWaitStarvationCycles the planner moves the head ahead of the + // rest of the list; the record is dropped once it is dispatched or + // settled. + Waits []ReviewWaitRecord `json:"review_waits,omitempty"` +} + +// ReviewWaitRecord is the durable form of ReviewWait: the per-head count of +// consecutive cycles a head with perspectives still owed got no reviewer. +type ReviewWaitRecord struct { + Repo string `json:"repo"` + Number int `json:"number"` + HeadSHA string `json:"head_sha,omitempty"` + Reason string `json:"reason"` + Cycles int `json:"cycles"` + FirstAt time.Time `json:"first_at"` + LastAt time.Time `json:"last_at"` } +// ReviewWaitStarvationCycles is how many consecutive cycles a head may wait +// for a reviewer before the planner walks it ahead of every other PR, +// including the webhook-event PRs the caller promoted. Three cycles is long +// enough that an ordinary busy cycle never reorders the queue, and short +// enough that a head is not left half-reviewed for an hour while fresher PRs +// keep taking the free reviewers (hivecommons/hive#15229). +const ReviewWaitStarvationCycles = 3 + // RevisitStarvation is the per-head counter behind RevisitStarvationCycles. type RevisitStarvation struct { Repo string `json:"repo"` @@ -510,7 +539,11 @@ type DispatchPlan struct { // spent. They are the reviewer autoscaler's demand signal // (hivecommons/hive#14685): one entry per PR, on its current head. Waiting []ReviewWait - State DispatchState + // Promoted are the heads this cycle walked ahead of the rest of the + // list because they had waited ReviewWaitStarvationCycles or more + // consecutive cycles for a reviewer. The caller logs each one. + Promoted []ReviewWaitRecord + State DispatchState } // ReviewWait is one PR left waiting for a reviewer this cycle. @@ -633,6 +666,7 @@ func PlanDispatch(prs []PullRequest, artifact Artifact, state DispatchState, opt maxParallel = DefaultMaxParallelReviews } availableSlots := maxParallel + prs, plan.Promoted = promoteStarvedWaits(prs, plan.State.Waits, opts.ProjectOrg) for _, pr := range prs { pr.Repo = fullRepoName(pr.Repo, opts.ProjectOrg) if pr.Number <= 0 || pr.Repo == "" { @@ -934,9 +968,101 @@ func PlanDispatch(prs []PullRequest, artifact Artifact, state DispatchState, opt availableSlots-- } } + plan.State.Waits = reconcileWaits(plan.State.Waits, plan.Waiting, opts.ProjectOrg, now) return plan } +// promoteStarvedWaits returns prs with every head whose wait record has +// reached ReviewWaitStarvationCycles moved to the front, in their existing +// relative order, and the records it promoted. The caller has usually +// already moved webhook-event PRs to the front; a head that has been passed +// over for that many cycles goes ahead of them, so a steady stream of fresh +// events cannot leave a half-reviewed head waiting indefinitely +// (hivecommons/hive#15229). Every gate in PlanDispatch still applies. +func promoteStarvedWaits(prs []PullRequest, waits []ReviewWaitRecord, org string) ([]PullRequest, []ReviewWaitRecord) { + if len(waits) == 0 || len(prs) < 2 { + return prs, nil + } + starved := make([]bool, len(prs)) + var promoted []ReviewWaitRecord + for i, pr := range prs { + repo := fullRepoName(pr.Repo, org) + for _, w := range waits { + if w.Cycles >= ReviewWaitStarvationCycles && samePRHead(w.Repo, w.Number, w.HeadSHA, repo, pr.Number, pr.HeadSHA) { + starved[i] = true + promoted = append(promoted, w) + break + } + } + } + if len(promoted) == 0 { + return prs, nil + } + out := make([]PullRequest, 0, len(prs)) + for i, pr := range prs { + if starved[i] { + out = append(out, pr) + } + } + for i, pr := range prs { + if !starved[i] { + out = append(out, pr) + } + } + return out, promoted +} + +// reconcileWaits folds this cycle's Waiting into the durable wait records: +// a head still waiting gains a cycle; a head that was waiting and now is +// not (dispatched or settled) loses its record. Records for PRs no longer +// open were already dropped by pruneState. +func reconcileWaits(records []ReviewWaitRecord, waiting []ReviewWait, org string, now time.Time) []ReviewWaitRecord { + stillWaiting := make(map[string]ReviewWait, len(waiting)) + for _, w := range waiting { + stillWaiting[reviewKey(fullRepoName(w.Repo, org), w.Number, w.HeadSHA, "")] = w + } + out := make([]ReviewWaitRecord, 0, len(waiting)) + seen := map[string]bool{} + for _, r := range records { + key := reviewKey(fullRepoName(r.Repo, org), r.Number, r.HeadSHA, "") + w, waits := stillWaiting[key] + if !waits { + continue + } + r.Cycles++ + r.Reason = w.Reason + r.LastAt = now + if r.FirstAt.IsZero() { + r.FirstAt = now + } + seen[key] = true + out = append(out, r) + } + for _, w := range waiting { + key := reviewKey(fullRepoName(w.Repo, org), w.Number, w.HeadSHA, "") + if seen[key] { + continue + } + seen[key] = true + out = append(out, ReviewWaitRecord{Repo: fullRepoName(w.Repo, org), Number: w.Number, HeadSHA: w.HeadSHA, Reason: w.Reason, Cycles: 1, FirstAt: now, LastAt: now}) + } + if len(out) == 0 { + return nil + } + return out +} + +// WaitFor returns the wait record for this PR head, if the planner has been +// passing it over for lack of a reviewer. +func (s DispatchState) WaitFor(repo string, number int, headSHA string) (ReviewWaitRecord, bool) { + for _, w := range s.Waits { + if samePRHead(w.Repo, w.Number, w.HeadSHA, repo, number, headSHA) { + return w, true + } + } + return ReviewWaitRecord{}, false +} + // deltaPreviousHead is the head a delta kick is judged against, "" for a // full review. func deltaPreviousHead(d *DeltaContext) string { @@ -1905,6 +2031,13 @@ func pruneState(state DispatchState, prs []PullRequest, org string) DispatchStat } } state.RevisitStarved = starved + var waits []ReviewWaitRecord + for _, w := range state.Waits { + if openHeads[reviewKey(w.Repo, w.Number, w.HeadSHA, "")] { + waits = append(waits, w) + } + } + state.Waits = waits state = pruneReviewHeadState(state, openHeads) return state } diff --git a/src/pkg/review/dispatch_waiting_test.go b/src/pkg/review/dispatch_waiting_test.go index 1eee7b8274..de13e70c5d 100644 --- a/src/pkg/review/dispatch_waiting_test.go +++ b/src/pkg/review/dispatch_waiting_test.go @@ -2,6 +2,7 @@ package review import ( "testing" + "time" ) // hivecommons/hive#14685: the planner reports the PRs it could not hand to a @@ -94,3 +95,115 @@ func TestDrainingReviewerGetsNoNewReview(t *testing.T) { t.Fatalf("waiting = %+v", plan.Waiting) } } + +// hivecommons/hive#15229: a head passed over for lack of a reviewer is +// remembered across cycles, and after ReviewWaitStarvationCycles it walks +// ahead of the PRs the caller put in front of it. + +func TestPlanDispatchRecordsConsecutiveWaitCycles(t *testing.T) { + now := time.Date(2026, 10, 29, 0, 0, 0, 0, time.UTC) + opts := DispatchOptions{ + RequireApproval: true, + FanOut: true, + CombinedPerspectives: true, + MaxParallelReviews: 10, + ProjectOrg: "acme", + Agents: []AgentCapability{reviewer("r1")}, + Now: now, + } + prs := []PullRequest{dispatchPRNum(1, "sha1"), dispatchPRNum(2, "sha2")} + plan := PlanDispatch(prs, Artifact{}, DispatchState{}, opts) + w, ok := plan.State.WaitFor("acme/hive", 2, "sha2") + if !ok || w.Cycles != 1 || !w.FirstAt.Equal(now) || w.Reason != ReviewWaitNoFreeReviewer { + t.Fatalf("first cycle wait = %+v (ok=%v), want cycles 1 at %v", w, ok, now) + } + if _, ok := plan.State.WaitFor("acme/hive", 1, "sha1"); ok { + t.Fatalf("dispatched PR 1 must not carry a wait record: %+v", plan.State.Waits) + } + + // r1 is still busy with PR 1 next cycle; PR 2 waits again. + opts.Now = now.Add(2 * time.Minute) + plan = PlanDispatch(prs, Artifact{}, plan.State, opts) + w, _ = plan.State.WaitFor("acme/hive", 2, "sha2") + if w.Cycles != 2 || !w.FirstAt.Equal(now) || !w.LastAt.Equal(opts.Now) { + t.Fatalf("second cycle wait = %+v, want cycles 2 first %v last %v", w, now, opts.Now) + } + + // PR 1's review landed: its pending entry is gone, r1 is free, PR 2 is + // dispatched and its wait record is dropped. + state := plan.State + state.Pending = removePendingForHead(state.Pending, PullRequest{Repo: "acme/hive", Number: 1, HeadSHA: "sha1"}) + opts.Now = now.Add(4 * time.Minute) + plan = PlanDispatch([]PullRequest{dispatchPRNum(2, "sha2")}, Artifact{}, state, opts) + if len(plan.ReviewKicks) != 1 || plan.ReviewKicks[0].Number != 2 { + t.Fatalf("kicks = %+v, want PR 2", plan.ReviewKicks) + } + if len(plan.State.Waits) != 0 { + t.Fatalf("waits after dispatch = %+v, want none", plan.State.Waits) + } +} + +func TestPlanDispatchPromotesStarvedHeadAheadOfTheList(t *testing.T) { + now := time.Date(2026, 10, 29, 0, 0, 0, 0, time.UTC) + opts := DispatchOptions{ + RequireApproval: true, + FanOut: true, + CombinedPerspectives: true, + MaxParallelReviews: 10, + ProjectOrg: "acme", + Agents: []AgentCapability{reviewer("r1")}, + Now: now, + } + // The caller has put the fresh event PR 9 first; PR 2 has already been + // passed over ReviewWaitStarvationCycles times. + prs := []PullRequest{dispatchPRNum(9, "sha9"), dispatchPRNum(2, "sha2")} + state := DispatchState{Waits: []ReviewWaitRecord{{Repo: "acme/hive", Number: 2, HeadSHA: "sha2", Reason: ReviewWaitNoFreeReviewer, Cycles: ReviewWaitStarvationCycles, FirstAt: now.Add(-10 * time.Minute)}}} + plan := PlanDispatch(prs, Artifact{}, state, opts) + if len(plan.ReviewKicks) != 1 || plan.ReviewKicks[0].Number != 2 { + t.Fatalf("kicks = %+v, want the starved PR 2 first", plan.ReviewKicks) + } + if len(plan.Promoted) != 1 || plan.Promoted[0].Number != 2 || plan.Promoted[0].Cycles != ReviewWaitStarvationCycles { + t.Fatalf("promoted = %+v", plan.Promoted) + } + if got := waitingNumbers(plan); len(got) != 1 || got[9] != ReviewWaitNoFreeReviewer { + t.Fatalf("waiting = %+v, want PR 9", plan.Waiting) + } + if _, ok := plan.State.WaitFor("acme/hive", 2, "sha2"); ok { + t.Fatalf("promoted and dispatched head still has a wait record: %+v", plan.State.Waits) + } +} + +func TestPlanDispatchDoesNotPromoteBelowStarvationThreshold(t *testing.T) { + opts := DispatchOptions{ + RequireApproval: true, + FanOut: true, + CombinedPerspectives: true, + MaxParallelReviews: 10, + ProjectOrg: "acme", + Agents: []AgentCapability{reviewer("r1")}, + Now: time.Date(2026, 10, 29, 0, 0, 0, 0, time.UTC), + } + prs := []PullRequest{dispatchPRNum(9, "sha9"), dispatchPRNum(2, "sha2")} + state := DispatchState{Waits: []ReviewWaitRecord{{Repo: "acme/hive", Number: 2, HeadSHA: "sha2", Reason: ReviewWaitNoFreeReviewer, Cycles: ReviewWaitStarvationCycles - 1}}} + plan := PlanDispatch(prs, Artifact{}, state, opts) + if len(plan.ReviewKicks) != 1 || plan.ReviewKicks[0].Number != 9 { + t.Fatalf("kicks = %+v, want list order kept (PR 9)", plan.ReviewKicks) + } + if len(plan.Promoted) != 0 { + t.Fatalf("promoted = %+v, want none", plan.Promoted) + } + if w, _ := plan.State.WaitFor("acme/hive", 2, "sha2"); w.Cycles != ReviewWaitStarvationCycles { + t.Fatalf("wait cycles = %d, want %d", w.Cycles, ReviewWaitStarvationCycles) + } +} + +func TestPruneStateDropsWaitRecordsForClosedHeads(t *testing.T) { + state := DispatchState{Waits: []ReviewWaitRecord{ + {Repo: "acme/hive", Number: 2, HeadSHA: "sha2", Cycles: 2}, + {Repo: "acme/hive", Number: 3, HeadSHA: "old", Cycles: 5}, + }} + got := pruneState(state, []PullRequest{dispatchPRNum(2, "sha2"), dispatchPRNum(3, "new")}, "acme") + if len(got.Waits) != 1 || got.Waits[0].Number != 2 { + t.Fatalf("waits = %+v, want only PR 2 on its open head", got.Waits) + } +}