From ad572a19f1163a8337d0b4f66a6f9373c7f2a9bf Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Wed, 28 Oct 2026 20:22:06 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Let=20hive=20decide=20on=20a=20r?= =?UTF-8?q?eady-for-human-merge=20PR=20approves=20and=20lets=20the=20sweep?= =?UTF-8?q?=20merge=20it?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A contributor PR at stage/090 with a good review, green CI and a mergeable head sat behind "I can't merge this" when a person delegated it. The hive now decides: approve (the Approve — let hive merge path — APPROVE review, automerge label, head admitted as operator-approval:) so the merge sweep merges the PR under its own number after soak. Adoption in supersede mode is the fallback only when the head cannot merge as it is (review not good, CI red, conflicting, sign-off missing). The decision carries the path in plain words for the ledger and the ✓ hover. Fixes #15173 Signed-off-by: Andrew Anderson --- .../fixed-delegate-human-merge-approves.md | 1 + src/docs/dashboard-sections.md | 2 +- src/docs/operator-reference.md | 18 +++ .../api_repo_item_decide_delegated.go | 47 +++++++ .../api_repo_item_decide_delegated_test.go | 119 +++++++++++++++++- .../delegated_decision_human_merge.go | 28 ++++- src/pkg/dashboard/operator_actions.go | 27 ++-- .../stage_tracker_acted_path_test.go | 38 ++++++ src/pkg/dashboard/static/index.html | 2 + src/pkg/delegatedecision/decision_glossary.go | 4 + .../decision_glossary_test.go | 3 +- src/pkg/delegatedecision/human_merge.go | 98 +++++++++++++++ src/pkg/delegatedecision/human_merge_test.go | 95 ++++++++++++++ src/pkg/delegatedecision/policy.go | 12 ++ 14 files changed, 478 insertions(+), 16 deletions(-) create mode 100644 changelog.d/fixed-delegate-human-merge-approves.md create mode 100644 src/pkg/dashboard/stage_tracker_acted_path_test.go create mode 100644 src/pkg/delegatedecision/human_merge.go create mode 100644 src/pkg/delegatedecision/human_merge_test.go diff --git a/changelog.d/fixed-delegate-human-merge-approves.md b/changelog.d/fixed-delegate-human-merge-approves.md new file mode 100644 index 0000000000..468bdd3c60 --- /dev/null +++ b/changelog.d/fixed-delegate-human-merge-approves.md @@ -0,0 +1 @@ +- "Let hive decide" on a contributor PR at *Ready for human merge · untrusted author* now approves and lets the merge sweep merge the PR under its own number when the review is good, CI is green and the head is mergeable (the same path as *Approve — let hive merge*, with the head admitted as the person's trust override); adoption in supersede mode is only the fallback for a head that cannot merge as it is (review not good, CI red, conflicting, sign-off missing). The chosen path is in the decision reason, so the ✓ hover reads *the hive decided: approve · review passed, CI green, mergeable*. Before, the hive only answered "I can't merge this". (#15173) diff --git a/src/docs/dashboard-sections.md b/src/docs/dashboard-sections.md index 8eccaaafc2..3dfaec4309 100644 --- a/src/docs/dashboard-sections.md +++ b/src/docs/dashboard-sections.md @@ -739,7 +739,7 @@ The glyph is drawn for **every** row on the gate, so two rows in the same state | State the pill shows | Lead actions in ⋯ and in the popover (consequence) | |---|---| -| PR at 090 *Ready for human merge* (untrusted author, workflow files, manual-merge path, branch protection…) | **Approve & merge as maintainer** (merges #N into *base* now via hive's identity, attributed to you — a comment on the PR names you, the audit log records `repo_item_decision_merge`, `source: dashboard`) · **Approve — let hive merge** (records your approval as the owner decision — an APPROVE review and the automerge label; the hive's merge sweep merges it on its next pass once CI is green and the head has soaked) · Give direction (hive-authored PRs only) · Close as superseded · Close as won't fix · Hold. Both merge actions need the merger or owner role and confirm first (with *Don't ask again*); the toast reads *merged* / *queued for merge*; the badge records `merge-now` / `queue-merge`. **Let hive decide** is here too (third): on a 090 PR it never runs the policy — the hive answers at once through the no-effect toast, *I can't merge this: author @Karibhou is untrusted on hotshot — …*, with *Approve & merge as maintainer · Approve — let hive merge* as buttons and the 👥 Contributors fix in the next line. **Either Approve is the trust override for that head** ([#15136](https://github.com/hivecommons/hive/issues/15136); owner, 2026-10-29: "I approved these and then they went to adopted…"): the stage record gets `admitted_by: operator-approval:`, the contributor lane merges the head as-is after checks and soak, and the adoption engine refuses to adopt or supersede it while the admission stands (the row reads *not adopted: a maintainer approved this head; the hive merges it as-is*; the 090 tooltip and popover say *Approved by @you as the owner decision…* and the second Approve greys out). When the record's commit check found commits without a `Signed-off-by`, the popover's blocker line and both Approve confirms say so **before** acting (*2 of 3 commits have no Signed-off-by, so a required DCO check refuses the merge*) and a third lead action appears, **Re-author as hive PR** — the explicit, separate choice: the hive re-homes the commits on a signed-off hive branch and opens a superseding PR; nothing is merged as-is. It is greyed with the reason on a person's PR. Renderer test: `TestStageTrackerReauthorAndApprovalCaveats`. | +| PR at 090 *Ready for human merge* (untrusted author, workflow files, manual-merge path, branch protection…) | **Approve & merge as maintainer** (merges #N into *base* now via hive's identity, attributed to you — a comment on the PR names you, the audit log records `repo_item_decision_merge`, `source: dashboard`) · **Approve — let hive merge** (records your approval as the owner decision — an APPROVE review and the automerge label; the hive's merge sweep merges it on its next pass once CI is green and the head has soaked) · Give direction (hive-authored PRs only) · Close as superseded · Close as won't fix · Hold. Both merge actions need the merger or owner role and confirm first (with *Don't ask again*); the toast reads *merged* / *queued for merge*; the badge records `merge-now` / `queue-merge`. **Let hive decide** is here too (third). On a contributor PR at 090 the hive decides itself ([#15173](https://github.com/hivecommons/hive/issues/15173); live: hive#15118, hotshot#317 — both Karibhou, MERGEABLE, 0 failing checks, yet the old answer was only *I can't merge this*): when the reviewer's verdict for the head is good (approve, or merge-eligible with nothing for a person — no verdict recorded counts as good, since the lane itself found nothing wrong at 090), CI is green, the head is not conflicting and no sign-off is missing, the decision is **approve** — the same path as *Approve — let hive merge*: an APPROVE review + the automerge label, the head admitted as `operator-approval:`, and the merge sweep merges the PR **under its own number** after soak; it is never copied into a hive-owned PR. Only when the head cannot merge as it is — review not good, CI red, conflicting (needs a rebase), sign-off missing — does the hive fall back to **adopt in supersede mode** (`human-merge-adopt`), re-homing the commits on its own branch and fixing what blocks the merge. The chosen path is in the decision reason, so the ✓ hover reads *the hive decided: approve · review passed, CI green, mergeable (rule: human merge approve)* or *… adopt-supersede · CI red, conflicting, needs rebase (rule: human merge adopt)*. The plain-words hand-back — *I can't merge this: author @Karibhou is untrusted on hotshot — …*, with *Approve & merge as maintainer · Approve — let hive merge* as buttons and the 👥 Contributors fix in the next line — remains only when the hive has no way forward of its own: a hive-authored PR, or a head that cannot merge as-is on a hive with `governor.labels.stage.adoption` off. Tests: `TestDecideHumanMergePaths`, `TestDelegatedDecisionReadyForHumanMergeApprovesWhenGreen`, `…AdoptsWhenNotMergeable`, `TestStageTrackerActedPlainDecisionPathJS`. **Either Approve is the trust override for that head** ([#15136](https://github.com/hivecommons/hive/issues/15136); owner, 2026-10-29: "I approved these and then they went to adopted…"): the stage record gets `admitted_by: operator-approval:`, the contributor lane merges the head as-is after checks and soak, and the adoption engine refuses to adopt or supersede it while the admission stands (the row reads *not adopted: a maintainer approved this head; the hive merges it as-is*; the 090 tooltip and popover say *Approved by @you as the owner decision…* and the second Approve greys out). When the record's commit check found commits without a `Signed-off-by`, the popover's blocker line and both Approve confirms say so **before** acting (*2 of 3 commits have no Signed-off-by, so a required DCO check refuses the merge*) and a third lead action appears, **Re-author as hive PR** — the explicit, separate choice: the hive re-homes the commits on a signed-off hive branch and opens a superseding PR; nothing is merged as-is. It is greyed with the reason on a person's PR. Renderer test: `TestStageTrackerReauthorAndApprovalCaveats`. | | PR at 085 *Needs human decision* | Let hive decide · Approve · Give direction · Close as superseded · Close as won't fix · Hold (unchanged). | | Issue at 045 *Needs decision* (direction) | Let hive decide · Give direction · Approve direction · Send back to the hive · Hold (unchanged). | | Human-authored PR at 071 *Needs author* / 072 *Returned to contributor* | Let hive decide · Close as superseded · Close as won't fix · Hold — the hive never rewrites a person's PR, so the choice is wait or close. (A hive-authored PR there carries *Adopt now* when adoption applies.) | diff --git a/src/docs/operator-reference.md b/src/docs/operator-reference.md index de89a34999..084ca14805 100644 --- a/src/docs/operator-reference.md +++ b/src/docs/operator-reference.md @@ -498,6 +498,24 @@ re-authoring and is not refused. |---|---|---| | `re-author` — *Re-author as hive PR* | Offered as its own lead action on a 090 PR whose head cannot merge as-is — the stage record's commit check counted commits without a `Signed-off-by` (`dco_unsigned` / `dco_total` on the row). The popover and both Approve confirms say so **before** acting (*2 of 3 commits have no Signed-off-by, so a required DCO check refuses the merge*); approving never re-authors by itself. Re-author posts its own note and the `adoption/supersede` request label; the hive re-homes the commits on a hive-owned, signed-off branch and opens a superseding PR. Disabled with the reason on a person's PR (the hive does not re-author a human's PR — ask them to `git commit -s --amend`). | `repo_item_decision_adopt` with `decision=re-author`; `last_operator_action.action: re-author`; toast *adopted*. | +**Let hive decide on a 090 PR takes the approve path when it can +([#15173](https://github.com/hivecommons/hive/issues/15173)).** Delegating a +contributor PR at *Ready for human merge* runs `DecideHumanMerge`: with the +reviewer's verdict for the head good (approve, or merge-eligible with +nothing for a person; no recorded verdict counts as good — the lane found +nothing wrong at 090), CI green, no conflict and no missing sign-off, the +rule is `human-merge-approve` — the `queue-merge` path above (APPROVE +review, automerge label, `admitted_by: operator-approval:`), so the +merge sweep merges the PR **under its own number** after soak, never a +hive-owned copy. Otherwise the rule is `human-merge-adopt`: adopt in +supersede mode, with the reasons in the decision (`review not good`, `CI +red`, `conflicting, needs rebase`, `sign-off missing`). The decision's +`path` is in the audit detail and the ledger line — *hive decided: approve · +review passed, CI green, mergeable (human-merge-approve)* — and the ✓ hover +renders it in plain words. The hive hands back with *I can't merge this…* +only for a hive-authored PR, or when the head cannot merge as-is and +`governor.labels.stage.adoption` is off. + Neither merge action widens who the lanes trust: to stop a contributor's PRs parking at 090 give them the `trusted` / `merger` tier under 👥 Contributors (`auto_merge.contributor_lane.require_tiers`), add them as a repository diff --git a/src/pkg/dashboard/api_repo_item_decide_delegated.go b/src/pkg/dashboard/api_repo_item_decide_delegated.go index 5c6a51c237..ff959cba4c 100644 --- a/src/pkg/dashboard/api_repo_item_decide_delegated.go +++ b/src/pkg/dashboard/api_repo_item_decide_delegated.go @@ -18,6 +18,7 @@ import ( "github.com/hivecommons/hive/pkg/config" "github.com/hivecommons/hive/pkg/delegatedecision" ghpkg "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/review" "github.com/hivecommons/hive/pkg/stagelabels" ) @@ -487,6 +488,9 @@ func (s *Server) decideDelegatedOne(ctx context.Context, repoFull string, number return out } d, deciderNotes := s.decideDelegated(ctx, in) + if hm, ok := s.delegatedHumanMergeDecision(in); ok { + d, deciderNotes = hm, []string{"ready for human merge: " + hm.Path} + } // An adoption whose copy was closed unmerged (hivecommons/hive#15052) // overrides the policy: the recorded decision no longer stands, so the // answer is to re-adopt, not "already decided". @@ -594,6 +598,7 @@ func (s *Server) gatherDelegatedInput(ctx context.Context, repoFull string, numb if err := s.gatherDelegatedPRContext(ctx, repoFull, pr, &in); err != nil { return in, err } + in.HeadSHA = strings.TrimSpace(pr.GetHead().GetSHA()) state := strings.ToLower(pr.GetMergeableState()) in.Conflicting = state == "dirty" || (pr.Mergeable != nil && !pr.GetMergeable()) green, err := delegatedHeadCIGreen(ctx, api, owner, repoName, pr, in.Conflicting, state) @@ -630,9 +635,41 @@ func (s *Server) gatherDelegatedInput(ctx context.Context, repoFull string, numb in.AdoptionStalled = strings.HasPrefix(lowerReason, stagelabels.LaneReasonAdoptionStalledPrefix) in.ReporterTrustHold = strings.Contains(lowerReason, "reporter-trust") || strings.Contains(lowerReason, "reporter trust") in.DCOMissing = strings.Contains(lowerReason, "dco") + s.gatherDelegatedHumanMergeInput(&in) return in, nil } +// gatherDelegatedHumanMergeInput fills what DecideHumanMerge reads for a +// PR at 090 (hivecommons/hive#15173): the reviewer's verdict for the head +// from the review artifact, and the stage record's sign-off check. Both +// are local reads; a missing artifact or record leaves the fields unknown. +func (s *Server) gatherDelegatedHumanMergeInput(in *delegatedecision.Input) { + if in.Kind != delegatedecision.KindPR || !slices.Contains(in.Labels, stagelabels.ReadyForHumanMerge.Label()) { + return + } + in.ReadyForHumanMerge = true + if art, err := review.LoadArtifact(""); err == nil { + if agg, ok := stageTrackerVerdictLookup(&art)(in.Repo, in.Number, in.HeadSHA); ok { + in.ReviewKnown = true + in.ReviewGood = delegatedReviewGood(agg) + } + } + if rec, ok := s.stageStoreOrShared().Get(stagelabels.Key(in.Repo, in.Number)); ok { + if _, unsigned, ok := rec.DCOFor(in.HeadSHA); ok && unsigned > 0 { + in.DCOMissing = true + } + } +} + +// delegatedReviewGood: the reviewer approved the head, or found it +// merge-eligible with nothing for a person and no close recommendation. +func delegatedReviewGood(agg review.Aggregate) bool { + if agg.RequiresHuman || agg.CloseRecommended { + return false + } + return agg.Verdict == review.VerdictApprove || agg.MergeEligible +} + // delegatedHeadCIGreen decides Input.CIGreen for the PR under decision. // mergeable_state alone cannot carry it: GitHub reports "blocked" both for a // missing required review and for red required checks, and "unknown" / @@ -728,6 +765,16 @@ func (s *Server) executeDelegatedDecision(ctx context.Context, in delegatedecisi } notes = append(notes, "`/lgtm` + `/approve` recorded; the merge gate takes it from here") } + // The delegated approval of an untrusted author's PR is the + // person's trust override for this head (hivecommons/hive#15136, + // #15173): the contributor lane merges it as it is instead of + // adopting it. A PR at 090 is untrusted by the lane's own + // judgement, whatever the author's repo permission says. + if !in.HiveAuthored && (in.ReadyForHumanMerge || !in.TrustedAuthor) { + if by := s.admitPullRequestHead(repoFull, number, in.HeadSHA, user); by != "" { + notes = append(notes, "head admitted as "+by+"; the contributor lane merges it as it is, no adoption") + } + } case delegatedecision.ActionLabelsAdd: for _, l := range a.Labels { color, desc := delegatedLabelMeta(l) diff --git a/src/pkg/dashboard/api_repo_item_decide_delegated_test.go b/src/pkg/dashboard/api_repo_item_decide_delegated_test.go index f272f9ba05..811fff1257 100644 --- a/src/pkg/dashboard/api_repo_item_decide_delegated_test.go +++ b/src/pkg/dashboard/api_repo_item_decide_delegated_test.go @@ -13,6 +13,7 @@ import ( "github.com/hivecommons/hive/pkg/config" "github.com/hivecommons/hive/pkg/delegatedecision" "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/stagelabels" ) func newDelegatedServer(t *testing.T, mock *repoItemDecisionMock) *Server { @@ -595,13 +596,109 @@ func TestDelegatedStuckWhyReadsTheLane(t *testing.T) { } } -// "Let hive decide" on a PR at 090 Ready for human merge answers at once -// (hivecommons/hive#15078): no policy run, no comment, a no-effect result -// that says in plain words why the hive will not merge it and carries the -// merge answers as buttons — enabled for a merger, greyed for a writer. -func TestDelegatedDecisionReadyForHumanMergeAnswersWithMergeOptions(t *testing.T) { +// "Let hive decide" on a contributor PR at 090 Ready for human merge +// (hivecommons/hive#15173): when review, CI and mergeability are good the +// hive approves and the merge sweep merges the PR under its own number — +// the head is admitted (hivecommons/hive#15136) so the lane merges it as +// it is and never adopts it; the ledger says the path in plain words. +func TestDelegatedDecisionReadyForHumanMergeApprovesWhenGreen(t *testing.T) { mock := &repoItemDecisionMock{prForeign: true, prLabels: []string{"stage/090-ready-for-human-merge", "role/newcomer"}} srv := newDelegatedServer(t, mock) + seedAdmissionRecord(t, srv, stagelabels.ReadyForHumanMerge) + srv.operatorActionStore = newOperatorActionStore("") + body := `{"type":"pr","reason":"contributor-lane:author-association","caption":"Ready for human merge · untrusted author"}` + w := postDelegated(t, srv, 8, "writer", body, map[string]string{"X-Hive-Role": "merger"}) + if w.Code != http.StatusOK { + t.Fatalf("code=%d body=%s", w.Code, w.Body.String()) + } + job := decodeDelegatedJob(t, w) + if job.Status != delegatedJobStatusDone || job.Decision == nil || job.Result == nil { + t.Fatalf("job = %+v", job) + } + d := job.Decision + if d.Verdict != delegatedecision.VerdictApprove || d.Rule != delegatedecision.RuleHumanMergeApprove || d.KeepsPark { + t.Fatalf("decision = %+v", d) + } + if d.Path != "review: lane found nothing wrong, CI green, mergeable" { + t.Errorf("path = %q", d.Path) + } + if job.Result.Action == rowActionNoEffect || strings.Contains(job.Result.Outcome, "I can't merge this") { + t.Fatalf("the hive must decide, not hand back: %+v", job.Result) + } + if len(mock.reviews) != 1 || len(mock.merges) != 0 { + t.Fatalf("expected one Approve review and no merge: reviews=%v merges=%v", mock.reviews, mock.merges) + } + if strings.Contains(strings.Join(mock.added, ","), delegatedecision.LabelAdoptSupersede) { + t.Fatalf("a green PR must not be adopted: %v", mock.added) + } + rec, ok := srv.stageStore.Get(stagelabels.Key("testorg/testrepo", 8)) + if !ok || rec.Admission("feedfacefeedfacefeedfacefeedfacefeedface") != stagelabels.OperatorApprovalAdmission("writer") { + t.Fatalf("head not admitted: ok=%v rec=%+v", ok, rec) + } + notes := strings.Join(job.Notes, "\n") + for _, want := range []string{"ready for human merge: review: lane found nothing wrong, CI green, mergeable", "head admitted as operator-approval:writer"} { + if !strings.Contains(notes, want) { + t.Errorf("notes %q missing %q", notes, want) + } + } + got := srv.operatorActions().List("testorg/testrepo", 8) + if len(got) != 1 || got[0].Outcome != operatorActionOutcomeApplied { + t.Fatalf("ledger = %+v", got) + } + if want := "hive decided: approve · review: lane found nothing wrong, CI green, mergeable (human-merge-approve)"; got[0].Detail != want { + t.Errorf("ledger detail = %q, want %q", got[0].Detail, want) + } +} + +// Red CI or a conflicting head at 090 cannot merge as it is: the hive adopts +// in supersede mode (the fallback), and the path names why. +func TestDelegatedDecisionReadyForHumanMergeAdoptsWhenNotMergeable(t *testing.T) { + cases := []struct { + name string + mock *repoItemDecisionMock + path string + }{ + {"red CI", &repoItemDecisionMock{prForeign: true, prLabels: []string{"stage/090-ready-for-human-merge"}, prMergeState: "blocked", prRedCI: true}, "CI red"}, + {"conflicting", &repoItemDecisionMock{prForeign: true, prLabels: []string{"stage/090-ready-for-human-merge"}, prMergeState: "dirty"}, "CI red, conflicting, needs rebase"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + srv := newDelegatedServer(t, tc.mock) + seedAdmissionRecord(t, srv, stagelabels.ReadyForHumanMerge) + srv.deps.Config.Governor.Labels.Stage.Adoption.Enabled = boolPtr(true) + w := postDelegated(t, srv, 8, "writer", `{"type":"pr","reason":"contributor-lane:author-association"}`, map[string]string{"X-Hive-Role": "merger"}) + if w.Code != http.StatusOK { + t.Fatalf("code=%d body=%s", w.Code, w.Body.String()) + } + job := decodeDelegatedJob(t, w) + if job.Decision == nil || job.Decision.Verdict != delegatedecision.VerdictAdoptSupersede || job.Decision.Rule != delegatedecision.RuleHumanMergeAdopt { + t.Fatalf("decision = %+v", job.Decision) + } + if job.Decision.Path != tc.path { + t.Errorf("path = %q, want %q", job.Decision.Path, tc.path) + } + if !strings.Contains(strings.Join(tc.mock.added, ","), delegatedecision.LabelAdoptSupersede) { + t.Fatalf("adoption not queued: %v", tc.mock.added) + } + if len(tc.mock.reviews) != 0 || len(tc.mock.merges) != 0 { + t.Fatalf("nothing may be approved or merged: reviews=%v merges=%v", tc.mock.reviews, tc.mock.merges) + } + if rec, _ := srv.stageStore.Get(stagelabels.Key("testorg/testrepo", 8)); rec.AdmittedBy != "" { + t.Fatalf("an adopted head must not be admitted: %q", rec.AdmittedBy) + } + }) + } +} + +// When the head cannot merge as it is and adoption is off on this hive, the +// hive has no way forward of its own: it answers at once, in plain words, +// with the merge buttons (hivecommons/hive#15078) — enabled for a merger, +// greyed for a writer — and writes nothing. +func TestDelegatedDecisionReadyForHumanMergeAnswersWithMergeOptions(t *testing.T) { + mock := &repoItemDecisionMock{prForeign: true, prLabels: []string{"stage/090-ready-for-human-merge", "role/newcomer"}, prMergeState: "blocked", prRedCI: true} + srv := newDelegatedServer(t, mock) + off := false + srv.deps.Config.Governor.Labels.Stage.Adoption.Enabled = &off body := `{"type":"pr","reason":"contributor-lane:author-association","caption":"Ready for human merge · untrusted author"}` w := postDelegated(t, srv, 8, "writer", body, map[string]string{"X-Hive-Role": "merger"}) if w.Code != http.StatusOK { @@ -648,6 +745,18 @@ func TestDelegatedDecisionReadyForHumanMergeAnswersWithMergeOptions(t *testing.T } } +// The ledger line for a delegated decision carries the path in plain words +// before the rule slug, so the ✓ hover reads "the hive decided: approve · +// review passed, CI green, mergeable (rule: human merge approve)". +func TestOperatorActionDecidedDetail(t *testing.T) { + if got, want := operatorActionDecidedDetail("approve", "human-merge-approve", "review passed, CI green, mergeable"), "hive decided: approve · review passed, CI green, mergeable (human-merge-approve)"; got != want { + t.Errorf("got %q want %q", got, want) + } + if got, want := operatorActionDecidedDetail("approve", "default-release", ""), "hive decided: approve (default-release)"; got != want { + t.Errorf("got %q want %q", got, want) + } +} + // All nested PR jobs already running is a fast finish with no GitHub calls. // The async parent still needs a settled ledger entry rather than an orphaned // pending entry (hivecommons/hive#15064). diff --git a/src/pkg/dashboard/delegated_decision_human_merge.go b/src/pkg/dashboard/delegated_decision_human_merge.go index 2f3be62aaa..63c6917cdb 100644 --- a/src/pkg/dashboard/delegated_decision_human_merge.go +++ b/src/pkg/dashboard/delegated_decision_human_merge.go @@ -56,12 +56,38 @@ func delegatedHumanMergeOptions(in delegatedecision.Input, number int, mergerRol // decisionWhyMergerRole is the plain reason a merge button is greyed. const decisionWhyMergerRole = "merger role required" +// delegatedHumanMergeDecision is the policy's answer for a 090 PR +// (hivecommons/hive#15173), or ok=false when the hive should instead say in +// plain words that a person has to act: the PR is not a contributor PR the +// lanes can take over, or the head cannot merge as it is and adoption — +// the only other way forward — is off on this hive. +func (s *Server) delegatedHumanMergeDecision(in delegatedecision.Input) (delegatedecision.Decision, bool) { + if in.HiveAuthored { + return delegatedecision.Decision{}, false + } + d, ok := delegatedecision.RunHumanMerge(in) + if !ok { + return delegatedecision.Decision{}, false + } + if d.Verdict == delegatedecision.VerdictAdoptSupersede && !s.stageAdoptionEnabled() { + return delegatedecision.Decision{}, false + } + return d, true +} + // delegatedHumanMergeOutcome answers "Let hive decide" on a PR wearing the -// 090 label without running the policy. ok=false for anything else. +// 090 label without running the owner-delegate policy. ok=false for +// anything else — and for a contributor PR the hive can decide itself: +// approve when review, CI and mergeability are good, adopt when the head +// cannot merge as it is (hivecommons/hive#15173); those run through the +// normal decide → execute path. func (s *Server) delegatedHumanMergeOutcome(repoFull string, number int, itemType, user string, mergerRole bool, in delegatedecision.Input, req delegatedDecisionRequest) (delegatedOutcome, bool) { if in.Kind != delegatedecision.KindPR || !slices.Contains(in.Labels, stagelabels.ReadyForHumanMerge.Label()) { return delegatedOutcome{}, false } + if _, ok := s.delegatedHumanMergeDecision(in); ok { + return delegatedOutcome{}, false + } reason := delegatedHumanMergeReason(in, req) base, _ := stagelabels.HumanApprovedReason(reason) base = strings.TrimSpace(base) diff --git a/src/pkg/dashboard/operator_actions.go b/src/pkg/dashboard/operator_actions.go index 712d03bb77..ee9aa51e1d 100644 --- a/src/pkg/dashboard/operator_actions.go +++ b/src/pkg/dashboard/operator_actions.go @@ -364,10 +364,9 @@ func operatorActionOutcome(status int, body []byte) (outcome, detail, head strin } if d, ok := resp["decision"].(map[string]any); ok && status == http.StatusOK { if v, _ := d["verdict"].(string); v != "" { - detail = "hive decided: " + v - if rule, _ := d["rule"].(string); rule != "" { - detail += " (" + rule + ")" - } + rule, _ := d["rule"].(string) + path, _ := d["path"].(string) + detail = operatorActionDecidedDetail(v, rule, path) } if st, _ := resp["status"].(string); st == delegatedJobStatusFailed { return operatorActionOutcomeFailed, str("error"), head, true @@ -465,6 +464,21 @@ func (s *Server) recordOperatorAction(h http.HandlerFunc) http.HandlerFunc { } } +// operatorActionDecidedDetail is the ledger line for a delegated decision: +// "hive decided: approve · review passed, CI green, mergeable (rule)". The +// path, when the policy recorded one, says in plain words why that +// verdict (hivecommons/hive#15173); the rule slug follows in parentheses. +func operatorActionDecidedDetail(verdict, rule, path string) string { + detail := "hive decided: " + verdict + if p := strings.TrimSpace(path); p != "" { + detail += " · " + p + } + if r := strings.TrimSpace(rule); r != "" { + detail += " (" + r + ")" + } + return detail +} + // settleOperatorAction closes the pending let-hive-decide record once the // delegated job ends. func (s *Server) settleOperatorAction(repoFull string, number int, job *delegatedDecisionJob) { @@ -473,10 +487,7 @@ func (s *Server) settleOperatorAction(repoFull string, number int, job *delegate } outcome, detail := operatorActionOutcomeApplied, "" if job.Decision != nil { - detail = "hive decided: " + string(job.Decision.Verdict) - if job.Decision.Rule != "" { - detail += " (" + job.Decision.Rule + ")" - } + detail = operatorActionDecidedDetail(string(job.Decision.Verdict), job.Decision.Rule, job.Decision.Path) if job.Decision.KeepsPark { detail += " — kept for the owner" } diff --git a/src/pkg/dashboard/stage_tracker_acted_path_test.go b/src/pkg/dashboard/stage_tracker_acted_path_test.go new file mode 100644 index 0000000000..8f53e8c75c --- /dev/null +++ b/src/pkg/dashboard/stage_tracker_acted_path_test.go @@ -0,0 +1,38 @@ +package dashboard + +import ( + "os/exec" + "strings" + "testing" +) + +// The ✓ hover for a delegated 090 decision reads the path in plain words +// (hivecommons/hive#15173): "the hive decided: approve · review passed, CI +// green, mergeable (rule: human merge approve)" — no slugs, no adoption +// words on the approve path. +func TestStageTrackerActedPlainDecisionPathJS(t *testing.T) { + node, err := exec.LookPath("node") + if err != nil { + t.Skip("node not on PATH — acted-plain decision path was NOT executed by this run") + } + html := indexHTML(t) + script := strings.Join([]string{ + "const window = {};", + "const assert = require('assert');", + jsConstBlock(t, html, "STAGE_TRACKER_ACTED_PLAIN"), + jsFunc(t, html, "stageTrackerActedPlain"), + }, "\n") + ` +const approve = stageTrackerActedPlain(` + "`" + operatorActionDecidedDetail("approve", "human-merge-approve", "review passed, CI green, mergeable") + "`" + `); +assert.strictEqual(approve, 'the hive decided: approve · review passed, CI green, mergeable (rule: human merge approve)'); +assert.ok(!/adopt/i.test(approve), approve); +const adopt = stageTrackerActedPlain(` + "`" + operatorActionDecidedDetail("adopt-supersede", "human-merge-adopt", "CI red, conflicting, needs rebase") + "`" + `); +assert.strictEqual(adopt, 'the hive decided: adopt-supersede · CI red, conflicting, needs rebase (rule: human merge adopt)'); +assert.strictEqual(stageTrackerActedPlain(` + "`" + operatorActionDecidedDetail("approve", "default-release", "") + "`" + `), 'the hive decided: approve (rule: default release)'); +console.log('ok'); +` + cmd := exec.Command(node, "-") + cmd.Stdin = strings.NewReader(script) + if out, err := cmd.CombinedOutput(); err != nil || !strings.Contains(string(out), "ok") { + t.Fatalf("node: %v\n%s", err, out) + } +} diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index b8f9465e05..0d2d516d80 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -16964,6 +16964,8 @@

