From 63b6b6cad3c988d80de00bff8c389c9bf2355e99 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Thu, 29 Oct 2026 23:18:19 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20hold=20guard:=20accept=20a=20tru?= =?UTF-8?q?sted=20merger's=20recorded=20approval=20of=20the=20current=20he?= =?UTF-8?q?ad?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A dashboard Approve is recorded against the exact head the operator saw, and the hive then removes the sentinel/hold gate label for them. The hold guard only read the timeline, where that lift is by the hive's own account and can never cover the head, so a contributor push during the gate turned the operator's approval into a re-applied hold label six minutes later (hive#15482/#15487/#15489). Consult the head-scoped decision ledger before the timeline: an approval on the current head by a trusted merger accepts the head; one on an older head or by an untrusted actor still drifts. Fixes #15644 Signed-off-by: Andrew Anderson --- .../fixed-hold-guard-operator-approval.md | 1 + src/cmd/hive/hold_guard_test.go | 99 +++++++++++++++++++ src/cmd/hive/main_helpers.go | 47 +++++++++ 3 files changed, 147 insertions(+) create mode 100644 changelog.d/fixed-hold-guard-operator-approval.md diff --git a/changelog.d/fixed-hold-guard-operator-approval.md b/changelog.d/fixed-hold-guard-operator-approval.md new file mode 100644 index 0000000000..8eaeb5a0a1 --- /dev/null +++ b/changelog.d/fixed-hold-guard-operator-approval.md @@ -0,0 +1 @@ +- The hold guard no longer re-applies `hold` over a dashboard Approve: when a trusted merger approved the PR's current head through the hive's own decision record and the hive removed the sentinel/hold gate label on their behalf, the guard accepts that head instead of reading the hive's lift as unreviewed drift ([#15644](https://github.com/hivecommons/hive/issues/15644)). diff --git a/src/cmd/hive/hold_guard_test.go b/src/cmd/hive/hold_guard_test.go index b4194e25bc..29ce4b9dbe 100644 --- a/src/cmd/hive/hold_guard_test.go +++ b/src/cmd/hive/hold_guard_test.go @@ -18,6 +18,7 @@ import ( "github.com/hivecommons/hive/pkg/github" "github.com/hivecommons/hive/pkg/holdguard" + "github.com/hivecommons/hive/pkg/review" ) func swapHoldGuardStore(t *testing.T, s *holdguard.Store) { @@ -1015,3 +1016,101 @@ func TestWriteMergeEligibleExcludesHoldDriftPRs(t *testing.T) { t.Fatalf("ci_failing = %+v, want only drifted red PR 8 flagged held", failing.CIFailing) } } + +// swapHoldGuardHumanChangeRequests points the guard's operator-approval +// lookup at a memory-only decision ledger for the test's lifetime. +func swapHoldGuardHumanChangeRequests(t *testing.T) *review.HumanChangeRequests { + t.Helper() + store := review.NewHumanChangeRequests("") + old := holdGuardHumanChangeRequests + holdGuardHumanChangeRequests = func() review.HumanChangeRequestStore { return store } + t.Cleanup(func() { holdGuardHumanChangeRequests = old }) + return store +} + +func recordHoldGuardDecision(t *testing.T, store *review.HumanChangeRequests, head, actor, kind string) { + t.Helper() + if err := store.Record(review.HumanChangeRequest{ + Repo: "acme/widgets", Number: 7, HeadSHA: head, Source: review.HumanChangeRequestSourceDecision, + SourceID: 1, Actor: actor, Body: kind, Kind: kind, + }); err != nil { + t.Fatal(err) + } +} + +// hive#15482: the contributor pushed while the PR sat behind the sentinel +// gate; the operator then pressed Approve on the dashboard for that exact +// head and the hive removed the gate label on their behalf. The timeline +// shows a hive lift, which can never cover the head — but the head-keyed +// approval record by a trusted merger is the fresh review, so the guard must +// accept the head instead of re-applying `hold` over the approval. +func TestEnforceHoldGuardOperatorApprovalOnCurrentHeadAcceptsHiveLift(t *testing.T) { + store := newTestHoldGuardStore(t) + decisions := swapHoldGuardHumanChangeRequests(t) + recordHoldGuardDecision(t, decisions, "p1", "clubanderson", review.HumanChangeRequestKindApprove) + fake := &holdGuardServer{commits: map[int][]map[string]any{ + 7: {ghCommit("c1", "odttlnt", "deploy: monitoring"), ghCommit("p1", "odttlnt", "deploy: patch secret")}, + }} + fake.timeline = map[int][]map[string]any{ + 7: gateLiftTimeline("sentinel-alert", "kubestellar-hive[bot]", "Bot", "2026-10-22T18:19:00Z"), + } + client := newHoldGuardClient(t, fake) + client.SetAppBotLogin("kubestellar-hive[bot]") + cfg := escalationTestConfig() + cfg.Dashboard.AuthorizedUsers = []string{"clubanderson:owner"} + store.Snapshot("acme/widgets", 7, "c1", []holdguard.Commit{{SHA: "c1", Author: "odttlnt"}}) + store.Touch("acme/widgets", 7, "p1") + + got := enforceHoldGuard(context.Background(), cfg, client, client, + actionableWith(github.PullRequest{Repo: "widgets", Number: 7, Author: "odttlnt", HeadSHA: "p1"}), discardLogger()) + if got["widgets/7"] { + t.Fatalf("head approved by a trusted merger must not be re-review-gated, got %v", got) + } + if len(fake.comments) != 0 || len(fake.labels) != 0 { + t.Fatalf("comments=%v labels=%v, want no drift comment and no re-hold", fake.comments, fake.labels) + } + if _, ok := store.Recorded("acme/widgets", 7); ok { + t.Fatal("snapshot must clear once the approved head is accepted") + } +} + +// The approval must name the head on the branch now: one recorded on the +// pre-push head, or by someone outside the merger tier, is not a review of +// the current tree and the drift path stands. +func TestEnforceHoldGuardOperatorApprovalMustCoverCurrentHeadByTrustedMerger(t *testing.T) { + cases := []struct { + name, head, actor string + users []string + }{ + {name: "approval on the old head", head: "c1", actor: "clubanderson", users: []string{"clubanderson:owner"}}, + {name: "approver below merger tier", head: "p1", actor: "visitor", users: []string{"visitor:read-write"}}, + {name: "approver not authorized", head: "p1", actor: "stranger", users: []string{"clubanderson:owner"}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + store := newTestHoldGuardStore(t) + decisions := swapHoldGuardHumanChangeRequests(t) + recordHoldGuardDecision(t, decisions, tc.head, tc.actor, review.HumanChangeRequestKindApprove) + fake := &holdGuardServer{commits: map[int][]map[string]any{ + 7: {ghCommit("c1", "odttlnt", "deploy: monitoring"), ghCommit("p1", "odttlnt", "deploy: patch secret")}, + }} + fake.timeline = map[int][]map[string]any{ + 7: gateLiftTimeline("sentinel-alert", "kubestellar-hive[bot]", "Bot", "2026-10-22T18:19:00Z"), + } + client := newHoldGuardClient(t, fake) + client.SetAppBotLogin("kubestellar-hive[bot]") + cfg := escalationTestConfig() + cfg.Dashboard.AuthorizedUsers = tc.users + store.Snapshot("acme/widgets", 7, "c1", []holdguard.Commit{{SHA: "c1", Author: "odttlnt"}}) + + got := enforceHoldGuard(context.Background(), cfg, client, client, + actionableWith(github.PullRequest{Repo: "widgets", Number: 7, Author: "odttlnt", HeadSHA: "p1"}), discardLogger()) + if !got["widgets/7"] { + t.Fatalf("must still be re-review-gated, got %v", got) + } + if len(fake.labels) != 1 || fake.labels[0] != holdguard.ReHoldLabel { + t.Fatalf("labels=%v, want re-hold", fake.labels) + } + }) + } +} diff --git a/src/cmd/hive/main_helpers.go b/src/cmd/hive/main_helpers.go index 7b7a64e167..275e5db6d1 100644 --- a/src/cmd/hive/main_helpers.go +++ b/src/cmd/hive/main_helpers.go @@ -15,6 +15,7 @@ import ( "github.com/hivecommons/hive/pkg/github/automerge" "github.com/hivecommons/hive/pkg/holdguard" "github.com/hivecommons/hive/pkg/intent" + "github.com/hivecommons/hive/pkg/review" "github.com/hivecommons/hive/pkg/stageengine/prstage" "github.com/hivecommons/hive/pkg/writeback" ) @@ -38,6 +39,40 @@ func getHoldGuardStore() *holdguard.Store { return holdGuardStore } +// holdGuardHumanChangeRequests resolves the decision ledger the hold guard +// reads operator approvals from; tests swap in a memory-only store. +var holdGuardHumanChangeRequests = func() review.HumanChangeRequestStore { + return review.DefaultHumanChangeRequests() +} + +// holdGuardOperatorApproval reports the trusted merger whose recorded +// approval (dashboard Approve or `/hive decision approve`, +// pr_decision_operator.go) stands against exactly headSHA on repo#number. +// The record is head-scoped — a newer head supersedes it — so it is proof +// the person reviewed the tree on the branch now, which the gate-lift +// timeline cannot show when the hive removed the label for them. Fails +// closed: no record, a change request, an unknown or untrusted actor, or an +// empty head never cover the head. +func holdGuardOperatorApproval(cfg *config.Config, repo string, number int, headSHA string) (string, bool) { + headSHA = strings.TrimSpace(headSHA) + if cfg == nil || headSHA == "" { + return "", false + } + store := holdGuardHumanChangeRequests() + if store == nil { + return "", false + } + req, ok := store.For(repo, number, headSHA) + if !ok || !req.IsApproval() || !strings.EqualFold(strings.TrimSpace(req.HeadSHA), headSHA) { + return "", false + } + login := strings.TrimSpace(req.Actor) + if !trustedMergerFunc(cfg)(login) { + return "", false + } + return login, true +} + // enforceHoldGuard closes #5589: a hold-gated PR that accumulates commits — // from ANY author, but especially from other agents via contaminated worktree // bases — must not sail into the merge lanes when the hold lifts, because the @@ -161,6 +196,18 @@ func enforceHoldGuard( continue } } + // A dashboard Approve (or `/hive decision approve`) is recorded + // against the exact head the person saw; the hive then removes the + // gate label on their behalf, so the timeline below shows a hive + // lift that can never cover the head. The head-keyed approval by a + // trusted merger is the review the guard asks for: accept it. + if login, ok := holdGuardOperatorApproval(cfg, repo, pr.Number, pr.HeadSHA); ok { + store.Clear(repo, pr.Number) + logger.Info("hold guard: current head approved by a trusted merger through the hive's decision record; head accepted", + "repo", repo, "pr", pr.Number, + "recorded_head", rec.HeadSHA, "current_head", pr.HeadSHA, "approver", login) + continue + } if ghClient != nil { // A trusted human who removed the gate label AFTER the current // head arrived reviewed the head that is on the branch now; an