Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-decision-path-owner-intent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Preserve maintainer decision intent through PR decision paths: `/hive decision approve <rationale>` stays an approval (`yes, but …` stays an instruction), `/hive decision` is read at 070 as well as 085, a later same-head trusted APPROVED review releases a standing metadata-only request (and is dropped again if dismissed), edited non-closing issue references stay non-closing, and trusted contributor change requests return to the author (#14816).
19 changes: 16 additions & 3 deletions src/docs/maintainer-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,26 @@ The whole comment after `/hive decision` is the instruction β€” numbered points
on the lines below the command are kept and quoted verbatim β€” and the
decision on the current head outranks the swarm's decision-requested verdict
on that head, so the stage moves in the same pass as the acknowledgement
rather than waiting on a re-review. A bare `/hive decision approve` (nothing
beyond the approval word) is recorded as your answer to the swarm's question
instead: no fix is kicked, requires_human is released for that head and the
rather than waiting on a re-review. A `/hive decision` whose first word is
`approve`, `approved`, or `lgtm` (a trailing comma or period is fine) is
recorded as approval; the rest of the comment is retained as its rationale. A
bare `yes` approves too, but `yes, but …` is a conditional answer and is routed
as an instruction. To request changes, start with the instruction
(e.g. `/hive decision change the PR body to Refs #123`). No fix is kicked
for an approval, requires_human is released for that head and the
PR follows the normal merge path. An acknowledged decision whose record did
not survive a pod restart is rebuilt from the comment thread; a decision
answered for an older head is left alone
([#14679](https://github.com/hivecommons/hive/issues/14679)).
The sweep also reads `/hive decision` at `stage/070-fixing`: approve the
current head after a body or metadata fix to release a standing request without
pushing a cosmetic commit. A later trusted GitHub APPROVED review on the same
head also supersedes the recorded request; an older approval does not. If
that review is later dismissed, or the reviewer asks for changes again on the
same head, the review-backed approval is dropped and the earlier instruction
stands again β€” a `/hive decision approve` comment is durable and is not.
Issue-link repair preserves edited `Refs` bodies and PRs with a recorded
decision, so it cannot restore auto-close over the owner's direction.
The 085 entry comment names both actions. The commenter must hold `write` or better on the
repo *and* be in the hive's trusted-merger set (the bar a human approval
already meets). `/hive decision` on a PR the hive did **not** open, or `/hive
Expand Down
6 changes: 6 additions & 0 deletions src/pkg/github/automerge/automerge_contributor_lane.go
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,12 @@ func (c *Engine) evaluateContributorLanePR(ctx context.Context, displayRepo, own
eval.verdict = verdict
approver, humanApproved := c.trustedHumanApprovalForRequiresHuman(ctx, owner, repo, number, headSHA, verdict)
c.noteStageVerdict(owner, repo, number, verdict)
// A trusted head-scoped change request answers the swarm's question
// even when admission or a requires_human verdict would return early.
if rv, ok := c.latestTrustedHumanChangesRequested(ctx, owner, repo, number, headSHA); ok {
c.noteStageHumanChangeRequest(owner, repo, number, headSHA, hgithub.SafeGetLogin(rv.GetUser()))
return eval, contributorLaneReasonChangesRequested, nil
}
if claim.Reason != "" {
// Neither this hive's registry nor an App-authored marker vouches
// for the author, so GitHub's association is the only evidence left;
Expand Down
30 changes: 30 additions & 0 deletions src/pkg/github/automerge/automerge_contributor_lane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
"time"

gh "github.com/google/go-github/v72/github"
"github.com/hivecommons/hive/pkg/authorkind"
hgithub "github.com/hivecommons/hive/pkg/github"
"github.com/hivecommons/hive/pkg/intent"
"github.com/hivecommons/hive/pkg/stagelabels"
)

const (
Expand Down Expand Up @@ -1681,3 +1683,31 @@ func TestContributorLaneAssociationTrusted(t *testing.T) {
t.Fatal("nil PR must not be trusted")
}
}

func TestContributorTrustedChangeRequestAnswersRequiresHuman(t *testing.T) {
fx := validContributorFixture()
fx.reviews = []map[string]any{humanReview(1, "clubanderson", "CHANGES_REQUESTED", "sha7", "please fix the metadata")}
api := newContributorLaneAPI(t, &fx)
defer api.Close()
policy := defaultContributorLanePolicy()
clock := &contributorLaneClock{at: time.Date(2026, 10, 28, 3, 0, 0, 0, time.UTC)}
engine := newContributorLaneEngine(api.URL, policy, clock, Options{
ContributorLaneVerdict: func(string, int, string) ContributorLaneVerdict {
return ContributorLaneVerdict{Configured: true, Verdict: "requires_human", DecisionRequested: true}
},
})
engine.SetMergerAuthorizer(func(login string) bool { return login == "clubanderson" })
engine.stageTracker = NewStageTracker()
engine.stageLabelPolicy = func() StageLabelPolicy { return StageLabelPolicy{Enabled: true} }
_, reason, err := engine.trySweepContributorLanePR(context.Background(), "widget", "acme", "widget", 7, policy)
if err != nil || reason != contributorLaneReasonChangesRequested || fx.merges != 0 {
t.Fatalf("reason=%s err=%v merges=%d", reason, err, fx.merges)
}
detail := engine.stageTracker.details[stageKey("acme", "widget", 7)]
// Production observations include the author kind; the lane alone does
// not distinguish an automated contributor (072) from a human (071).
o := stageObservation{AuthorKind: authorkind.Clanker, Lane: stageLaneContributor, HeadSHA: "sha7", Reason: reason, Detail: detail}
if v := classifyStage(o); v.Stage != stagelabels.ReturnedToContributor {
t.Fatalf("stage=%+v, want returned to contributor", v)
}
}
69 changes: 59 additions & 10 deletions src/pkg/github/automerge/automerge_human_changes_requested.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package automerge

import (
Expand All @@ -20,6 +20,12 @@
// already pushed past it, and the new head goes through the normal swarm
// re-review rather than another kick.
func (c *Engine) latestTrustedHumanChangesRequested(ctx context.Context, owner, repo string, number int, head string) (*gh.PullRequestReview, bool) {
return c.latestTrustedHumanReview(ctx, owner, repo, number, head, "CHANGES_REQUESTED")
}

// latestTrustedHumanReview reads all pages before selecting an opinion. An
// older approval must never shadow a later change request or dismissal.
func (c *Engine) latestTrustedHumanReview(ctx context.Context, owner, repo string, number int, head, state string) (*gh.PullRequestReview, bool) {
if c == nil || c.gh == nil || strings.TrimSpace(head) == "" {
return nil, false
}
Expand Down Expand Up @@ -54,7 +60,7 @@
c.noteStageReviews(owner, repo, number, seen)
var found *gh.PullRequestReview
for login, rv := range latest {
if !strings.EqualFold(rv.GetState(), "CHANGES_REQUESTED") || strings.TrimSpace(rv.GetCommitID()) != strings.TrimSpace(head) {
if !strings.EqualFold(rv.GetState(), state) || strings.TrimSpace(rv.GetCommitID()) != strings.TrimSpace(head) {
continue
}
if c.isHiveAppReviewAuthor(rv) || strings.HasSuffix(login, "[bot]") {
Expand Down Expand Up @@ -88,10 +94,26 @@
if c.humanChangeRequests == nil || strings.TrimSpace(head) == "" {
return false
}
standing, found := c.humanChangeRequests.For(owner+"/"+repo, number, strings.TrimSpace(head))
standing, found := c.reconcileReviewBackedApproval(ctx, owner, repo, number, strings.TrimSpace(head))
if !found || standing.IsApproval() {
return false
}
// A later trusted approval on this same head can acknowledge a body-
// only fix. Persist it so the planner also stops scheduling a fix.
if approval, approved := c.latestTrustedHumanReview(ctx, owner, repo, number, head, "APPROVED"); approved && approval.GetSubmittedAt().After(standing.RecordedAt) {
req := review.HumanChangeRequest{
Repo: owner + "/" + repo, Number: number, HeadSHA: head,
Source: review.HumanChangeRequestSourceReview, SourceID: approval.GetID(),
Actor: hgithub.SafeGetLogin(approval.GetUser()), Body: approval.GetBody(), URL: approval.GetHTMLURL(),
Kind: review.HumanChangeRequestKindApprove, RecordedAt: approval.GetSubmittedAt().Time,
}
if err := c.humanChangeRequests.Record(req); err == nil {
c.noteStageTrustedHumanApproval(owner, repo, number, head, req.Actor)
return false
} else {
c.warn("could not record approval clearing human change request", "pr", number, "error", err)
}
}
c.noteStageHumanChangeRequest(owner, repo, number, head, standing.Actor)
return true
}
Expand All @@ -100,14 +122,15 @@
return true
}
req := review.HumanChangeRequest{
Repo: owner + "/" + repo,
Number: number,
HeadSHA: strings.TrimSpace(head),
Source: review.HumanChangeRequestSourceReview,
SourceID: rv.GetID(),
Actor: strings.TrimSpace(hgithub.SafeGetLogin(rv.GetUser())),
Body: rv.GetBody(),
URL: rv.GetHTMLURL(),
Repo: owner + "/" + repo,
Number: number,
HeadSHA: strings.TrimSpace(head),
Source: review.HumanChangeRequestSourceReview,
SourceID: rv.GetID(),
Actor: strings.TrimSpace(hgithub.SafeGetLogin(rv.GetUser())),
Body: rv.GetBody(),
URL: rv.GetHTMLURL(),
RecordedAt: rv.GetSubmittedAt().Time,
}
if existing, found := c.humanChangeRequests.For(req.Repo, number, req.HeadSHA); found && existing.Source == req.Source && existing.SourceID == req.SourceID {
return true
Expand All @@ -117,3 +140,29 @@
}
return true
}

// reconcileReviewBackedApproval returns the decision standing against head
// after dropping an approval-kind record that was persisted from a GitHub
// review which no longer stands: the reviewer dismissed it, or their latest
// opinion on head is not APPROVED any more. The revoked approval would
// otherwise keep answering requires_human and shadow the instruction
// recorded before it. Comment and dashboard decisions are durable and are
// never revalidated here.
func (c *Engine) reconcileReviewBackedApproval(ctx context.Context, owner, repo string, number int, head string) (review.HumanChangeRequest, bool) {
if c == nil || c.humanChangeRequests == nil || strings.TrimSpace(head) == "" {
return review.HumanChangeRequest{}, false
}
key := owner + "/" + repo
standing, found := c.humanChangeRequests.For(key, number, head)
if !found || !standing.IsApproval() || standing.Source != review.HumanChangeRequestSourceReview {
return standing, found
}
if live, ok := c.latestTrustedHumanReview(ctx, owner, repo, number, head, "APPROVED"); ok && live.GetID() == standing.SourceID {
return standing, true
}
if err := c.humanChangeRequests.Forget(key, number, standing.Source, standing.SourceID); err != nil {
c.warn("could not drop revoked review approval", "pr", number, "review", standing.SourceID, "error", err)
return review.HumanChangeRequest{}, false
}
return c.humanChangeRequests.For(key, number, head)
}
124 changes: 124 additions & 0 deletions src/pkg/github/automerge/automerge_human_changes_requested_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"strings"
"testing"
"time"

"github.com/hivecommons/hive/pkg/review"
"github.com/hivecommons/hive/pkg/stagelabels"
Expand Down Expand Up @@ -228,3 +229,126 @@ func TestTrySweepSelfAuthoredPRRecordedDecisionNotesStageAndApprovalReleases(t *
})
}
}

func TestHumanChangeRequestClearedByLaterApprovalWithoutNewHead(t *testing.T) {
for _, tc := range []struct {
name, login, head, state, submitted string
wantHeld bool
}{
{"later trusted approval", "bob", "sha7", "APPROVED", "2026-10-28T02:00:00Z", false},
{"old approval", "bob", "sha7", "APPROVED", "2026-10-28T00:00:00Z", true},
{"approval on another head", "bob", "sha6", "APPROVED", "2026-10-28T02:00:00Z", true},
{"untrusted approval", "mallory", "sha7", "APPROVED", "2026-10-28T02:00:00Z", true},
{"still requesting changes", "bob", "sha7", "CHANGES_REQUESTED", "2026-10-28T02:00:00Z", true},
} {
t.Run(tc.name, func(t *testing.T) {
rv := humanReview(2, tc.login, tc.state, tc.head, "metadata fix checked")
rv["submitted_at"] = tc.submitted
api := newSelfSweepGuardAPI(t, selfSweepFixture{reviews: []map[string]any{rv}})
defer api.Close()
c := newAutoMergeSweepClient(api.URL)
store := review.NewHumanChangeRequests("")
c.humanChangeRequests = store
at, err := time.Parse(time.RFC3339, "2026-10-28T01:00:00Z")
if err != nil {
t.Fatal(err)
}
if err := store.Record(review.HumanChangeRequest{Repo: "acme/widget", Number: 7, HeadSHA: "sha7", Source: review.HumanChangeRequestSourceDecision, SourceID: 99, Actor: "bob", Body: "edit body", RecordedAt: at}); err != nil {
t.Fatal(err)
}
for i := 0; i < 2; i++ {
if held := c.humanChangesRequestedForHead(context.Background(), "acme", "widget", 7, "sha7"); held != tc.wantHeld {
t.Fatalf("held=%v, want %v", held, tc.wantHeld)
}
if scheduled := review.HumanChangeRequestFor(store, "acme/widget", 7, "sha7") != nil; scheduled != tc.wantHeld {
t.Fatalf("planner still schedules=%v, want %v", scheduled, tc.wantHeld)
}
}
})
}
}

func TestTrustedHumanApprovalUsesLatestOpinion(t *testing.T) {
api := newSelfSweepGuardAPI(t, selfSweepFixture{reviews: []map[string]any{
humanReview(1, "bob", "APPROVED", "sha7", ""),
humanReview(2, "bob", "CHANGES_REQUESTED", "sha7", "please fix"),
}})
defer api.Close()
c := newAutoMergeSweepClient(api.URL)
if login, ok := c.latestTrustedHumanApproval(context.Background(), "acme", "widget", 7, "sha7"); ok {
t.Fatalf("obsolete approval from %s accepted", login)
}
}

// A same-head APPROVED review persisted as an approval-kind decision must
// not outlive the review: once it is dismissed (or the reviewer's latest
// opinion on the head is no longer APPROVED), the record is dropped, the
// earlier instruction stands again and requires_human is unanswered. A
// comment-sourced approval is durable and is not revalidated.
func TestReviewBackedApprovalRevokedOnDismissal(t *testing.T) {
for _, tc := range []struct {
name, revoke, wantBody string
}{
{"dismissed", "DISMISSED", "edit body"},
// The fresh change request is now the newest instruction.
{"changes requested again", "CHANGES_REQUESTED", "metadata fix checked"},
} {
t.Run(tc.name, func(t *testing.T) {
approval := humanReview(2, "bob", "APPROVED", "sha7", "metadata fix checked")
approval["submitted_at"] = "2026-10-28T02:00:00Z"
api := newSelfSweepGuardAPI(t, selfSweepFixture{reviews: []map[string]any{approval}})
defer api.Close()
c := newAutoMergeSweepClient(api.URL)
store := review.NewHumanChangeRequests("")
c.humanChangeRequests = store
at, err := time.Parse(time.RFC3339, "2026-10-28T01:00:00Z")
if err != nil {
t.Fatal(err)
}
if err := store.Record(review.HumanChangeRequest{Repo: "acme/widget", Number: 7, HeadSHA: "sha7", Source: review.HumanChangeRequestSourceDecision, SourceID: 99, Actor: "bob", Body: "edit body", RecordedAt: at}); err != nil {
t.Fatal(err)
}
if c.humanChangesRequestedForHead(context.Background(), "acme", "widget", 7, "sha7") {
t.Fatal("approval on the same head did not release the request")
}
if login, ok := c.trustedHumanApprovalForRequiresHuman(context.Background(), "acme", "widget", 7, "sha7", ContributorLaneVerdict{Configured: true, Verdict: "requires_human"}); !ok || login != "bob" {
t.Fatalf("approval not seen: %q %v", login, ok)
}

// The reviewer takes the approval back without a new head.
approval["state"] = tc.revoke
approval["submitted_at"] = "2026-10-28T03:00:00Z"
if login, ok := c.trustedHumanApprovalForRequiresHuman(context.Background(), "acme", "widget", 7, "sha7", ContributorLaneVerdict{Configured: true, Verdict: "requires_human"}); ok {
t.Fatalf("revoked review approval still answers requires_human as %q", login)
}
if !c.humanChangesRequestedForHead(context.Background(), "acme", "widget", 7, "sha7") {
t.Fatal("instruction recorded before the revoked approval no longer holds the lane")
}
req := review.HumanChangeRequestFor(store, "acme/widget", 7, "sha7")
if req == nil || req.Body != tc.wantBody {
t.Fatalf("planner instruction = %+v, want %q", req, tc.wantBody)
}
if got, ok := store.Lookup("acme/widget", 7, review.HumanChangeRequestSourceReview, 2); ok && got.IsApproval() {
t.Fatal("revoked review approval still persisted")
}
})
}
}

func TestCommentApprovalSurvivesReviewDismissal(t *testing.T) {
dismissed := humanReview(2, "bob", "DISMISSED", "sha7", "")
api := newSelfSweepGuardAPI(t, selfSweepFixture{reviews: []map[string]any{dismissed}})
defer api.Close()
c := newAutoMergeSweepClient(api.URL)
store := review.NewHumanChangeRequests("")
c.humanChangeRequests = store
if err := store.Record(review.HumanChangeRequest{Repo: "acme/widget", Number: 7, HeadSHA: "sha7", Source: review.HumanChangeRequestSourceDecision, SourceID: 4242, Actor: "bob", Body: "approve", Kind: review.HumanChangeRequestKindApprove}); err != nil {
t.Fatal(err)
}
if login, ok := c.trustedHumanApprovalForRequiresHuman(context.Background(), "acme", "widget", 7, "sha7", ContributorLaneVerdict{Configured: true, Verdict: "requires_human"}); !ok || login != "bob" {
t.Fatalf("durable /hive decision approve dropped: %q %v", login, ok)
}
if c.humanChangesRequestedForHead(context.Background(), "acme", "widget", 7, "sha7") {
t.Fatal("durable approval read as a change request")
}
}
43 changes: 8 additions & 35 deletions src/pkg/github/automerge/automerge_review_gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,43 +312,13 @@ func reviewGateRecordLane(lane string) string {
}

func (c *Engine) latestTrustedHumanApproval(ctx context.Context, owner, repo string, number int, head string) (string, bool) {
if c == nil || c.gh == nil {
rv, ok := c.latestTrustedHumanReview(ctx, owner, repo, number, head, "APPROVED")
if !ok {
return "", false
}
if ctx == nil {
ctx = context.Background()
}
opts := &gh.ListOptions{PerPage: 100}
var seen []*gh.PullRequestReview
for {
reviews, resp, err := c.gh.PullRequests.ListReviews(ctx, owner, repo, number, opts)
if err != nil {
c.warn("automerge review gate could not read PR reviews for human approval", "owner", owner, "repo", repo, "pr", number, "error", err)
return "", false
}
seen = append(seen, reviews...)
for _, review := range reviews {
if !strings.EqualFold(review.GetState(), "APPROVED") || strings.TrimSpace(review.GetCommitID()) != head {
continue
}
login := strings.TrimSpace(hgithub.SafeGetLogin(review.GetUser()))
if login == "" || c.isHiveAppReviewAuthor(review) || strings.HasSuffix(strings.ToLower(login), "[bot]") {
continue
}
trusted, configured := c.isTrustedMerger(login)
if configured && trusted {
c.noteStageReviews(owner, repo, number, seen)
c.noteStageTrustedHumanApproval(owner, repo, number, head, login)
return login, true
}
}
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
c.noteStageReviews(owner, repo, number, seen)
return "", false
login := strings.TrimSpace(hgithub.SafeGetLogin(rv.GetUser()))
c.noteStageTrustedHumanApproval(owner, repo, number, head, login)
return login, true
}

func (c *Engine) trustedHumanApprovalForRequiresHuman(ctx context.Context, owner, repo string, number int, head string, verdict ContributorLaneVerdict) (string, bool) {
Expand All @@ -358,6 +328,9 @@ func (c *Engine) trustedHumanApprovalForRequiresHuman(ctx context.Context, owner
if login, ok := c.latestTrustedHumanApproval(ctx, owner, repo, number, head); ok {
return login, true
}
// No live trusted approval on head: a persisted review-backed approval
// is revoked, so drop it before reading the recorded decision.
c.reconcileReviewBackedApproval(ctx, owner, repo, number, head)
return c.recordedHumanApprovalForHead(owner, repo, number, head)
}

Expand Down
Loading
Loading