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-unpark-stage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- A trusted merger's `/hive decision` on a hive-authored PR parked at `stage/085-needs-decision` now moves the PR: the whole comment (not just its first line) is recorded as the instruction and quoted verbatim in the fix kick, a decision on the current head supersedes the swarm's decision-requested verdict so the PR derives `stage/070-fixing` in the same pass as the acknowledgement, a bare `/hive decision approve` is recorded as an approval that releases requires_human instead of kicking a fix that says "approve", and an acknowledged decision whose record did not survive a restart is rebuilt from the comment thread ([#14679](https://github.com/hivecommons/hive/issues/14679)).
11 changes: 11 additions & 0 deletions src/docs/maintainer-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ delivered ahead of the governor's own kick to the same agent in that eval
cycle, so it does not time out behind routine queue work; a kick that still
cannot be sent is logged as `review swarm kick not delivered` with the PR and
audited as `review_kick_undelivered`, and the planner re-plans it next cycle.
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
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 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
13 changes: 11 additions & 2 deletions src/pkg/github/automerge/automerge_human_changes_requested.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,24 @@ func (c *Engine) latestTrustedHumanChangesRequested(ctx context.Context, owner,
// request already standing in the store for this head — a `/hive decision`
// the un-park sweep recorded (pr_unpark_decision.go) — counts the same way,
// so the text path reaches 070 through the lane reason like a review does.
// A bare `/hive decision approve` recorded there is not a change request
// (hivecommons/hive#14679). Either way the requester is noted on the stage
// detail so the reviewer's decision-requested verdict on this head stops
// deriving 085.
func (c *Engine) humanChangesRequestedForHead(ctx context.Context, owner, repo string, number int, head string) bool {
rv, ok := c.latestTrustedHumanChangesRequested(ctx, owner, repo, number, head)
if !ok {
if c.humanChangeRequests == nil || strings.TrimSpace(head) == "" {
return false
}
_, standing := c.humanChangeRequests.For(owner+"/"+repo, number, strings.TrimSpace(head))
return standing
standing, found := c.humanChangeRequests.For(owner+"/"+repo, number, strings.TrimSpace(head))
if !found || standing.IsApproval() {
return false
}
c.noteStageHumanChangeRequest(owner, repo, number, head, standing.Actor)
return true
}
c.noteStageHumanChangeRequest(owner, repo, number, head, hgithub.SafeGetLogin(rv.GetUser()))
if c.humanChangeRequests == nil {
return true
}
Expand Down
53 changes: 53 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 @@ -175,3 +175,56 @@ func TestTrySweepSelfAuthoredPRHonoursRecordedDecision(t *testing.T) {
})
}
}

// hivecommons/hive#14679: a recorded `/hive decision` on the current head
// is noted on the stage detail as the standing human change requester, so
// the stage derives fixing over the swarm's decision-requested verdict on
// that head; a bare approval recorded there is not a change request and
// does not hold the lane.
func TestTrySweepSelfAuthoredPRRecordedDecisionNotesStageAndApprovalReleases(t *testing.T) {
for _, tt := range []struct {
name string
kind string
wantReason string
wantRequester string
}{
{name: "instruction holds the lane and notes the requester", kind: review.HumanChangeRequestKindChanges, wantReason: autoMergeReasonHumanChangesRequested, wantRequester: "bob"},
{name: "bare approval releases the lane", kind: review.HumanChangeRequestKindApprove},
} {
t.Run(tt.name, func(t *testing.T) {
api := newSelfSweepGuardAPI(t, selfSweepFixture{mergeApplied: true})
defer api.Close()
c := newAutoMergeSweepClient(api.URL)
c.stageTracker = NewStageTracker()
c.stageLabelPolicy = func() StageLabelPolicy { return StageLabelPolicy{Enabled: true} }
store := review.NewHumanChangeRequests("")
c.humanChangeRequests = store
if err := store.Record(review.HumanChangeRequest{Repo: "acme/widget", Number: 7, HeadSHA: "sha7", Source: review.HumanChangeRequestSourceDecision, SourceID: 99, Actor: "bob", Body: "approve", Kind: tt.kind}); err != nil {
t.Fatal(err)
}
event, reason, err := c.trySweepSelfAuthoredPR(context.Background(), "acme/widget", "acme", "widget", 7, true)
if err != nil {
t.Fatal(err)
}
if reason != tt.wantReason {
t.Fatalf("reason = %q, want %q", reason, tt.wantReason)
}
c.stageTracker.mu.Lock()
detail := c.stageTracker.details[stageKey("acme", "widget", 7)]
c.stageTracker.mu.Unlock()
if detail.HumanChangeRequester != tt.wantRequester || (tt.wantRequester != "" && detail.HumanChangeRequestHead != "sha7") {
t.Fatalf("stage detail = %+v, want requester %q on sha7", detail, tt.wantRequester)
}
if tt.wantReason == "" && event.MergeSHA != "merge7" {
t.Fatalf("event = %+v, want the merge to proceed over a bare approval", event)
}
if tt.wantReason != "" {
o := stageObservation{Lane: stageLaneSelf, Reason: reason, HeadSHA: "sha7", Detail: detail}
o.Detail.Verdict, o.Detail.VerdictKnown = ContributorLaneVerdict{Configured: true, Verdict: "requires_human", DecisionRequested: true}, true
if v := classifyStage(o); v.Stage != stagelabels.Fixing || v.Reason != autoMergeReasonHumanChangesRequested {
t.Fatalf("stage with the decision-requested verdict = %+v, want 070 on the recorded decision", v)
}
}
})
}
}
23 changes: 22 additions & 1 deletion src/pkg/github/automerge/automerge_review_gate.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package automerge

import (
Expand Down Expand Up @@ -355,5 +355,26 @@
if !verdictNeedsHuman(stageDetail{Verdict: verdict, VerdictKnown: true}) {
return "", false
}
return c.latestTrustedHumanApproval(ctx, owner, repo, number, head)
if login, ok := c.latestTrustedHumanApproval(ctx, owner, repo, number, head); ok {
return login, true
}
return c.recordedHumanApprovalForHead(owner, repo, number, head)
}

// recordedHumanApprovalForHead is a bare `/hive decision approve` the un-park
// sweep recorded against head (pr_unpark_decision.go): the owner answered the
// swarm's question with "yes" in a comment rather than a GitHub review. It
// releases requires_human the way an APPROVED review on the head does
// (hivecommons/hive#14679).
func (c *Engine) recordedHumanApprovalForHead(owner, repo string, number int, head string) (string, bool) {
if c == nil || c.humanChangeRequests == nil || strings.TrimSpace(head) == "" {
return "", false
}
req, ok := c.humanChangeRequests.For(owner+"/"+repo, number, strings.TrimSpace(head))
if !ok || !req.IsApproval() || strings.TrimSpace(req.Actor) == "" {
return "", false
}
login := strings.ToLower(strings.TrimSpace(req.Actor))
c.noteStageTrustedHumanApproval(owner, repo, number, head, login)
return login, true
}
17 changes: 17 additions & 0 deletions src/pkg/github/automerge/automerge_stage_labels.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package automerge

// Stage labels (hivecommons/hive#12429). Each automerge lane already computes
Expand Down Expand Up @@ -285,6 +285,9 @@
if detail.HumanApprover != "" && detail.HumanApprovalHead != "" && detail.HumanApprovalHead == o.HeadSHA {
o.Detail.HumanApprover, o.Detail.HumanApprovalHead = detail.HumanApprover, detail.HumanApprovalHead
}
if detail.HumanChangeRequester != "" && detail.HumanChangeRequestHead != "" && detail.HumanChangeRequestHead == o.HeadSHA {
o.Detail.HumanChangeRequester, o.Detail.HumanChangeRequestHead = detail.HumanChangeRequester, detail.HumanChangeRequestHead
}
if detail.Role != stagelabels.RoleNone {
o.Detail.Role = detail.Role
}
Expand Down Expand Up @@ -445,6 +448,20 @@
})
}

