diff --git a/changelog.d/fixed-decision-answered-verdict.md b/changelog.d/fixed-decision-answered-verdict.md new file mode 100644 index 0000000000..7f2917a1e5 --- /dev/null +++ b/changelog.d/fixed-decision-answered-verdict.md @@ -0,0 +1 @@ +- A recorded `/hive decision` (or trusted CHANGES_REQUESTED review) on a PR head is now folded into the reviewer verdict for that head as a durable, head-scoped answer, so the stage-label reconciler's verdict route treats the `requires_human` / decision-requested verdict as **answered** on every tick and after a restart: instructions route to `070-fixing` with reason `contributor-lane:decision-answered`, a bare approve clears the hold, and only a new head with a fresh `requires_human` verdict asks again. Ends the 070↔085 flap seen on hive#14628, where the fix planner or a maintainer moved the PR to 070 and the next reconcile tick put it back ([#14679](https://github.com/hivecommons/hive/issues/14679)). diff --git a/src/docs/labels-and-control-signals.md b/src/docs/labels-and-control-signals.md index 75c66a2e22..e1f0ff5168 100644 --- a/src/docs/labels-and-control-signals.md +++ b/src/docs/labels-and-control-signals.md @@ -457,6 +457,7 @@ used to admit the PR: | CI pending, verdict approve | `060-reviewed` | | CI green, verdict approve, no modifier, yet the lane stopped on a reason no stage claims or tripped on a transient error (`review-state-check`, …) | `080-awaiting-merge-admission` (weak opinion: any lane's definite stage wins) | | a posted reviewer decision question (`decision_requested`) on a conflicting / not-mergeable / changes-requested head | `085-needs-decision`, reason `contributor-lane:decision-requested` — no fixer is kicked for such a head, and a conflict is moot if the answer is to close it ([#14607](https://github.com/hivecommons/hive/issues/14607)) | +| a trusted maintainer answered that question on the same head — `/hive decision ` or a CHANGES_REQUESTED review (recorded in `review-human-change-requests.json`, head-scoped) | `070-fixing`, reason `contributor-lane:decision-answered`: the answer is folded into the reviewer verdict for that head on every tick and after a restart, so the stage never re-derives 085 from the stale `requires_human` verdict. A bare `/hive decision approve` instead clears the hold like a trusted Approve review. A new head draws a new verdict with no answer and asks again ([#14679](https://github.com/hivecommons/hive/issues/14679)) | | CI red (`check-failing:*`, `required-check-failing:*`, `status-*`), `conflicting`, `not-mergeable`, `changes-requested`, verdict changes_requested/reject, `needs-rebase` present | `070-fixing` (self-authored lane or `author/hive`) / `071-needs-author` (`author/human`) / `072-returned-to-contributor` (`author/clanker`) — see [Stage 07x by author kind](#stage-07x-by-author-kind). A head whose App-linked commits carry a `Signed-off-by` the squash would misattribute to the PR author (`hive-signoff-attribution`) follows the same split: the branch owner is the only one who can rewrite them. | | `contributor-lane:soaking(...)` — and no red check on the head | `095-pre-merge-soak` | | `head-changed-since-eval`, `merge-failed`, `merge-queue-required` (the base branch's ruleset only accepts merges through GitHub's merge queue, so the direct REST merge is refused; logged as a skip rather than a merge error, [#14018](https://github.com/hivecommons/hive/issues/14018); the self-authored sweep then skips the direct merge on that base for 30 minutes and logs the skip once per PR head, [#14068](https://github.com/hivecommons/hive/issues/14068); the trusted-author, queued and contributor-lane sweeps skip it the same way), `required-checks-expected`, `behind`, `updated-branch`, `behind-update-budget-exhausted` (a behind self-authored PR the sweep did not update only because this tick's branch-update budget was spent; retried next tick, waits on the hive, [#12607](https://github.com/hivecommons/hive/issues/12607)), `release-hold*` — and no red check on the head | `100-merge-queue` | diff --git a/src/pkg/github/automerge/automerge_contributor_lane.go b/src/pkg/github/automerge/automerge_contributor_lane.go index 80b5799a76..3ef3443014 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane.go +++ b/src/pkg/github/automerge/automerge_contributor_lane.go @@ -71,6 +71,7 @@ import ( hgithub "github.com/hivecommons/hive/pkg/github" "github.com/hivecommons/hive/pkg/intent" "github.com/hivecommons/hive/pkg/issueclaim" + "github.com/hivecommons/hive/pkg/review" "github.com/hivecommons/hive/pkg/stageengine/prstage" "github.com/hivecommons/hive/pkg/stagelabels" ) @@ -1130,7 +1131,23 @@ func (c *Engine) contributorLaneVerdictFor(fullRepo string, number int, headSHA if c == nil || c.contributorLaneVerdict == nil { return ContributorLaneVerdict{} } - return normalizeContributorLaneVerdict(c.contributorLaneVerdict(fullRepo, number, headSHA)) + return c.withRecordedDecision(fullRepo, number, headSHA, normalizeContributorLaneVerdict(c.contributorLaneVerdict(fullRepo, number, headSHA))) +} + +// withRecordedDecision folds the trusted human's recorded decision on +// headSHA (review-human-change-requests.json) into the reviewer verdict, so +// a requires_human / decision-requested verdict the person already answered +// reads as answered on every tick and after every restart — never back to +// 085 until a new head draws a new verdict (hivecommons/hive#14679). +func (c *Engine) withRecordedDecision(fullRepo string, number int, headSHA string, v ContributorLaneVerdict) ContributorLaneVerdict { + if c == nil || c.humanChangeRequests == nil { + return v + } + req, ok := review.DecisionAnswerFor(c.humanChangeRequests, fullRepo, number, headSHA) + if !ok { + return v + } + return v.WithDecisionAnswer(strings.ToLower(req.Actor), headSHA, req.Directive()) } // contributorLaneEval is what the contributor lane established about one PR diff --git a/src/pkg/github/automerge/automerge_contributor_nudge.go b/src/pkg/github/automerge/automerge_contributor_nudge.go index e4b2dd0f4d..90f1639e32 100644 --- a/src/pkg/github/automerge/automerge_contributor_nudge.go +++ b/src/pkg/github/automerge/automerge_contributor_nudge.go @@ -110,6 +110,9 @@ func nudgeDiagnosis(reason string, d stageDetail) (checks, asks []string, human case r == autoMergeReasonHumanChangesRequested: asks = append(asks, "a maintainer requested changes; the hive is pushing the fix") human = true + case r == stageReasonDecisionAnswered: + asks = append(asks, "a maintainer answered the decision question with an instruction; the hive is pushing the fix") + human = true case r == contributorLaneReasonChangesRequested, r == stagelabels.LaneReasonChangesRequested: asks = append(asks, "the review swarm requested changes") case r == "conflicting" || r == "not-mergeable" || r == "label:needs-rebase": diff --git a/src/pkg/github/automerge/automerge_decision_answered_test.go b/src/pkg/github/automerge/automerge_decision_answered_test.go new file mode 100644 index 0000000000..00a56f8f5e --- /dev/null +++ b/src/pkg/github/automerge/automerge_decision_answered_test.go @@ -0,0 +1,95 @@ +package automerge + +import ( + "testing" + + "github.com/hivecommons/hive/pkg/review" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// hivecommons/hive#14679 (the 070↔085 flap on #14628): the reconcile tick +// re-derived 085 from the persisted decision-requested verdict because no +// lane had noted the owner's answer that cycle. The pass now folds the +// durable decision record into every observation's verdict before +// classification, so the tick yields 070 decision-answered — whatever the +// lanes reported and however the PR reached 070 (planner, manual label) — +// and a new head with a fresh requires_human verdict parks again. +func TestFoldRecordedDecisionAnswersVerdictRoute(t *testing.T) { + const head, newHead = "3b4a2b5510feb333579520508478acd0bc1b2ccc", "beefbeefbeefbeefbeefbeefbeefbeefbeefbeef" + asked := ContributorLaneVerdict{Configured: true, Verdict: "requires_human", DecisionRequested: true, HiveAuthoredKnown: true, HiveAuthored: true} + store := review.NewHumanChangeRequests("") + if err := store.Record(review.HumanChangeRequest{Repo: "hivecommons/hive", Number: 14628, HeadSHA: head, Source: review.HumanChangeRequestSourceDecision, SourceID: 6450262272, Actor: "clubanderson", Body: "approve\n1. Change `Closes #14605` to `Refs #14605`."}); err != nil { + t.Fatal(err) + } + c := &Engine{humanChangeRequests: store} + + for _, tc := range []struct { + name string + head string + labels []string + wantStage stagelabels.Stage + wantReason string + }{ + {name: "tick after the decision → 070 decision-answered", head: head, wantStage: stagelabels.Fixing, wantReason: stageReasonDecisionAnswered}, + {name: "manual 070 is not reverted", head: head, labels: []string{stagelabels.Fixing.Label()}, wantStage: stagelabels.Fixing, wantReason: stageReasonDecisionAnswered}, + {name: "new head, fresh requires_human → 085", head: newHead, wantStage: stagelabels.NeedsDecision, wantReason: stageReasonDecisionRequested}, + } { + t.Run(tc.name, func(t *testing.T) { + list := []stageObservation{ + {Lane: stageLaneSelf, Owner: "hivecommons", Repo: "hive", Number: 14628, HeadSHA: tc.head, Labels: tc.labels, Reason: contributorLaneReasonNoVerdict, Detail: stageDetail{VerdictKnown: true, Verdict: asked}}, + {Lane: stageLaneContributor, Owner: "hivecommons", Repo: "hive", Number: 14628, HeadSHA: tc.head, Labels: tc.labels, Reason: contributorLaneReasonAppAuthored, Detail: stageDetail{VerdictKnown: true, Verdict: asked}}, + } + c.foldRecordedDecision(list) + got, _, obs := resolveStage(list) + if got.Stage != tc.wantStage || got.Reason != tc.wantReason { + t.Fatalf("stage=%s reason=%q (lane %s), want %s %q", got.Stage, got.Reason, obs.Lane, tc.wantStage, tc.wantReason) + } + add, remove := stagelabels.Plan(tc.labels, stagelabels.Desired{Stage: got.Stage}) + if len(tc.labels) > 0 && (hasStageLabel(remove, stagelabels.Fixing.Label()) || hasStageLabel(add, stagelabels.NeedsDecision.Label())) { + t.Fatalf("manual 070 would be rewritten: add=%v remove=%v", add, remove) + } + }) + } +} + +// The lanes' own verdict lookup carries the same answer, so the review gate +// and the stage tracker never see an unanswered decision-requested verdict +// for a head the owner already answered; a bare approve reads as approve. +func TestContributorLaneVerdictForCarriesRecordedDecision(t *testing.T) { + const head = "3b4a2b5510feb333579520508478acd0bc1b2ccc" + asked := ContributorLaneVerdict{Configured: true, Verdict: "requires_human", DecisionRequested: true} + for _, tc := range []struct { + name, kind, wantDirective string + }{ + {name: "instruction", kind: review.HumanChangeRequestKindChanges, wantDirective: review.DecisionDirectiveChanges}, + {name: "bare approve", kind: review.HumanChangeRequestKindApprove, wantDirective: review.DecisionDirectiveApprove}, + } { + t.Run(tc.name, func(t *testing.T) { + store := review.NewHumanChangeRequests("") + if err := store.Record(review.HumanChangeRequest{Repo: "hivecommons/hive", Number: 14628, HeadSHA: head, Source: review.HumanChangeRequestSourceDecision, SourceID: 1, Actor: "ClubAnderson", Body: "approve", Kind: tc.kind}); err != nil { + t.Fatal(err) + } + c := &Engine{humanChangeRequests: store, contributorLaneVerdict: func(string, int, string) ContributorLaneVerdict { return asked }} + v := c.contributorLaneVerdictFor("hivecommons/hive", 14628, head) + if !v.DecisionAnswered || v.DecisionAnsweredBy != "clubanderson" || v.DecisionAnsweredHead != head || v.DecisionDirective != tc.wantDirective { + t.Fatalf("verdict = %+v", v) + } + if other := c.contributorLaneVerdictFor("hivecommons/hive", 14628, "0ther"); other.DecisionAnswered { + t.Fatalf("answer leaked onto another head: %+v", other) + } + o := stageObservation{Lane: stageLaneSelf, HeadSHA: head, Reason: contributorLaneReasonNoVerdict, Detail: stageDetail{VerdictKnown: true, Verdict: v}} + if got := classifyStage(o); got.Stage == stagelabels.NeedsDecision { + t.Fatalf("classified as %+v, want the question answered", got) + } + }) + } +} + +func hasStageLabel(list []string, want string) bool { + for _, l := range list { + if l == want { + return true + } + } + return false +} diff --git a/src/pkg/github/automerge/automerge_stage_labels.go b/src/pkg/github/automerge/automerge_stage_labels.go index 499ba84b66..6e687c20b3 100644 --- a/src/pkg/github/automerge/automerge_stage_labels.go +++ b/src/pkg/github/automerge/automerge_stage_labels.go @@ -31,6 +31,7 @@ import ( "github.com/hivecommons/hive/pkg/authorkind" "github.com/hivecommons/hive/pkg/delegatedecision" hgithub "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/review" "github.com/hivecommons/hive/pkg/stageengine" "github.com/hivecommons/hive/pkg/stageengine/prstage" "github.com/hivecommons/hive/pkg/stagelabels" @@ -462,6 +463,32 @@ func (c *Engine) noteStageHumanChangeRequest(owner, repo string, number int, hea }) } +// foldRecordedDecision stamps the trusted human's recorded decision on each +// observation's head into its verdict before classification. The lanes note +// the same answer when they reach it, but a lane that skips early (the +// contributor lane on an app-authored PR, a lane stopped at CI) never does, +// and the stage pass must not re-derive 085 from the stale decision-requested +// verdict in that case — that was the 070↔085 flap on hive#14628 +// (hivecommons/hive#14679). Reads the durable store, so it holds across +// restarts and across the drain that clears the tracker's details. +func (c *Engine) foldRecordedDecision(list []stageObservation) { + if c == nil || c.humanChangeRequests == nil { + return + } + for i := range list { + o := &list[i] + if o.HeadSHA == "" || (o.Detail.VerdictKnown && o.Detail.Verdict.DecisionAnswered && o.Detail.Verdict.DecisionAnsweredHead == o.HeadSHA) { + continue + } + req, ok := review.DecisionAnswerFor(c.humanChangeRequests, o.Owner+"/"+o.Repo, o.Number, o.HeadSHA) + if !ok { + continue + } + o.Detail.Verdict = o.Detail.Verdict.WithDecisionAnswer(strings.ToLower(req.Actor), o.HeadSHA, req.Directive()) + o.Detail = normalizeStageDetail(o.Detail) + } +} + // noteStageRole records the author's admission tier as the lane computed // it. Lanes disagree by design (the contributor lane reports "untrusted" // for a maintainer with no registry profile), so tiers merge by precedence @@ -615,6 +642,7 @@ const ( ciReasonRed = stageengine.CIReasonRed stageReasonVerdictRequiresHuman = stageengine.StageReasonVerdictRequiresHuman stageReasonDecisionRequested = stageengine.StageReasonDecisionRequested + stageReasonDecisionAnswered = stageengine.StageReasonDecisionAnswered ) func classifyCIReason(reason string) ciReasonKind { return stageengine.ClassifyCIReason(reason) } @@ -743,6 +771,7 @@ func (c *Engine) ApplyStageLabels(ctx context.Context) (StageLabelResult, error) continue } result.Items++ + c.foldRecordedDecision(list) verdict, role, obs := resolveStage(list) key := stageKey(obs.Owner, obs.Repo, obs.Number) if verdict.Rank == stageRankNone { @@ -949,7 +978,7 @@ func (c *Engine) ApplyStageLabels(ctx context.Context) (StageLabelResult, error) if obs.Detail.VerdictKnown { rawVerdict := obs.Detail.Verdict normalizedVerdict := normalizeContributorLaneVerdict(rawVerdict) - c.info("stage labels: reviewer verdict route", "repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA, "lane", obs.Lane, "raw_verdict", rawVerdict.Verdict, "normalized_verdict", normalizedVerdict.Verdict, "decision_requested", normalizedVerdict.DecisionRequested, "stage", verdict.Stage.String(), "reason", obs.Reason) + c.info("stage labels: reviewer verdict route", "repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA, "lane", obs.Lane, "raw_verdict", rawVerdict.Verdict, "normalized_verdict", normalizedVerdict.Verdict, "decision_requested", normalizedVerdict.DecisionRequested, "decision_answered", normalizedVerdict.DecisionAnswered && normalizedVerdict.DecisionAnsweredHead == obs.HeadSHA, "decision_by", normalizedVerdict.DecisionAnsweredBy, "decision_directive", normalizedVerdict.DecisionDirective, "stage", verdict.Stage.String(), "reason", obs.Reason) } add, remove := stagelabels.Plan(obs.Labels, want) if len(add)+len(remove) > 0 { diff --git a/src/pkg/review/human_change_request.go b/src/pkg/review/human_change_request.go index 6cd27863bc..02068bd375 100644 --- a/src/pkg/review/human_change_request.go +++ b/src/pkg/review/human_change_request.go @@ -75,6 +75,23 @@ func (r HumanChangeRequest) IsApproval() bool { return r.Kind == HumanChangeRequestKindApprove } +// Decision directives: what a recorded `/hive decision` told the hive to do +// with the head it names. They ride on the reviewer verdict for that head +// (prstage.ReviewVerdict.DecisionDirective) so every stage derivation reads +// the requires_human / decision-requested question as answered. +const ( + DecisionDirectiveApprove = "approve" + DecisionDirectiveChanges = "changes_requested" +) + +// Directive is the decision directive the request carries. +func (r HumanChangeRequest) Directive() string { + if r.IsApproval() { + return DecisionDirectiveApprove + } + return DecisionDirectiveChanges +} + // HumanChangeRequestStore records maintainer change requests and answers // which one, if any, stands against a PR head. Implementations are safe for // concurrent use: the automerge sweep records while the planner reads. @@ -252,6 +269,23 @@ func HumanChangeRequestFor(store HumanChangeRequestStore, repo string, number in return &req } +// DecisionAnswerFor returns the newest recorded trusted-human decision — +// instruction or bare approval, from a review or a `/hive decision` — that +// stands against pr's exact head. It is the durable, head-scoped fact that a +// reviewer verdict asking for a person on that head has been ANSWERED +// (hivecommons/hive#14679): a newer head carries no answer until someone +// gives one. +func DecisionAnswerFor(store HumanChangeRequestStore, repo string, number int, head string) (HumanChangeRequest, bool) { + if store == nil || strings.TrimSpace(head) == "" { + return HumanChangeRequest{}, false + } + req, ok := store.For(repo, number, strings.TrimSpace(head)) + if !ok || strings.TrimSpace(req.Actor) == "" { + return HumanChangeRequest{}, false + } + return req, true +} + // HumanApprovalFor returns the bare `/hive decision approve` standing // against pr's head, if the newest recorded decision on that head is one. func HumanApprovalFor(store HumanChangeRequestStore, repo string, number int, head string) *HumanChangeRequest { diff --git a/src/pkg/review/human_change_request_test.go b/src/pkg/review/human_change_request_test.go index 306dea45f7..01505f79d6 100644 --- a/src/pkg/review/human_change_request_test.go +++ b/src/pkg/review/human_change_request_test.go @@ -294,3 +294,33 @@ func TestHumanChangeRequestApprovalKindIsNotAFixRequest(t *testing.T) { t.Fatalf("superseded approval still standing: %+v", got) } } + +// The fix kick carries every line of the decision, not just the first: +// the numbered instructions under `/hive decision approve` on hive#14628 +// are the whole scope of the fix (hivecommons/hive#14679). +func TestHumanChangeRequestPromptSectionCarriesEveryDecisionLine(t *testing.T) { + req := liveCaseRequest() + req.Source, req.Body = HumanChangeRequestSourceDecision, "approve\n1. Change `Closes #14605` to `Refs #14605`.\n2. Keep #14605 open.\n3. Merge on green." + got := humanChangeRequestPromptSection(req) + for _, want := range []string{"> approve\n", "> 1. Change `Closes #14605` to `Refs #14605`.\n", "> 2. Keep #14605 open.\n", "> 3. Merge on green.\n", "via /hive decision"} { + if !strings.Contains(got, want) { + t.Fatalf("prompt section missing %q:\n%s", want, got) + } + } + if req.Directive() != DecisionDirectiveChanges { + t.Fatalf("directive = %q", req.Directive()) + } + if _, ok := DecisionAnswerFor(nil, liveCaseRepo, liveCaseNumber, liveCaseHead); ok { + t.Fatal("nil store answered") + } + store := NewHumanChangeRequests("") + if err := store.Record(req); err != nil { + t.Fatal(err) + } + if got, ok := DecisionAnswerFor(store, liveCaseRepo, liveCaseNumber, liveCaseHead); !ok || got.Actor != req.Actor || got.Directive() != DecisionDirectiveChanges { + t.Fatalf("DecisionAnswerFor = %+v ok=%v", got, ok) + } + if _, ok := DecisionAnswerFor(store, liveCaseRepo, liveCaseNumber, "0ther"); ok { + t.Fatal("answer leaked onto another head") + } +} diff --git a/src/pkg/stageengine/derive_pr.go b/src/pkg/stageengine/derive_pr.go index 2c948b0097..72af92e547 100644 --- a/src/pkg/stageengine/derive_pr.go +++ b/src/pkg/stageengine/derive_pr.go @@ -4,6 +4,7 @@ import ( "strings" "github.com/hivecommons/hive/pkg/authorkind" + "github.com/hivecommons/hive/pkg/stageengine/prstage" "github.com/hivecommons/hive/pkg/stagelabels" ) @@ -90,6 +91,7 @@ func FactsFromPRObservations(list []stageObservation) Facts { // reads: Approved ↔ VerdictApproved, the raw verdict string, the decision // request flag and a human approval that matches the head. func ReviewFactsFrom(d stageDetail, head string) ReviewFacts { + d = NormalizeStageDetail(d) r := ReviewFacts{ Verdict: d.Verdict.Verdict, VerdictKnown: d.VerdictKnown, @@ -103,6 +105,9 @@ func ReviewFactsFrom(d stageDetail, head string) ReviewFacts { if requester := strings.TrimSpace(d.HumanChangeRequester); requester != "" && strings.TrimSpace(d.HumanChangeRequestHead) != "" && strings.TrimSpace(d.HumanChangeRequestHead) == strings.TrimSpace(head) { r.HumanChangeRequester = requester } + if v := d.Verdict; d.VerdictKnown && v.DecisionAnswered && strings.TrimSpace(v.DecisionAnsweredHead) == strings.TrimSpace(head) { + r.DecisionAnswered, r.DecisionDirective = true, v.DecisionDirective + } r.State = VerdictStateFrom(d) return r } @@ -159,6 +164,13 @@ func ObservationsFromPRFacts(f Facts) []stageObservation { if pr.Review.HumanChangeRequester != "" && pr.HeadSHA != "" { detail.HumanChangeRequester, detail.HumanChangeRequestHead = pr.Review.HumanChangeRequester, pr.HeadSHA } + if pr.Review.DecisionAnswered && pr.HeadSHA != "" { + by := pr.Review.HumanChangeRequester + if pr.Review.DecisionDirective == prstage.DecisionDirectiveApprove { + by = pr.Review.HumanApprover + } + detail.Verdict = detail.Verdict.WithDecisionAnswer(by, pr.HeadSHA, pr.Review.DecisionDirective) + } if pr.DCOGate { detail.DCOGateHead = pr.HeadSHA } diff --git a/src/pkg/stageengine/derive_pr_classify.go b/src/pkg/stageengine/derive_pr_classify.go index c8ddae5b3d..bd7d1eaf47 100644 --- a/src/pkg/stageengine/derive_pr_classify.go +++ b/src/pkg/stageengine/derive_pr_classify.go @@ -127,13 +127,43 @@ const StageReasonVerdictRequiresHuman = "reviewer-verdict:requires_human" const StageReasonDecisionRequested = "contributor-lane:decision-requested" +// StageReasonDecisionAnswered is the fixing reason when a trusted human's +// `/hive decision ` answered the decision-requested / +// requires_human verdict on the head: 085 cleared because the person spoke, +// and the author agent is pushing the fix (hivecommons/hive#14679). +const StageReasonDecisionAnswered = "contributor-lane:decision-answered" + +// NormalizeStageDetail normalizes the reviewer verdict and folds a decision +// answer it carries into the detail's human approver / change requester, so +// every derivation — lane tracker, startup reconcile, dashboard — reads the +// answered question the same way whichever lane noted it. func NormalizeStageDetail(d stageDetail) stageDetail { if d.VerdictKnown { d.Verdict = normalizeContributorLaneVerdict(d.Verdict) + if v := d.Verdict; v.DecisionAnswered && v.DecisionAnsweredBy != "" && v.DecisionAnsweredHead != "" { + switch v.DecisionDirective { + case prstage.DecisionDirectiveApprove: + if d.HumanApprover == "" || d.HumanApprovalHead != v.DecisionAnsweredHead { + d.HumanApprover, d.HumanApprovalHead = v.DecisionAnsweredBy, v.DecisionAnsweredHead + } + default: + if d.HumanChangeRequester == "" || d.HumanChangeRequestHead != v.DecisionAnsweredHead { + d.HumanChangeRequester, d.HumanChangeRequestHead = v.DecisionAnsweredBy, v.DecisionAnsweredHead + } + } + } } return d } +// DecisionAnswered reports whether a recorded human decision stands against +// head and the reviewer verdict on that head asked for one. +func DecisionAnswered(d stageDetail, head string) bool { + d = NormalizeStageDetail(d) + v := d.Verdict + return d.VerdictKnown && v.DecisionAnswered && strings.TrimSpace(v.DecisionAnsweredHead) == strings.TrimSpace(head) && VerdictNeedsHuman(d) +} + // StageVerdictReason is the recorded reason when the reviewer verdict, not // the lane's skip reason, decided the stage. func StageVerdictReason(d stageDetail) string { @@ -182,6 +212,7 @@ func TrustedHumanApprovedRequiresHuman(d stageDetail, head string) bool { // PR belongs in fixing with the instruction, not parked on the question // it already answered (hivecommons/hive#14679, seen on #14628). func TrustedHumanChangeRequested(d stageDetail, head string) bool { + d = NormalizeStageDetail(d) return strings.TrimSpace(d.HumanChangeRequester) != "" && strings.TrimSpace(d.HumanChangeRequestHead) != "" && strings.TrimSpace(d.HumanChangeRequestHead) == strings.TrimSpace(head) } @@ -189,10 +220,15 @@ func TrustedHumanChangeRequested(d stageDetail, head string) bool { // request on the head carries: owned rank, reason human-changes-requested, // whatever the reviewer verdict on that head says. func HumanChangeRequestStage(o stageObservation) (stageVerdict, bool) { - if !TrustedHumanChangeRequested(o.Detail, o.HeadSHA) { + d := NormalizeStageDetail(o.Detail) + if !TrustedHumanChangeRequested(d, o.HeadSHA) { return stageVerdict{}, false } - return stageVerdict{Stage: FixingStage(o), Rank: stageRankOwned, Reason: autoMergeReasonHumanChangesRequested}, true + reason := autoMergeReasonHumanChangesRequested + if DecisionAnswered(d, o.HeadSHA) && d.Verdict.DecisionDirective != prstage.DecisionDirectiveApprove { + reason = StageReasonDecisionAnswered + } + return stageVerdict{Stage: FixingStage(o), Rank: stageRankOwned, Reason: reason}, true } func WithHumanApprovalReason(v stageVerdict, reason string) stageVerdict { @@ -266,7 +302,7 @@ var stageMergeQueueReasons = map[string]bool{ // stageFixingReasons are reasons an agent (or the author) must fix the head. var stageFixingReasons = map[string]bool{ - "changes-requested": true, autoMergeReasonHumanChangesRequested: true, contributorLaneReasonChangesRequested: true, "conflicting": true, "not-mergeable": true, + "changes-requested": true, autoMergeReasonHumanChangesRequested: true, StageReasonDecisionAnswered: true, contributorLaneReasonChangesRequested: true, "conflicting": true, "not-mergeable": true, "label:needs-rebase": true, autoMergeReasonForkNotEditable: true, } @@ -458,6 +494,7 @@ func ReviewGateStage(reason string, fixing stagelabels.Stage) (stageVerdict, boo // is requires_human / needs-decision and that no trusted human has approved // since; rank is the strength the caller's reason carries. func RequiresHumanStage(o stageObservation, rank int) (stageVerdict, bool) { + o.Detail = NormalizeStageDetail(o.Detail) if !VerdictNeedsHuman(o.Detail) || TrustedHumanApprovedRequiresHuman(o.Detail, o.HeadSHA) || TrustedHumanChangeRequested(o.Detail, o.HeadSHA) { return stageVerdict{}, false } diff --git a/src/pkg/stageengine/derive_pr_human_decision_test.go b/src/pkg/stageengine/derive_pr_human_decision_test.go index ab0af98306..865dafa957 100644 --- a/src/pkg/stageengine/derive_pr_human_decision_test.go +++ b/src/pkg/stageengine/derive_pr_human_decision_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/hivecommons/hive/pkg/stageengine/prstage" "github.com/hivecommons/hive/pkg/stagelabels" ) @@ -166,3 +167,76 @@ func (r deriveOnlyReconciler) ReconcileBatch(ctx context.Context, facts []Facts) } return out, nil } + +// hivecommons/hive#14679 (the 070↔085 flap on #14628): the decision answer +// rides on the reviewer verdict for the head, so a reconcile tick that only +// sees the verdict — no lane noted a requester this cycle — still derives +// fixing with reason decision-answered, never 085. A new head's fresh +// requires_human verdict carries no answer and parks again; a decision +// recorded for the old head does not leak onto the new one. +func TestClassifyStageDecisionAnsweredVerdictNeverParks(t *testing.T) { + const newHead = "beef5678newhead" + asked := ContributorLaneVerdict{Configured: true, Verdict: "requires_human", DecisionRequested: true} + answered := asked.WithDecisionAnswer("clubanderson", goldenHead, prstage.DecisionDirectiveChanges) + approved := asked.WithDecisionAnswer("clubanderson", goldenHead, prstage.DecisionDirectiveApprove) + fixing := FixingStage(stageObservation{Lane: stageLaneSelf}) + cases := []struct { + name string + head string + reason string + verdict ContributorLaneVerdict + labels []string + wantStage stagelabels.Stage + wantReason string + }{ + {name: "tick after the instruction → 070 decision-answered", head: goldenHead, reason: "ci-pending", verdict: answered, wantStage: fixing, wantReason: StageReasonDecisionAnswered}, + {name: "tick with the lane's own decision-requested reason → still 070", head: goldenHead, reason: StageReasonDecisionRequested, verdict: answered, wantStage: fixing, wantReason: StageReasonDecisionAnswered}, + {name: "manual 070 label stands while the decision stands", head: goldenHead, reason: "ci-pending", verdict: answered, labels: []string{stagelabels.Fixing.Label()}, wantStage: fixing, wantReason: StageReasonDecisionAnswered}, + {name: "bare approve → not parked", head: goldenHead, reason: "ci-pending", verdict: approved, wantStage: stagelabels.None}, + {name: "new head, fresh requires_human → 085 again", head: newHead, reason: "ci-pending", verdict: asked, wantStage: stagelabels.NeedsDecision, wantReason: StageReasonDecisionRequested}, + {name: "new head carrying the old head's answer → 085 again", head: newHead, reason: "ci-pending", verdict: answered, wantStage: stagelabels.NeedsDecision, wantReason: StageReasonDecisionRequested}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + o := stageObservation{Lane: stageLaneSelf, Reason: tc.reason, HeadSHA: tc.head, Labels: tc.labels, Detail: stageDetail{VerdictKnown: true, Verdict: tc.verdict}} + v := ClassifyStage(o) + if tc.wantStage == stagelabels.None { + if v.Stage == stagelabels.NeedsDecision || v.Stage == fixing { + t.Fatalf("classified as %+v, want the merge path", v) + } + return + } + if v.Stage != tc.wantStage || v.Reason != tc.wantReason { + t.Fatalf("classified as %+v, want stage=%s reason=%q", v, tc.wantStage, tc.wantReason) + } + if tc.wantStage == fixing && v.Human { + t.Fatalf("classified as %+v, want no human route", v) + } + // The derive path (startup reconcile, dashboard) reads the same + // answer through ReviewFacts. + facts := FactsFromPRObservations([]stageObservation{o}) + if facts.PR == nil { + t.Fatal("no PR facts") + } + if got := ClassifyStage(ObservationsFromPRFacts(facts)[0]); got.Stage != v.Stage || got.Reason != v.Reason { + t.Fatalf("derived %+v, want %+v", got, v) + } + }) + } +} + +func TestReviewFactsRoundTripDecisionAnswer(t *testing.T) { + asked := ContributorLaneVerdict{Configured: true, Verdict: "requires_human", DecisionRequested: true} + d := stageDetail{VerdictKnown: true, Verdict: asked.WithDecisionAnswer("clubanderson", goldenHead, prstage.DecisionDirectiveChanges)} + r := ReviewFactsFrom(d, goldenHead) + if !r.DecisionAnswered || r.DecisionDirective != prstage.DecisionDirectiveChanges || r.HumanChangeRequester != "clubanderson" { + t.Fatalf("ReviewFacts = %+v", r) + } + if r := ReviewFactsFrom(d, "0ther"); r.DecisionAnswered || r.HumanChangeRequester != "" { + t.Fatalf("answer leaked onto another head: %+v", r) + } + d = stageDetail{VerdictKnown: true, Verdict: asked.WithDecisionAnswer("clubanderson", goldenHead, prstage.DecisionDirectiveApprove)} + if r := ReviewFactsFrom(d, goldenHead); !r.DecisionAnswered || r.DecisionDirective != prstage.DecisionDirectiveApprove || r.HumanApprover != "clubanderson" || r.HumanChangeRequester != "" { + t.Fatalf("approve ReviewFacts = %+v", r) + } +} diff --git a/src/pkg/stageengine/facts.go b/src/pkg/stageengine/facts.go index 96844b1de1..061f14d715 100644 --- a/src/pkg/stageengine/facts.go +++ b/src/pkg/stageengine/facts.go @@ -280,6 +280,13 @@ type ReviewFacts struct { // reviewer-verdict:requires_human). Additive to the ADR-0025 shape: the // legacy verdict carries it as a separate flag the deriver reads. DecisionRequested bool + // DecisionAnswered is set when a trusted human answered the verdict on + // HeadSHA (`/hive decision` or a change-request review); DecisionDirective + // is approve or changes_requested. The answer is what keeps a + // decision-requested head out of 085 on every later derivation + // (hivecommons/hive#14679). + DecisionAnswered bool + DecisionDirective string } // LaneFact is one automerge lane's view of the PR this cycle. It is the diff --git a/src/pkg/stageengine/facts_sink.go b/src/pkg/stageengine/facts_sink.go index d70e0b7ac8..53aba0de3d 100644 --- a/src/pkg/stageengine/facts_sink.go +++ b/src/pkg/stageengine/facts_sink.go @@ -6,6 +6,7 @@ import ( "time" "github.com/hivecommons/hive/pkg/claims" + "github.com/hivecommons/hive/pkg/stageengine/prstage" "github.com/hivecommons/hive/pkg/stagelabels" ) @@ -221,7 +222,11 @@ func (s *factSink) PRDecided(ctx context.Context, ref Ref, d PRDecisionFact) (Tr lane = stageLaneTrusted } pr.Review.HumanChangeRequester = strings.TrimSpace(d.By) + pr.Review.DecisionAnswered, pr.Review.DecisionDirective = true, prstage.DecisionDirectiveChanges pr.Lanes = []LaneFact{{Lane: lane, Reason: autoMergeReasonHumanChangesRequested}} + } else if head != "" && strings.TrimSpace(d.By) != "" { + pr.Review.HumanApprover = strings.TrimSpace(d.By) + pr.Review.DecisionAnswered, pr.Review.DecisionDirective = true, prstage.DecisionDirectiveApprove } f.PR = pr if s.opts.Enrich != nil { diff --git a/src/pkg/stageengine/prstage/types.go b/src/pkg/stageengine/prstage/types.go index ffa75488c6..f8e5f9b2a7 100644 --- a/src/pkg/stageengine/prstage/types.go +++ b/src/pkg/stageengine/prstage/types.go @@ -186,6 +186,38 @@ type ReviewVerdict struct { // FixCycleCapReached preserves a human hold once review repair attempts are // exhausted, even if the review text also names concrete remediation. FixCycleCapReached bool + // DecisionAnswered is set when a trusted human answered this head's + // requires_human / decision-requested verdict — a `/hive decision` or a + // CHANGES_REQUESTED review recorded against DecisionAnsweredHead + // (hivecommons/hive#14679). The question is no longer open: a + // DecisionDirective of changes_requested routes to fixing with the + // instruction, approve lets the lane proceed. A new head produces a new + // verdict with no answer, so the stage asks again. + DecisionAnswered bool + DecisionAnsweredBy string + DecisionAnsweredHead string + DecisionDirective string +} + +// Decision directives, mirroring review.DecisionDirective*. +const ( + DecisionDirectiveApprove = "approve" + DecisionDirectiveChanges = "changes_requested" +) + +// WithDecisionAnswer returns v with the human's answer on head folded in. +func (v ReviewVerdict) WithDecisionAnswer(by, head, directive string) ReviewVerdict { + by, head = strings.TrimSpace(by), strings.TrimSpace(head) + if by == "" || head == "" { + return v + } + v.DecisionAnswered, v.DecisionAnsweredBy, v.DecisionAnsweredHead = true, by, head + if strings.TrimSpace(directive) == DecisionDirectiveApprove { + v.DecisionDirective = DecisionDirectiveApprove + } else { + v.DecisionDirective = DecisionDirectiveChanges + } + return v } func NormalizeReviewVerdict(v ReviewVerdict) ReviewVerdict { diff --git a/src/pkg/stageengine/prstage/types_test.go b/src/pkg/stageengine/prstage/types_test.go index 15163f9cef..41f36132da 100644 --- a/src/pkg/stageengine/prstage/types_test.go +++ b/src/pkg/stageengine/prstage/types_test.go @@ -1,6 +1,9 @@ package prstage -import "testing" +import ( + "reflect" + "testing" +) func TestDetailRedCheck(t *testing.T) { t.Parallel() @@ -62,3 +65,69 @@ func TestNormalizeReviewVerdict(t *testing.T) { }) } } + +func TestReviewVerdictWithDecisionAnswer(t *testing.T) { + t.Parallel() + base := ReviewVerdict{Verdict: "requires_human"} + cases := []struct { + name string + by, head string + directive string + want ReviewVerdict + }{ + { + name: "blank actor leaves verdict untouched", + by: " ", head: "abc", directive: DecisionDirectiveApprove, + want: base, + }, + { + name: "blank head leaves verdict untouched", + by: "alice", head: "", directive: DecisionDirectiveApprove, + want: base, + }, + { + name: "approve directive is preserved and trimmed", + by: " alice ", head: " abc ", directive: " approve ", + want: ReviewVerdict{ + Verdict: "requires_human", DecisionAnswered: true, + DecisionAnsweredBy: "alice", DecisionAnsweredHead: "abc", + DecisionDirective: DecisionDirectiveApprove, + }, + }, + { + name: "changes_requested directive", + by: "alice", head: "abc", directive: DecisionDirectiveChanges, + want: ReviewVerdict{ + Verdict: "requires_human", DecisionAnswered: true, + DecisionAnsweredBy: "alice", DecisionAnsweredHead: "abc", + DecisionDirective: DecisionDirectiveChanges, + }, + }, + { + name: "unknown directive defaults to changes_requested", + by: "alice", head: "abc", directive: "shrug", + want: ReviewVerdict{ + Verdict: "requires_human", DecisionAnswered: true, + DecisionAnsweredBy: "alice", DecisionAnsweredHead: "abc", + DecisionDirective: DecisionDirectiveChanges, + }, + }, + { + name: "empty directive defaults to changes_requested", + by: "alice", head: "abc", directive: "", + want: ReviewVerdict{ + Verdict: "requires_human", DecisionAnswered: true, + DecisionAnsweredBy: "alice", DecisionAnsweredHead: "abc", + DecisionDirective: DecisionDirectiveChanges, + }, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + if got := base.WithDecisionAnswer(tc.by, tc.head, tc.directive); !reflect.DeepEqual(got, tc.want) { + t.Fatalf("WithDecisionAnswer(%q,%q,%q) = %+v, want %+v", tc.by, tc.head, tc.directive, got, tc.want) + } + }) + } +}