All labels

"hold": {"kind": "reason", "plain": "on hold; the hive does nothing until it is released", "detail": "The item is on hold. The hive does nothing with it until a person releases the hold."}, "human-changes-requested": {"kind": "reason", "plain": "a reviewer asked for changes; a new commit is needed", "detail": "A reviewer asked for changes on this branch. Approving cannot override that: only a new commit, or a maintainer merging by hand, moves it."}, "human-merge": {"kind": "reason", "plain": "no merge lane vouches for this PR's author; a maintainer merges", "detail": "The PR is ready, but its author is not a collaborator and holds no trusted or merger tier, so no merge lane vouches for them and the hive never merges it unattended. A maintainer merges it (Approve & merge as maintainer, or Approve — let hive merge), or trusts the author under 👥 Contributors."}, + "human-merge-adopt": {"kind": "rule", "plain": "the head can't merge as it is, so the hive takes the work over", "detail": "The review is not good, CI is red, the PR conflicts with its base or a sign-off is missing, so approving would only queue a merge GitHub refuses. The hive re-homes the commits on its own branch, fixes what blocks the merge and opens a superseding PR."}, + "human-merge-approve": {"kind": "rule", "plain": "review passed, CI green, mergeable — the hive merges it under its own number", "detail": "The PR was ready for a human merge only because nobody vouches for its author. Review, CI and mergeability are good, so the person's delegation is the approval: the head is admitted and the merge sweep merges the PR as it is after soak, not copied into a hive-owned PR."}, "label:needs-human": {"kind": "reason", "plain": "marked needs-human; a person must remove the label", "detail": "The item is marked needs-human: automated fixes ran out or a person flagged it. The hive waits until the label is removed."}, "labels-add": {"kind": "action", "plain": "added labels", "detail": "The hive added labels to the item."}, "labels-remove": {"kind": "action", "plain": "removed labels", "detail": "The hive removed labels from the item."}, diff --git a/src/pkg/delegatedecision/decision_glossary.go b/src/pkg/delegatedecision/decision_glossary.go index 809c9bbba5..0d28437a54 100644 --- a/src/pkg/delegatedecision/decision_glossary.go +++ b/src/pkg/delegatedecision/decision_glossary.go @@ -75,6 +75,10 @@ var glossary = []GlossaryEntry{ // Rules — why the delegate chose what it chose. {Slug: "contributor-offline", Kind: GlossaryRule, Plain: "the author's hive hasn't been connected since the nudge", Detail: "The PR's author has been offline since the hive nudged them, so the hive takes the work over instead of waiting."}, + {Slug: RuleHumanMergeApprove, Kind: GlossaryRule, Plain: "review passed, CI green, mergeable — the hive merges it under its own number", + Detail: "The PR was ready for a human merge only because nobody vouches for its author. Review, CI and mergeability are good, so the person's delegation is the approval: the head is admitted and the merge sweep merges the PR as it is after soak, not copied into a hive-owned PR."}, + {Slug: RuleHumanMergeAdopt, Kind: GlossaryRule, Plain: "the head can't merge as it is, so the hive takes the work over", + Detail: "The review is not good, CI is red, the PR conflicts with its base or a sign-off is missing, so approving would only queue a merge GitHub refuses. The hive re-homes the commits on its own branch, fixes what blocks the merge and opens a superseding PR."}, {Slug: "dedupe", Kind: GlossaryRule, Plain: "an older issue already tracks this", Detail: "An older issue already tracks the same work; one thread keeps it moving."}, {Slug: "fix-then-merge", Kind: GlossaryRule, Plain: "the branch isn't mergeable yet", diff --git a/src/pkg/delegatedecision/decision_glossary_test.go b/src/pkg/delegatedecision/decision_glossary_test.go index 6701abd9c3..d9ad0459bc 100644 --- a/src/pkg/delegatedecision/decision_glossary_test.go +++ b/src/pkg/delegatedecision/decision_glossary_test.go @@ -20,9 +20,10 @@ func TestGlossaryCoversEveryRuleSlug(t *testing.T) { regexp.MustCompile(`\b(?:recommendOnly|approveIssue|releasePR)\("([a-z0-9-]+)"`), regexp.MustCompile(`\bruleDefault[A-Za-z]+\s*=\s*"([a-z0-9-]+)"`), regexp.MustCompile(`\bRuleLLM\s*=\s*"([a-z0-9-]+)"`), + regexp.MustCompile(`\bRuleHumanMerge[A-Za-z]+\s*=\s*"([a-z0-9-]+)"`), } found := map[string]bool{} - for _, file := range []string{"policy.go", "llm.go"} { + for _, file := range []string{"policy.go", "llm.go", "human_merge.go"} { src, err := os.ReadFile(file) if err != nil { t.Fatal(err) diff --git a/src/pkg/delegatedecision/human_merge.go b/src/pkg/delegatedecision/human_merge.go new file mode 100644 index 0000000000..d356696a11 --- /dev/null +++ b/src/pkg/delegatedecision/human_merge.go @@ -0,0 +1,98 @@ +package delegatedecision + +import ( + "strconv" + "strings" +) + +// "Let hive decide" on a PR at stage/090 Ready for human merge +// (hivecommons/hive#15173). +// +// The lanes parked the PR for one reason only: nobody vouches for its +// author. When the reviewer's verdict is good, CI is green and the head is +// mergeable, the person delegating has nothing left to judge — the hive's +// decision is approve, and the merge sweep merges the PR under its own +// number after soak (the same path as "Approve — let hive merge"; the +// approval is the trust override for that head, hivecommons/hive#15136). +// Re-authoring the work into a hive-owned PR is the fallback only when the +// head cannot merge as it is: the review is not good, CI is red, or the PR +// conflicts with its base and needs a rebase the hive cannot do in place. + +// Rules DecideHumanMerge returns. +const ( + RuleHumanMergeApprove = "human-merge-approve" + RuleHumanMergeAdopt = "human-merge-adopt" +) + +// Path words the ✓ hover shows after "hive decided: approve · ". +const ( + pathReviewPassed = "review passed" + pathReviewLane = "review: lane found nothing wrong" + pathReviewNotGood = "review not good" + pathCIGreen = "CI green" + pathCIRed = "CI red" + pathMergeable = "mergeable" + pathConflicting = "conflicting, needs rebase" + pathDCOMissing = "sign-off missing" +) + +// HumanMergeApprovable reports whether a 090 PR head is one the hive may +// approve for the sweep: review good (or none recorded — at 090 the lane's +// own evaluation already found nothing wrong), CI green, no conflict, no +// missing sign-off. +func HumanMergeApprovable(in Input) bool { + reviewGood := in.ReviewGood || !in.ReviewKnown + return reviewGood && in.CIGreen && !in.Conflicting && !in.DCOMissing +} + +// DecideHumanMerge is the delegate's answer for a PR at 090. ok=false when +// the input is not a 090 PR. +func DecideHumanMerge(in Input) (Decision, bool) { + if in.Kind != KindPR || !in.ReadyForHumanMerge { + return Decision{}, false + } + ref := "#" + strconv.Itoa(in.Number) + if HumanMergeApprovable(in) { + review := pathReviewPassed + if !in.ReviewKnown { + review = pathReviewLane + } + path := strings.Join([]string{review, pathCIGreen, pathMergeable}, ", ") + text := "approve " + ref + ": " + path + " — the merge sweep merges it under its own number after soak" + return Decision{ + Verdict: VerdictApprove, Rule: RuleHumanMergeApprove, Text: text, Path: path, + Rationale: "the lanes parked " + ref + " only because nobody vouches for its author; the review, CI and mergeability are good, so the person's delegation is the approval and the hive merges the PR as it is — it is not copied into a hive-owned PR", + Actions: []Action{decisionComment(text), {Kind: ActionApproveReview}}, + }, true + } + var why []string + if in.ReviewKnown && !in.ReviewGood { + why = append(why, pathReviewNotGood) + } + if !in.CIGreen { + why = append(why, pathCIRed) + } + if in.Conflicting { + why = append(why, pathConflicting) + } + if in.DCOMissing { + why = append(why, pathDCOMissing) + } + path := strings.Join(why, ", ") + text := "adopt " + ref + " in supersede mode: " + path + " — the hive re-homes the commits on its own branch, fixes what blocks the merge and opens a superseding PR, preserving the author's commits and sign-off" + return Decision{ + Verdict: VerdictAdoptSupersede, Rule: RuleHumanMergeAdopt, Text: text, Path: path, + Rationale: "the head cannot merge as it is (" + path + "); approving would only queue a merge GitHub refuses, so the hive takes the work over", + Actions: []Action{decisionComment(text), {Kind: ActionAdoptSupersede}}, + }, true +} + +// RunHumanMerge is DecideHumanMerge with the safety rails applied (a held +// PR is refused, like every other delegated decision). +func RunHumanMerge(in Input) (Decision, bool) { + d, ok := DecideHumanMerge(in) + if !ok { + return Decision{}, false + } + return ApplyRails(d, Rails{Held: hasLabel(in.Labels, LabelHold)}), true +} diff --git a/src/pkg/delegatedecision/human_merge_test.go b/src/pkg/delegatedecision/human_merge_test.go new file mode 100644 index 0000000000..53e643f9c1 --- /dev/null +++ b/src/pkg/delegatedecision/human_merge_test.go @@ -0,0 +1,95 @@ +package delegatedecision + +import ( + "strings" + "testing" +) + +// "Let hive decide" on a PR at 090 Ready for human merge +// (hivecommons/hive#15173): approve and let the sweep merge it under its +// own number when review, CI and mergeability are good; adopt only when +// the head cannot merge as it is. +func TestDecideHumanMergePaths(t *testing.T) { + base := Input{Repo: "hivecommons/hotshot", Number: 317, Kind: KindPR, Author: "Karibhou", + ReadyForHumanMerge: true, ReviewKnown: true, ReviewGood: true, CIGreen: true, HeadSHA: "abc123"} + cases := []struct { + name string + mutate func(*Input) + verdict Verdict + rule string + action ActionKind + path string + }{ + {"good review, green, mergeable → approve", func(*Input) {}, VerdictApprove, RuleHumanMergeApprove, ActionApproveReview, "review passed, CI green, mergeable"}, + {"no review recorded at 090 → approve, says the lane found nothing wrong", func(in *Input) { in.ReviewKnown, in.ReviewGood = false, false }, VerdictApprove, RuleHumanMergeApprove, ActionApproveReview, "review: lane found nothing wrong, CI green, mergeable"}, + {"red CI → adopt", func(in *Input) { in.CIGreen = false }, VerdictAdoptSupersede, RuleHumanMergeAdopt, ActionAdoptSupersede, "CI red"}, + {"conflicting → adopt with rebase", func(in *Input) { in.Conflicting = true; in.CIGreen = false }, VerdictAdoptSupersede, RuleHumanMergeAdopt, ActionAdoptSupersede, "CI red, conflicting, needs rebase"}, + {"review not good → adopt", func(in *Input) { in.ReviewGood = false }, VerdictAdoptSupersede, RuleHumanMergeAdopt, ActionAdoptSupersede, "review not good"}, + {"sign-off missing → adopt", func(in *Input) { in.DCOMissing = true }, VerdictAdoptSupersede, RuleHumanMergeAdopt, ActionAdoptSupersede, "sign-off missing"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + in := base + tc.mutate(&in) + d, ok := DecideHumanMerge(in) + if !ok { + t.Fatal("expected a 090 decision") + } + if d.Verdict != tc.verdict || d.Rule != tc.rule || d.Path != tc.path { + t.Fatalf("got verdict=%s rule=%s path=%q, want %s/%s/%q", d.Verdict, d.Rule, d.Path, tc.verdict, tc.rule, tc.path) + } + if d.KeepsPark { + t.Fatal("a 090 decision must not keep the park") + } + if len(d.Actions) != 2 || d.Actions[0].Kind != ActionCommentDecision || d.Actions[1].Kind != tc.action { + t.Fatalf("actions = %+v, want comment + %s", d.Actions, tc.action) + } + if !strings.Contains(d.Text, "#317") || !strings.Contains(d.Text, tc.path) { + t.Fatalf("text %q must name the PR and the path", d.Text) + } + if strings.ContainsAny(d.Path, "()_") { + t.Fatalf("path %q must be plain words", d.Path) + } + if tc.verdict == VerdictApprove && strings.Contains(strings.ToLower(d.Text), "adopt") { + t.Fatalf("approve text must not mention adoption: %q", d.Text) + } + if tc.verdict == VerdictAdoptSupersede && !strings.Contains(d.Text, "author's commits") { + t.Fatalf("adopt text must say the author's commits are preserved: %q", d.Text) + } + // The rails must keep both the comment and the effect. + railed, _ := RunHumanMerge(in) + if len(railed.Actions) != 2 || len(railed.Refusals) != 0 { + t.Fatalf("rails altered the decision: %+v", railed) + } + in.Labels = append(in.Labels, LabelHold) + if held, _ := RunHumanMerge(in); held.Verdict != VerdictRefused { + t.Fatalf("a held PR must be refused, got %s", held.Verdict) + } + }) + } +} + +func TestDecideHumanMergeOnlyForReadyPRs(t *testing.T) { + if _, ok := DecideHumanMerge(Input{Kind: KindPR, Number: 1, CIGreen: true}); ok { + t.Fatal("a PR not at 090 is not a human-merge decision") + } + if _, ok := DecideHumanMerge(Input{Kind: KindIssue, Number: 1, ReadyForHumanMerge: true}); ok { + t.Fatal("an issue is never a human-merge decision") + } +} + +func TestHumanMergeApprovable(t *testing.T) { + if !HumanMergeApprovable(Input{CIGreen: true}) { + t.Fatal("green, no review, no conflict, signed → approvable") + } + for name, in := range map[string]Input{ + "red": {CIGreen: false}, + "conflict": {CIGreen: true, Conflicting: true}, + "bad review": {CIGreen: true, ReviewKnown: true}, + "dco": {CIGreen: true, DCOMissing: true}, + } { + if HumanMergeApprovable(in) { + t.Fatalf("%s must not be approvable", name) + } + } +} diff --git a/src/pkg/delegatedecision/policy.go b/src/pkg/delegatedecision/policy.go index 876901e896..8b0665dc89 100644 --- a/src/pkg/delegatedecision/policy.go +++ b/src/pkg/delegatedecision/policy.go @@ -167,6 +167,15 @@ type Input struct { CIGreen bool Conflicting bool DCOMissing bool + // HeadSHA is the PR head the decision is about (for per-head records). + HeadSHA string + // ReviewKnown: the reviewer recorded a verdict for HeadSHA; ReviewGood: + // that verdict approves (no blocking findings, no requires_human). + ReviewKnown bool + ReviewGood bool + // ReadyForHumanMerge: the PR sits at stage/090 — the lanes found nothing + // wrong with it and only its author's trust keeps the hive from merging. + ReadyForHumanMerge bool // Small: a size/XS or size/S PR (also read from the labels). Small bool // LowRiskPaths: every changed file is CI config, workflow, docs, text or @@ -201,6 +210,9 @@ type Decision struct { // KeepsPark is true when the item stays at 085/045 (recommend-only, // owner-only, refused). KeepsPark bool `json:"keeps_park"` + // Path is the chosen path in a few plain words ("review passed, CI + // green, mergeable"), for the ✓ hover and the audit line. + Path string `json:"path,omitempty"` } // Marker is the audit stamp every delegated comment carries.