// noteStageHumanChangeRequest records that a trusted human's change request
// (review or `/hive decision <text>`) stands against the exact head. Stage
// derivation reads it as "the person answered": the requires_human /
// decision-requested verdict on that head no longer parks the PR at 085
// (hivecommons/hive#14679).
func (c *Engine) noteStageHumanChangeRequest(owner, repo string, number int, head, login string) {
if !c.stagePolicy().Enabled || strings.TrimSpace(login) == "" || strings.TrimSpace(head) == "" {
return
}
c.stageTrackerOrShared().note(stageKey(owner, repo, number), func(d *stageDetail) {
d.HumanChangeRequester, d.HumanChangeRequestHead = strings.TrimSpace(login), strings.TrimSpace(head)
})
}

// 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
Expand Down
64 changes: 58 additions & 6 deletions src/pkg/github/issue_unpark_command.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package github

import (
Expand Down Expand Up @@ -92,6 +92,30 @@
type unparkCommand struct {
Kind unparkCommandKind
Decision string
// Text is the whole decision as the person wrote it: the rest of the
// command line plus every line after it, trimmed. Decision stays the
// first line so the reply's `Decision:` round-trip is unchanged; the PR
// path hands Text to the author agent verbatim (hivecommons/hive#14679).
Text string
}

// BareApproval reports whether a `/hive decision` carries nothing beyond an
// approval word — the owner answered "yes" without an instruction to act on.
func (c unparkCommand) BareApproval() bool {
switch strings.ToLower(strings.TrimSpace(c.Text)) {
case "approve", "approved", "approve.", "approved.", "lgtm", "yes":
return true
}
return false
}

// DecisionText is what the author agent should follow: the full comment
// body after the command when it has one, else the first-line decision.
func (c unparkCommand) DecisionText() string {
if t := strings.TrimSpace(c.Text); t != "" {
return t
}
return strings.TrimSpace(c.Decision)
}

// IssueUnparkEvent records one issue this sweep un-parked, for the audit log.
Expand Down Expand Up @@ -149,11 +173,14 @@
// PullRequestDecisionEvent records one `/hive decision` on a hive-authored
// PR the sweep routed to the fix lane (hivecommons/hive#13041).
type PullRequestDecisionEvent struct {
Repo string
Number int
HeadSHA string
Actor string
Decision string
Repo string
Number int
HeadSHA string
Actor string
Decision string
// Mode is "changes_requested" (the decision carries an instruction)
// or "approve" (a bare `/hive decision approve`).
Mode string
CommentURL string
}

Expand Down Expand Up @@ -933,11 +960,36 @@
if decision == "" {
return unparkCommand{}, false
}
return unparkCommand{Kind: unparkCommandDecision, Decision: decision}, true
return unparkCommand{Kind: unparkCommandDecision, Decision: decision, Text: decisionText(body, decision)}, true
}
return unparkCommand{}, false
}

// decisionText joins the first-line decision with every line that follows
// the command line, so numbered instructions under `/hive decision approve`
// reach the author agent instead of being dropped (hivecommons/hive#14679).
func decisionText(body, firstLine string) string {
lines := strings.Split(body, "\n")
rest := make([]string, 0, len(lines))
seen := false
for _, line := range lines {
line = strings.TrimRight(strings.TrimSuffix(line, "\r"), " \t")
if !seen {
if strings.TrimSpace(line) == "" {
continue
}
seen = true
continue
}
rest = append(rest, line)
}
tail := strings.TrimSpace(strings.Join(rest, "\n"))
if tail == "" {
return firstLine
}
return strings.TrimSpace(firstLine + "\n\n" + tail)
}

// firstNonEmptyLine returns the comment's first line that has content,
// trimmed of surrounding whitespace.
func firstNonEmptyLine(body string) string {
Expand Down
29 changes: 29 additions & 0 deletions src/pkg/github/issue_unpark_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1241,3 +1241,32 @@ func TestSweepClosedIssueStageLabelsCap(t *testing.T) {
t.Fatalf("nil client err = %v, want ErrNoGitHubClient", err)
}
}

// hivecommons/hive#14679 (seen on #14628): the owner's `/hive decision
// approve` followed by numbered instructions must reach the author agent
// whole, and a bare approve must read as an approval, not an instruction.
func TestParseUnparkCommandKeepsDecisionTextAfterFirstLine(t *testing.T) {
body := "/hive decision approve\r\n\r\nPlease do the following before merging:\r\n\r\n1. Change `Closes #14605` to `Refs #14605`.\n2. Keep #14605 open.\n3. Merge on green.\n"
cmd, ok := parseUnparkCommand(body)
if !ok || cmd.Kind != unparkCommandDecision || cmd.Decision != "approve" {
t.Fatalf("parseUnparkCommand = %+v, %v", cmd, ok)
}
if cmd.BareApproval() {
t.Fatalf("approve with instructions read as a bare approval: %q", cmd.Text)
}
for _, want := range []string{"approve\n\n", "1. Change `Closes #14605` to `Refs #14605`.", "2. Keep #14605 open.", "3. Merge on green."} {
if !strings.Contains(cmd.DecisionText(), want) {
t.Fatalf("decision text missing %q:\n%s", want, cmd.DecisionText())
}
}
for _, bare := range []string{"/hive decision approve", "/hive decision Approved\n", "/hive decision approve.\n\n"} {
cmd, ok := parseUnparkCommand(bare)
if !ok || !cmd.BareApproval() || cmd.DecisionText() == "" {
t.Fatalf("parseUnparkCommand(%q) = %+v, %v; want a bare approval", bare, cmd, ok)
}
}
single, _ := parseUnparkCommand("/hive decision keep 10 stable tags")
if single.DecisionText() != "keep 10 stable tags" || single.BareApproval() {
t.Fatalf("single-line decision = %+v", single)
}
}
11 changes: 6 additions & 5 deletions src/pkg/github/pr_decision_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func (c *Client) RecordOperatorPullRequestDecision(ctx context.Context, repo str
if head == "" {
return PullRequestDecisionEvent{}, ErrPullRequestHeadUnknown
}
mode := prDecisionModeFor(unparkCommand{Kind: unparkCommandDecision, Decision: decision, Text: decision})
req := review.HumanChangeRequest{
Repo: owner + "/" + name,
Number: number,
Expand All @@ -220,19 +221,19 @@ func (c *Client) RecordOperatorPullRequestDecision(ctx context.Context, repo str
SourceID: operatorDecisionMarkerID,
Actor: actor,
Body: decision,
Kind: mode.kind(),
}
// Record before replying: a reply without a record would promise a fix
// nothing is queued to make.
if err := store.Record(req); err != nil {
return PullRequestDecisionEvent{}, fmt.Errorf("recording dashboard decision on %s/%s#%d: %w", owner, name, number, err)
}
body := renderPRDecisionRecordedReply(actor, head) + "\n\n_Recorded from the hive dashboard._"
stage := c.applyPRDecisionStage(ctx, owner, name, number, head, actor, mode, selfAuthored)
body := renderPRDecisionReply(actor, head, mode) + "\n\n_Recorded from the hive dashboard._"
if err := c.postUnparkReply(ctx, owner, name, number, operatorDecisionMarkerID, body); err != nil {
return PullRequestDecisionEvent{}, err
}
event := PullRequestDecisionEvent{Repo: owner + "/" + name, Number: number, HeadSHA: head, Actor: actor, Decision: decision}
if c.logger != nil {
c.logger.Info("pull request direction recorded by dashboard decision", "repo", event.Repo, "pr", number, "actor", actor, "head", head)
}
event := PullRequestDecisionEvent{Repo: owner + "/" + name, Number: number, HeadSHA: head, Actor: actor, Decision: decision, Mode: string(mode)}
c.logPRDecision(event.Repo, number, head, actor, mode, stage, false)
return event, nil
}
22 changes: 22 additions & 0 deletions src/pkg/github/pr_decision_operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,28 @@ func TestRecordOperatorPullRequestDecisionRecordsChangeRequestAndReplies(t *test
if !strings.Contains(posted, unparkReplyMarkerPrefix+"0 -->") {
t.Fatalf("reply must carry the shared unpark marker with a non-command id, got %q", posted)
}
if event.Mode != string(prDecisionChangesRequested) || got.IsApproval() {
t.Fatalf("event mode = %q, kind = %q; want changes_requested", event.Mode, got.Kind)
}
if facts := prDecisionFacts(t, c); len(facts) != 1 || !facts[0].Instruction || facts[0].Head != liveDecisionHead {
t.Fatalf("stage facts = %+v, want one PRDecided with the instruction", facts)
}

// A bare "approve" from the dashboard is an approval, not a fix request.
c2, rec2 := newUnparkPRClient(t, []unparkWireIssue{hiveParkedPR()}, nil)
event2, err := c2.RecordOperatorPullRequestDecision(context.Background(), "org/repo", liveDecisionPR, "maintainer", "approve", store)
if err != nil {
t.Fatalf("approve: %v", err)
}
if event2.Mode != string(prDecisionApprove) || review.HumanChangeRequestFor(store, "org/repo", liveDecisionPR, liveDecisionHead) != nil || review.HumanApprovalFor(store, "org/repo", liveDecisionPR, liveDecisionHead) == nil {
t.Fatalf("approve event = %+v, store = %+v", event2, store)
}
if posted := strings.Join(rec2.posted[liveDecisionPR], "\n"); !strings.Contains(posted, "Approval recorded by @maintainer") {
t.Fatalf("expected the approval reply, got %q", posted)
}
if facts := prDecisionFacts(t, c2); len(facts) != 1 || facts[0].Instruction {
t.Fatalf("stage facts = %+v, want one PRDecided without an instruction", facts)
}
}

func TestRecordOperatorPullRequestDecisionRefusesForeignEmptyAndUnstored(t *testing.T) {
Expand Down
Loading
Loading