From 15024d25e607c1bf0992ddfb148cf04825c20ae0 Mon Sep 17 00:00:00 2001 From: thatguywob Date: Wed, 4 Nov 2026 15:55:34 +0000 Subject: [PATCH 1/8] =?UTF-8?q?=E2=9C=A8=20feat(ci):=20approve=20gated=20w?= =?UTF-8?q?orkflows=20for=20trusted=20PR=20heads?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Start trusted CI without waiting for a maintainer click when GitHub has created no check runs yet. Bind approvals to fresh PR and run evidence, suppress repeat attempts, and show permission refusals to operators. Hive-Run: hivecommons/hive#16603 Hive-Plan: workflow-approval-sweep Hive-Spec: hivecommons/hive#16603#proposal Signed-off-by: thatguywob Co-authored-by: clubanderson <407614+clubanderson@users.noreply.github.com> --- changelog.d/added-auto-approve-workflows.md | 1 + src/cmd/hive/main_eval_cycle.go | 3 + src/hive.yaml.example | 6 + src/pkg/config/config.go | 1 + src/pkg/config/governor_ci.go | 21 ++ src/pkg/config/governor_ci_test.go | 49 +++ src/pkg/dashboard/server.go | 6 +- src/pkg/dashboard/stage_tracker_activity.go | 36 +- src/pkg/dashboard/workflow_approvals_test.go | 102 ++++++ src/pkg/github/client.go | 1 + src/pkg/github/webhook_ci_evidence.go | 9 +- src/pkg/github/workflow_approval.go | 207 +++++++---- src/pkg/github/workflow_approval_test.go | 26 +- src/pkg/github/workflow_auto_approval.go | 177 +++++++++ src/pkg/github/workflow_auto_approval_test.go | 337 ++++++++++++++++++ 15 files changed, 877 insertions(+), 105 deletions(-) create mode 100644 changelog.d/added-auto-approve-workflows.md create mode 100644 src/pkg/config/governor_ci.go create mode 100644 src/pkg/config/governor_ci_test.go create mode 100644 src/pkg/dashboard/workflow_approvals_test.go create mode 100644 src/pkg/github/workflow_auto_approval.go create mode 100644 src/pkg/github/workflow_auto_approval_test.go diff --git a/changelog.d/added-auto-approve-workflows.md b/changelog.d/added-auto-approve-workflows.md new file mode 100644 index 0000000000..24326f1c4d --- /dev/null +++ b/changelog.d/added-auto-approve-workflows.md @@ -0,0 +1 @@ +- Hive approves GitHub-gated workflow runs once per PR head for trusted bots, its App, or owner-admitted PRs, with configurable trust, approval counters and a manual-action caption when permissions are missing. diff --git a/src/cmd/hive/main_eval_cycle.go b/src/cmd/hive/main_eval_cycle.go index 3aacf02c1b..39a091883d 100644 --- a/src/cmd/hive/main_eval_cycle.go +++ b/src/cmd/hive/main_eval_cycle.go @@ -581,6 +581,9 @@ func runEvalCycle( } ghClient.EnrichCIStatus(ctx, actionable.PRs.Items) + if !budgetDecision.SkipOptional { + ghClient.SweepWorkflowApprovals(ctx, actionable.PRs.Items, cfg.Governor.CI) + } // Held PRs need CI status too, or a red held PR can never be repaired: // the hold label kept it out of PRs.Items, so nothing ever learned it was // red and no agent was ever told to fix it (hivecommons/hive#7438). This diff --git a/src/hive.yaml.example b/src/hive.yaml.example index a895a2abf9..539a329770 100644 --- a/src/hive.yaml.example +++ b/src/hive.yaml.example @@ -413,6 +413,12 @@ agents: # Governor — adaptive scheduler that adjusts agent cadences based on queue depth governor: + # GitHub's Approve and run workflows gate. This starts CI only; the + # existing writeback, hold, review and merge gates still apply. + # ci: + # auto_approve_workflows: true + # auto_approve_workflow_actors: ['dependabot[bot]', 'renovate[bot]'] + # An explicit [] trusts only this hive's App and owner-admitted PRs. eval_interval_s: 300 # Seconds between governor evaluations eval_interval_max_s: 1800 # Cap when API budget mode stretches evals eval_interval_webhook_s: 900 # Base interval while GitHub webhooks are healthy (see /api/webhook/github) diff --git a/src/pkg/config/config.go b/src/pkg/config/config.go index 401f97b80d..606fab07a6 100644 --- a/src/pkg/config/config.go +++ b/src/pkg/config/config.go @@ -1574,6 +1574,7 @@ func (g *GovernorConfig) ReleaseCadenceOwnership(mode, agent string) { } type GovernorConfig struct { + CI GovernorCIConfig `yaml:"ci,omitempty" json:"ci,omitempty"` Modes map[string]ModeConfig `yaml:"modes"` EvalIntervalS int `yaml:"eval_interval_s"` EvalIntervalMaxS int `yaml:"eval_interval_max_s,omitempty"` diff --git a/src/pkg/config/governor_ci.go b/src/pkg/config/governor_ci.go new file mode 100644 index 0000000000..4cf4893bc2 --- /dev/null +++ b/src/pkg/config/governor_ci.go @@ -0,0 +1,21 @@ +package config + +// GovernorCIConfig controls approval of GitHub's gated workflow runs. Trust +// permits starting CI only; every existing review and merge gate still applies. +type GovernorCIConfig struct { + AutoApproveWorkflows *bool `yaml:"auto_approve_workflows,omitempty" json:"auto_approve_workflows,omitempty"` + AutoApproveWorkflowActors *[]string `yaml:"auto_approve_workflow_actors,omitempty" json:"auto_approve_workflow_actors,omitempty"` +} + +func (c GovernorCIConfig) AutoApproveWorkflowsEnabled() bool { + return c.AutoApproveWorkflows == nil || *c.AutoApproveWorkflows +} + +// WorkflowApprovalActors distinguishes an omitted list (trusted bot defaults) +// from an explicit empty list (only the App and owner admission are trusted). +func (c GovernorCIConfig) WorkflowApprovalActors() []string { + if c.AutoApproveWorkflowActors == nil { + return []string{"dependabot[bot]", "renovate[bot]"} + } + return *c.AutoApproveWorkflowActors +} diff --git a/src/pkg/config/governor_ci_test.go b/src/pkg/config/governor_ci_test.go new file mode 100644 index 0000000000..02c0a8b9d9 --- /dev/null +++ b/src/pkg/config/governor_ci_test.go @@ -0,0 +1,49 @@ +package config + +import ( + "encoding/json" + "reflect" + "testing" + + "gopkg.in/yaml.v3" +) + +func TestGovernorCIWorkflowApprovalConfig(t *testing.T) { + for _, tc := range []struct { + name, yaml string + enabled bool + actors []string + }{ + {"defaults", "{}", true, []string{"dependabot[bot]", "renovate[bot]"}}, + {"disabled", "governor:\n ci:\n auto_approve_workflows: false\n auto_approve_workflow_actors: []", false, []string{}}, + {"custom", "governor:\n ci:\n auto_approve_workflows: true\n auto_approve_workflow_actors: [trusted-bot]", true, []string{"trusted-bot"}}, + } { + t.Run(tc.name, func(t *testing.T) { + var cfg Config + if err := yaml.Unmarshal([]byte(tc.yaml), &cfg); err != nil { + t.Fatal(err) + } + if cfg.Governor.CI.AutoApproveWorkflowsEnabled() != tc.enabled || !reflect.DeepEqual(cfg.Governor.CI.WorkflowApprovalActors(), tc.actors) { + t.Fatalf("CI config=%+v actors=%v", cfg.Governor.CI, cfg.Governor.CI.WorkflowApprovalActors()) + } + // Saving configuration must preserve an explicitly empty trust + // list rather than restoring the default bots after a reload. + for _, codec := range []struct { + marshal func(any) ([]byte, error) + unmarshal func([]byte, any) error + }{{yaml.Marshal, yaml.Unmarshal}, {json.Marshal, json.Unmarshal}} { + data, err := codec.marshal(cfg.Governor.CI) + if err != nil { + t.Fatal(err) + } + var restored GovernorCIConfig + if err := codec.unmarshal(data, &restored); err != nil { + t.Fatal(err) + } + if restored.AutoApproveWorkflowsEnabled() != tc.enabled || !reflect.DeepEqual(restored.WorkflowApprovalActors(), tc.actors) { + t.Fatalf("round trip changed trust: %s -> %+v", data, restored) + } + } + }) + } +} diff --git a/src/pkg/dashboard/server.go b/src/pkg/dashboard/server.go index b6a84cb3e8..d44ea8c9b1 100644 --- a/src/pkg/dashboard/server.go +++ b/src/pkg/dashboard/server.go @@ -3754,6 +3754,9 @@ func (s *Server) handleStatus(w http.ResponseWriter, r *http.Request) { payload.Writeback = s.WritebackStatus() payload.Hotspots = s.HotspotStatusBlock() payload.Config = s.configHealthStatus() + if s.deps != nil { + payload.WorkflowApprovals = s.deps.GHClient.WorkflowApprovalsStatus() + } statusJSONResponse(w, r, filterStatusPayload(&payload, r.URL.Query().Get("fields"), r.URL.Query().Get("omit"))) } @@ -3783,7 +3786,8 @@ type statusWithWebhooks struct { Webhooks github.WebhookHealth `json:"webhooks"` // CIFailureSignals is the webhook CI-failure signal store: retained // signals, the last one, webhook-triggered kicks and unmatched heads. - CIFailureSignals github.CIFailureSignalsStatus `json:"ci_failure_signals"` + CIFailureSignals github.CIFailureSignalsStatus `json:"ci_failure_signals"` + WorkflowApprovals github.WorkflowApprovalsStatus `json:"workflow_approvals"` *EvalLoopStatus // MatrixEnabled reports whether the Matrix block is active (enabled with // at least one repo, see config.MatrixConfig.Active) so the header shows diff --git a/src/pkg/dashboard/stage_tracker_activity.go b/src/pkg/dashboard/stage_tracker_activity.go index e85ee45610..9413846d4d 100644 --- a/src/pkg/dashboard/stage_tracker_activity.go +++ b/src/pkg/dashboard/stage_tracker_activity.go @@ -67,6 +67,27 @@ func stageTrackerCIActivity(pr github.PullRequest, now time.Time) *StageTrackerA if a.StatusesKnown { out.Source += "+github-commit-statuses" } + defer func() { + wa := a.WorkflowApproval + if wa == nil || wa.Pending <= 0 || (out.State != "missing" && out.State != "unknown") { + return + } + out.State, out.Blocker = "missing", stageTrackerWorkflowApprovalBlocker(wa.Pending) + verb := "need" + if wa.Pending == 1 { + verb = "needs" + } + out.Caption = fmt.Sprintf("CI waiting · %s %s approval on GitHub", plural(wa.Pending, "workflow run"), verb) + out.Detail = "GitHub is holding workflow runs for this commit behind Approve and run workflows." + if wa.PermissionDenied { + out.Caption = "CI waiting · needs workflow approval on GitHub" + out.Detail = "The App was refused workflow approval (actions: write is required). A maintainer must click Approve and run workflows on GitHub." + } + if out.Active { + out.Caption += " · other checks still running" + } + out.Actor, out.Since = wa.Actor, wa.ObservedAt + }() if !a.RequiredKnown || a.RequiredSource == "" || len(a.RequiredChecks) == 0 || len(a.Required) == 0 { stageTrackerCountHeadChecks(out, a) return out @@ -202,21 +223,6 @@ func stageTrackerCIActivity(pr github.PullRequest, now time.Time) *StageTrackerA out.Caption = "CI queued · " + check + " waiting for a runner" out.Detail = "Required check " + check + " has been requested for the PR's current commit but has not started executing yet." case "missing": - if wa := a.WorkflowApproval; wa != nil && wa.Pending > 0 { - // Unreported because GitHub holds the runs behind its - // approve-workflows gate: a person's click, not a push, - // moves it (hivecommons/hive#16599). - out.Blocker = stageTrackerWorkflowApprovalBlocker(wa.Pending) - out.Caption = fmt.Sprintf("CI waiting · %s %s approval on GitHub", plural(wa.Pending, "workflow run"), pluralVerb(wa.Pending, "needs", "need")) - out.Detail = fmt.Sprintf("%s for the PR's current commit %s waiting for a maintainer to approve workflows on GitHub (the gate GitHub applies to dependabot and first-time or fork contributors), so required check %s cannot report until someone approves them.", plural(wa.Pending, "workflow run"), pluralVerb(wa.Pending, "is", "are"), check) - if wa.Actor != "" { - out.Actor = wa.Actor - } - if wa.ObservedAt != nil { - out.Since = wa.ObservedAt - } - break - } out.Caption = "CI waiting · required check " + check + " hasn't reported for this head yet" out.Detail = "Branch protection requires a check named " + check + ", but no check run or commit status with that name exists for the PR's current commit. Usually the workflow that produces it has not been triggered; a push, re-run, or label can start it." case "unknown": diff --git a/src/pkg/dashboard/workflow_approvals_test.go b/src/pkg/dashboard/workflow_approvals_test.go new file mode 100644 index 0000000000..49d93c4e85 --- /dev/null +++ b/src/pkg/dashboard/workflow_approvals_test.go @@ -0,0 +1,102 @@ +package dashboard + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "net/url" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/github" +) + +func TestStageTrackerWorkflowApprovalCaption(t *testing.T) { + now := time.Now().UTC() + for _, tc := range []struct { + name string + pending int + permission bool + caption string + }{ + {"one run", 1, false, "CI waiting · 1 workflow run needs approval on GitHub"}, + {"several runs", 3, false, "CI waiting · 3 workflow runs need approval on GitHub"}, + {"refused", 3, true, "CI waiting · needs workflow approval on GitHub"}, + } { + t.Run(tc.name, func(t *testing.T) { + pr := github.PullRequest{HeadSHA: "head", CIActivity: &github.PRCIActivity{ + Head: "head", Source: "github-check-runs", ObservedAt: &now, + WorkflowApproval: &github.PRWorkflowApproval{Pending: tc.pending, PermissionDenied: tc.permission, ObservedAt: &now}, + }} + got := stageTrackerCIActivity(pr, now) + if got.Caption != tc.caption || got.State != "missing" || got.Active { + t.Fatalf("activity=%+v", got) + } + if tc.permission && !strings.Contains(got.Detail, "actions: write") { + t.Fatalf("permission requirement missing: %s", got.Detail) + } + pr.CIActivity.Head = "oldhead" + if got := stageTrackerCIActivity(pr, now); got.State != "unknown" || strings.Contains(got.Caption, "workflow approval") { + t.Fatalf("stale refusal leaked: %+v", got) + } + }) + } +} + +func TestStageTrackerWorkflowApprovalDoesNotHideRequiredFailure(t *testing.T) { + now := time.Now().UTC() + pr := github.PullRequest{HeadSHA: "head", CIActivity: &github.PRCIActivity{ + Head: "head", Source: "github-check-runs", ObservedAt: &now, + RequiredKnown: true, RequiredSource: "protection", Required: []string{"build", "test"}, + RequiredChecks: []github.PRRequiredCheck{{Name: "build", AppKnown: true}, {Name: "test", AppKnown: true}}, + StatusesKnown: true, Checks: []github.PRActivityCheck{{Name: "build", State: "completed", Conclusion: "failure"}}, + WorkflowApproval: &github.PRWorkflowApproval{Pending: 2, PermissionDenied: true}, + }} + if got := stageTrackerCIActivity(pr, now); got.State != "failing" || got.ChecksFailed != 1 { + t.Fatalf("workflow approval hid a red required check: %+v", got) + } +} + +func TestStatusWorkflowApprovals(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/repos/org/repo/actions/runs": + _, _ = w.Write([]byte(`{"workflow_runs":[{"id":11,"head_sha":"head","event":"pull_request","status":"action_required","head_repository":{"full_name":"org/repo"}}]}`)) + case "/repos/org/repo/pulls/7": + _, _ = w.Write([]byte(`{"number":7,"state":"open","user":{"login":"dependabot[bot]"},"head":{"sha":"head","repo":{"full_name":"org/repo"}}}`)) + case "/repos/org/repo/actions/runs/11/approve": + if r.Method != http.MethodPost { + t.Errorf("approval method=%s", r.Method) + } + http.Error(w, `{"message":"forbidden"}`, http.StatusForbidden) + default: + t.Errorf("unexpected request: %s", r.URL) + http.NotFound(w, r) + } + })) + defer srv.Close() + s := newTestServer() + c := github.NewClient("fake", "org", []string{"repo"}, s.logger, "") + base, err := url.Parse(srv.URL + "/") + if err != nil { + t.Fatal(err) + } + c.GoGitHub().BaseURL = base + c.SweepWorkflowApprovals(context.Background(), []github.PullRequest{{Repo: "org/repo", Number: 7, HeadSHA: "head", CIStatus: "pending"}}, config.GovernorCIConfig{}) + s.deps = &Dependencies{GHClient: c} + s.UpdateStatus(minimalPayload()) + rec := httptest.NewRecorder() + s.handleStatus(rec, httptest.NewRequest(http.MethodGet, "/api/status?fields=workflow_approvals", nil)) + var payload struct { + Approvals github.WorkflowApprovalsStatus `json:"workflow_approvals"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil { + t.Fatal(err) + } + if payload.Approvals.PermissionErrors != 1 || payload.Approvals.Last == nil || payload.Approvals.Last.RunID != 11 { + t.Fatalf("status=%s", rec.Body.String()) + } +} diff --git a/src/pkg/github/client.go b/src/pkg/github/client.go index 333f1cdeaa..1def9a633f 100644 --- a/src/pkg/github/client.go +++ b/src/pkg/github/client.go @@ -163,6 +163,7 @@ type Client struct { mergeAlertIDsByRepo map[string]map[string]*mergeAlertEntry mergeAlertLastRevalidate time.Time logger *slog.Logger + workflowApprovals workflowApprovalState mergedClaimScanTransient sync.Map // repo -> true while its merged-PR scan is timing out stageFacts stageFactsState // out-of-cycle stage writes; see StageFacts apiBudget apiBudgetState diff --git a/src/pkg/github/webhook_ci_evidence.go b/src/pkg/github/webhook_ci_evidence.go index 576d51c812..261748df1f 100644 --- a/src/pkg/github/webhook_ci_evidence.go +++ b/src/pkg/github/webhook_ci_evidence.go @@ -278,6 +278,13 @@ func (s *ciEvidenceStore) merged(repo, head string, base *PRCIActivity) *PRCIAct if base != nil && (listed == nil || observedAfter(base.ObservedAt, listed.ObservedAt)) { listed = base } + // The approval sweep runs after the check listing. Its workflow-run + // observation has a separate clock, so selecting check evidence must + // not discard (or revive) the base snapshot's approval gate. + if base != nil && listed != nil && listed != base { + listed = cloneCIActivity(listed) + listed.WorkflowApproval = base.WorkflowApproval + } if len(events) == 0 { return listed } @@ -488,7 +495,7 @@ func (c *Client) applyWebhookCIEvidence(event string, p webhookEventPayload, con // Any CI delivery for the head — a workflow_run leaving action_required // once someone clicked Approve, a check run starting — means the cached // approval probe may be stale; re-list on the next cycle. - forgetWorkflowApproval(repo, head) + c.forgetWorkflowApproval(repo, head) out := &WebhookCIEvidence{HeadSHA: head, PRs: append([]int(nil), numbers...)} store := sharedCIEvidence() checks := webhookCIChecks(event, p) diff --git a/src/pkg/github/workflow_approval.go b/src/pkg/github/workflow_approval.go index dc23eacf1c..75cb0ba0a4 100644 --- a/src/pkg/github/workflow_approval.go +++ b/src/pkg/github/workflow_approval.go @@ -4,31 +4,24 @@ import ( "context" "strings" "sync" - "sync/atomic" "time" gh "github.com/google/go-github/v72/github" ) -// PRWorkflowApproval records workflow runs on a PR head that GitHub holds -// behind its "approve and run workflows" gate (status action_required). Such -// runs create no check runs, so from the check-run listing alone the required -// checks simply look unreported; this is the evidence that they are in fact -// waiting on a person clicking Approve (hivecommons/hive#16599). +// PRWorkflowApproval is evidence of GitHub's approve-and-run gate, which +// can hold workflow runs before they create any check runs. type PRWorkflowApproval struct { - Pending int `json:"pending"` - RunIDs []int64 `json:"run_ids,omitempty"` - Actor string `json:"actor,omitempty"` - ObservedAt *time.Time `json:"observed_at,omitempty"` + Pending int `json:"pending"` + RunIDs []int64 `json:"run_ids,omitempty"` + Actor string `json:"actor,omitempty"` + ObservedAt *time.Time `json:"observed_at,omitempty"` + PermissionDenied bool `json:"permission_denied,omitempty"` + runs []*gh.WorkflowRun } -// WorkflowApprovalStatus is the workflow-run status GitHub reports while a -// run waits for a maintainer to approve it. +// WorkflowApprovalStatus is GitHub's status/conclusion for approval-gated runs. const WorkflowApprovalStatus = "action_required" - -// workflowApprovalCacheTTL bounds the extra workflow-run listing to one call -// per head per window; the cycle calls EnrichCIStatus far more often than a -// human approves workflows. const workflowApprovalCacheTTL = 90 * time.Second type workflowApprovalEntry struct { @@ -36,71 +29,126 @@ type workflowApprovalEntry struct { val *PRWorkflowApproval } -var workflowApprovalCache sync.Map // repo@sha → workflowApprovalEntry +type workflowApprovalKey struct { + repo string + number int + head string +} + +type workflowApprovalAttempt struct { + attempted bool + denied bool + permissionDenied bool +} + +type workflowApprovalState struct { + mu sync.Mutex + probes map[string]workflowApprovalEntry + attempts map[workflowApprovalKey]workflowApprovalAttempt + status WorkflowApprovalsStatus +} -// workflowApprovalEvictAfter is how long a head's probe stays cached once -// it is no longer refreshed: heads that got pushed over or merged stop -// being probed, and their entries go with the next sweep of the map. -const workflowApprovalEvictAfter = 30 * time.Minute +// WorkflowApprovalsStatus counts decisions since this client was created. +type WorkflowApprovalsStatus struct { + Approved int `json:"approved"` + Denied int `json:"denied"` + PermissionErrors int `json:"permission_errors"` + Last *WorkflowApprovalEvent `json:"last,omitempty"` +} -var workflowApprovalLastEvict atomic.Int64 // unix seconds +// WorkflowApprovalEvent describes the most recent approval decision. +type WorkflowApprovalEvent struct { + Repo string `json:"repo"` + Number int `json:"number"` + Head string `json:"head"` + RunID int64 `json:"run_id"` + Outcome string `json:"outcome"` + At time.Time `json:"at"` +} -// evictStaleWorkflowApprovals drops entries older than -// workflowApprovalEvictAfter, at most once per eviction window, so the -// package-global map stays bounded by the heads probed in the last half hour. -func evictStaleWorkflowApprovals(now time.Time) { - last := workflowApprovalLastEvict.Load() - if now.Unix()-last < int64(workflowApprovalEvictAfter/time.Second) { - return +// WorkflowApprovalsStatus returns a snapshot suitable for /api/status. +func (c *Client) WorkflowApprovalsStatus() WorkflowApprovalsStatus { + if c == nil { + return WorkflowApprovalsStatus{} } - if !workflowApprovalLastEvict.CompareAndSwap(last, now.Unix()) { - return + c.workflowApprovals.mu.Lock() + defer c.workflowApprovals.mu.Unlock() + out := c.workflowApprovals.status + if out.Last != nil { + last := *out.Last + out.Last = &last } - workflowApprovalCache.Range(func(key, value any) bool { - if e, ok := value.(workflowApprovalEntry); !ok || now.Sub(e.at) > workflowApprovalEvictAfter { - workflowApprovalCache.Delete(key) - } - return true - }) + return out +} + +func (s *workflowApprovalState) record(key workflowApprovalKey, runID int64, outcome string) { + s.mu.Lock() + defer s.mu.Unlock() + switch outcome { + case "approved": + s.status.Approved++ + case "denied": + s.status.Denied++ + case "permission_error": + s.status.PermissionErrors++ + attempt := s.attempts[key] + attempt.permissionDenied = true + s.attempts[key] = attempt + } + s.status.Last = &WorkflowApprovalEvent{Repo: key.repo, Number: key.number, Head: key.head, RunID: runID, Outcome: outcome, At: time.Now().UTC()} } -// workflowRunsAwaitingApproval lists the head's workflow runs and keeps the -// ones gated on approval. It returns nil when none are, or when the listing -// failed: absence of evidence is not evidence. +// workflowRunsAwaitingApproval memoizes the read for a short window. The +// caller validates each run's head repository and SHA before any mutation. func (c *Client) workflowRunsAwaitingApproval(ctx context.Context, repo, sha string) *PRWorkflowApproval { if c == nil || c.client == nil || strings.TrimSpace(sha) == "" { return nil } key := repo + "@" + sha - evictStaleWorkflowApprovals(time.Now()) - if cached, ok := workflowApprovalCache.Load(key); ok { - if e, ok := cached.(workflowApprovalEntry); ok && time.Since(e.at) < workflowApprovalCacheTTL { - return e.val - } + c.workflowApprovals.mu.Lock() + cached, ok := c.workflowApprovals.probes[key] + c.workflowApprovals.mu.Unlock() + if ok && time.Since(cached.at) < workflowApprovalCacheTTL { + return cached.val } owner, name := c.splitRepo(repo) - runs, _, err := c.client.Actions.ListRepositoryWorkflowRuns(WithRESTCaller(ctx, "hive:workflow_approval"), owner, name, &gh.ListWorkflowRunsOptions{ - HeadSHA: sha, - ListOptions: gh.ListOptions{PerPage: 100}, - }) - if err != nil || runs == nil { - c.logger.Debug("failed to list workflow runs for head", "repo", repo, "sha", sha, "error", err) - return nil + opts := &gh.ListWorkflowRunsOptions{ + HeadSHA: sha, Event: "pull_request", ListOptions: gh.ListOptions{PerPage: 100}, + } + var current []*gh.WorkflowRun + for { + runs, resp, err := c.client.Actions.ListRepositoryWorkflowRuns(WithRESTCaller(ctx, "hive:workflow_approval"), owner, name, opts) + if err != nil || runs == nil { + c.logger.Debug("failed to list workflow runs for head", "repo", repo, "sha", sha, "error", err) + return nil + } + // Do not turn a response for a different head into approval evidence. + for _, run := range runs.WorkflowRuns { + if run != nil && strings.EqualFold(run.GetHeadSHA(), sha) && run.GetEvent() == "pull_request" { + current = append(current, run) + } + } + if resp == nil || resp.NextPage == 0 { + break + } + opts.Page = resp.NextPage } - val := workflowApprovalFromRuns(runs.WorkflowRuns, time.Now().UTC()) - workflowApprovalCache.Store(key, workflowApprovalEntry{at: time.Now(), val: val}) + val := workflowApprovalFromRuns(current, time.Now().UTC()) + c.workflowApprovals.mu.Lock() + if c.workflowApprovals.probes == nil { + c.workflowApprovals.probes = map[string]workflowApprovalEntry{} + } + // Bound the probe cache to heads seen in the current TTL window. + c.workflowApprovals.evictStaleProbes(time.Now()) + c.workflowApprovals.probes[key] = workflowApprovalEntry{at: time.Now(), val: val} + c.workflowApprovals.mu.Unlock() return val } -// workflowApprovalFromRuns is the pure half: the approval-gated subset of a -// head's workflow runs, or nil when the head has none. func workflowApprovalFromRuns(runs []*gh.WorkflowRun, now time.Time) *PRWorkflowApproval { var out *PRWorkflowApproval for _, run := range runs { - if run == nil { - continue - } - if run.GetStatus() != WorkflowApprovalStatus && run.GetConclusion() != WorkflowApprovalStatus { + if run == nil || run.GetID() <= 0 || (run.GetStatus() != WorkflowApprovalStatus && run.GetConclusion() != WorkflowApprovalStatus) { continue } if out == nil { @@ -108,23 +156,36 @@ func workflowApprovalFromRuns(runs []*gh.WorkflowRun, now time.Time) *PRWorkflow out = &PRWorkflowApproval{ObservedAt: &at} } out.Pending++ - if id := run.GetID(); id != 0 { - out.RunIDs = append(out.RunIDs, id) - } + out.RunIDs = append(out.RunIDs, run.GetID()) + out.runs = append(out.runs, run) if out.Actor == "" { - if actor := run.GetActor().GetLogin(); actor != "" { - out.Actor = actor - } else if actor := run.GetTriggeringActor().GetLogin(); actor != "" { - out.Actor = actor - } + out.Actor = workflowTriggeringActor(run) } } return out } -// forgetWorkflowApproval drops the cached probe for a head so the next -// cycle re-lists: called when a CI webhook for the head arrives (someone -// approved, a run started) and after the hive approves runs itself. -func forgetWorkflowApproval(repo, sha string) { - workflowApprovalCache.Delete(repo + "@" + sha) +func workflowTriggeringActor(run *gh.WorkflowRun) string { + if login := run.GetTriggeringActor().GetLogin(); login != "" { + return login + } + return run.GetActor().GetLogin() +} + +// evictStaleProbes bounds the per-client cache to heads seen in the current +// TTL window. The caller holds s.mu. +func (s *workflowApprovalState) evictStaleProbes(now time.Time) { + for key, entry := range s.probes { + if now.Sub(entry.at) >= workflowApprovalCacheTTL { + delete(s.probes, key) + } + } +} + +// forgetWorkflowApproval drops a head's probe after CI webhooks or successful +// approvals, so the next cycle observes the workflow gate again. +func (c *Client) forgetWorkflowApproval(repo, sha string) { + c.workflowApprovals.mu.Lock() + defer c.workflowApprovals.mu.Unlock() + delete(c.workflowApprovals.probes, repo+"@"+sha) } diff --git a/src/pkg/github/workflow_approval_test.go b/src/pkg/github/workflow_approval_test.go index a9cacfc97a..613e66d13b 100644 --- a/src/pkg/github/workflow_approval_test.go +++ b/src/pkg/github/workflow_approval_test.go @@ -45,25 +45,21 @@ func TestCloneCIActivityCopiesWorkflowApproval(t *testing.T) { } func TestWorkflowApprovalCacheEvictsAndForgets(t *testing.T) { - workflowApprovalCache.Store("org/repo@old", workflowApprovalEntry{at: time.Now().Add(-2 * workflowApprovalEvictAfter)}) - workflowApprovalCache.Store("org/repo@fresh", workflowApprovalEntry{at: time.Now()}) - workflowApprovalLastEvict.Store(0) - evictStaleWorkflowApprovals(time.Now()) - if _, ok := workflowApprovalCache.Load("org/repo@old"); ok { + now := time.Now() + c := &Client{} + c.workflowApprovals.probes = map[string]workflowApprovalEntry{ + "org/repo@old": {at: now.Add(-2 * workflowApprovalCacheTTL)}, + "org/repo@fresh": {at: now}, + } + c.workflowApprovals.evictStaleProbes(now) + if _, ok := c.workflowApprovals.probes["org/repo@old"]; ok { t.Fatal("stale entry survived eviction") } - if _, ok := workflowApprovalCache.Load("org/repo@fresh"); !ok { + if _, ok := c.workflowApprovals.probes["org/repo@fresh"]; !ok { t.Fatal("fresh entry evicted") } - // Within the window the sweep is a no-op even for stale entries. - workflowApprovalCache.Store("org/repo@old2", workflowApprovalEntry{at: time.Now().Add(-2 * workflowApprovalEvictAfter)}) - evictStaleWorkflowApprovals(time.Now()) - if _, ok := workflowApprovalCache.Load("org/repo@old2"); !ok { - t.Fatal("eviction ran again inside its window") - } - forgetWorkflowApproval("org/repo", "fresh") - if _, ok := workflowApprovalCache.Load("org/repo@fresh"); ok { + c.forgetWorkflowApproval("org/repo", "fresh") + if _, ok := c.workflowApprovals.probes["org/repo@fresh"]; ok { t.Fatal("forget left the entry") } - workflowApprovalCache.Delete("org/repo@old2") } diff --git a/src/pkg/github/workflow_auto_approval.go b/src/pkg/github/workflow_auto_approval.go new file mode 100644 index 0000000000..29bb939f42 --- /dev/null +++ b/src/pkg/github/workflow_auto_approval.go @@ -0,0 +1,177 @@ +package github + +import ( + "context" + "errors" + "fmt" + "net/http" + "strings" + + gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/writeback" +) + +// workflowApprovalTrusted is deliberately per run: one trusted triggering +// actor must not authorize other actors' runs on the same PR head. +func workflowApprovalTrusted(author, actor, app string, admitted bool, actors []string) bool { + if admitted { + return true + } + for _, login := range []string{author, actor} { + login = strings.TrimSpace(login) + if login == "" { + continue + } + if app != "" && strings.EqualFold(login, app) { + return true + } + for _, trusted := range actors { + if strings.EqualFold(login, strings.TrimSpace(trusted)) { + return true + } + } + } + return false +} + +// SweepWorkflowApprovals runs after CI enrichment. It observes the workflow +// approval gate and approves trusted runs at most once per PR head in this +// client. Permission refusals remain display evidence without repeated writes. +func (c *Client) SweepWorkflowApprovals(ctx context.Context, prs []PullRequest, policy config.GovernorCIConfig) { + if c == nil || c.client == nil { + return + } + for i := range prs { + pr := &prs[i] + if pr.Draft || pr.HeadSHA == "" || pr.Number <= 0 || pr.CIStatus == "success" || (pr.State != "" && pr.State != "open") { + continue + } + owner, name := c.splitRepo(pr.Repo) + repo := owner + "/" + name + if c.workflowApprovalHeld(pr.Labels) || c.RepoIsPaused(repo) { + continue + } + if pr.CIActivity != nil && strings.EqualFold(pr.CIActivity.Head, pr.HeadSHA) { + activity := *pr.CIActivity + activity.WorkflowApproval = nil + pr.CIActivity = &activity + } + probe := c.workflowRunsAwaitingApproval(ctx, repo, pr.HeadSHA) + if probe == nil { + continue + } + wa := *probe // never mutate a cached probe or an older dashboard snapshot + if pr.CIActivity == nil { + pr.CIActivity = &PRCIActivity{Head: pr.HeadSHA, Source: "github-workflow-runs"} + } + if !strings.EqualFold(pr.CIActivity.Head, pr.HeadSHA) { + continue + } + pr.CIActivity.WorkflowApproval = &wa + key := workflowApprovalKey{repo: repo, number: pr.Number, head: pr.HeadSHA} + c.workflowApprovals.mu.Lock() + previous := c.workflowApprovals.attempts[key] + c.workflowApprovals.mu.Unlock() + wa.PermissionDenied = previous.permissionDenied + if previous.attempted || !policy.AutoApproveWorkflowsEnabled() { + continue + } + // Enumeration can be cached. Re-read the head, author and admission + // before trusting them to execute code with the repository's CI rights. + fresh, _, err := c.client.PullRequests.Get(WithRESTCaller(ctx, "hive:workflow_approval"), owner, name, pr.Number) + if err != nil || fresh == nil || fresh.GetState() != "open" || fresh.GetDraft() || fresh.GetHead().GetSHA() != pr.HeadSHA { + continue + } + labels := labelNames(fresh.Labels) + if c.workflowApprovalHeld(labels) { + continue + } + admitted := false + for _, label := range labels { + admitted = admitted || strings.EqualFold(label, c.AutoMergeLabel()) + } + var trusted, denied []*gh.WorkflowRun + headRepo := fresh.GetHead().GetRepo().GetFullName() + for _, run := range wa.runs { + if headRepo == "" || !strings.EqualFold(run.GetHeadRepository().GetFullName(), headRepo) { + continue + } + if len(run.PullRequests) > 0 { + matches := false + for _, linked := range run.PullRequests { + matches = matches || linked.GetNumber() == pr.Number + } + if !matches { + continue + } + } + if workflowApprovalTrusted(fresh.GetUser().GetLogin(), workflowTriggeringActor(run), c.AppBotLogin(), admitted, policy.WorkflowApprovalActors()) { + trusted = append(trusted, run) + } else { + denied = append(denied, run) + } + } + c.workflowApprovals.mu.Lock() + if c.workflowApprovals.attempts == nil { + c.workflowApprovals.attempts = map[workflowApprovalKey]workflowApprovalAttempt{} + } + attempt := c.workflowApprovals.attempts[key] + recordDenied := !attempt.denied + attempt.denied = attempt.denied || len(denied) > 0 + c.workflowApprovals.attempts[key] = attempt + c.workflowApprovals.mu.Unlock() + if recordDenied { + for _, run := range denied { + c.workflowApprovals.record(key, run.GetID(), "denied") + } + } + item := c.WritebackItemForPR(*pr) + item.Author, item.Labels = fresh.GetUser().GetLogin(), labels + if len(trusted) == 0 || !c.WritebackAllowed(repo, item, writeback.KindCIRerun, "approve gated workflow runs").Allowed { + continue + } + // Claim before the first POST, including partial failures. A concurrent + // cycle and an unchanged head must not repeat the approval pass. + c.workflowApprovals.mu.Lock() + attempt = c.workflowApprovals.attempts[key] + claimed := !attempt.attempted + attempt.attempted = true + c.workflowApprovals.attempts[key] = attempt + c.workflowApprovals.mu.Unlock() + if !claimed { + continue + } + for _, run := range trusted { + if err := c.approveGatedWorkflowRun(ctx, owner, name, run.GetID()); err != nil { + var apiErr *gh.ErrorResponse + if errors.As(err, &apiErr) && apiErr.Response != nil && (apiErr.Response.StatusCode == http.StatusForbidden || apiErr.Response.StatusCode == http.StatusNotFound) { + c.workflowApprovals.record(key, run.GetID(), "permission_error") + wa.PermissionDenied = true + c.logger.Warn("workflow approval refused; needs workflow approval on GitHub", "repo", repo, "pr", pr.Number, "head", pr.HeadSHA, "run_id", run.GetID(), "error", err) + break + } + c.logger.Warn("failed to approve gated workflow run", "repo", repo, "pr", pr.Number, "run_id", run.GetID(), "error", err) + continue + } + c.workflowApprovals.record(key, run.GetID(), "approved") + c.logger.Info("approved gated workflow run", "repo", repo, "pr", pr.Number, "head", pr.HeadSHA, "run_id", run.GetID()) + c.forgetWorkflowApproval(repo, pr.HeadSHA) + } + } +} + +func (c *Client) workflowApprovalHeld(labels []string) bool { + return c.isHeld(labels) || c.isExempt(labels) || c.sentinelBlockedLabel(labels) != "" +} + +// go-github v72 does not bind POST /actions/runs/{id}/approve. +func (c *Client) approveGatedWorkflowRun(ctx context.Context, owner, repo string, runID int64) error { + req, err := c.client.NewRequest(http.MethodPost, fmt.Sprintf("repos/%s/%s/actions/runs/%d/approve", owner, repo, runID), nil) + if err != nil { + return err + } + resp, err := c.client.Do(WithRESTCaller(ctx, "hive:workflow_approval"), req, nil) + c.noteWritebackOutcome(owner+"/"+repo, resp, err) + return err +} diff --git a/src/pkg/github/workflow_auto_approval_test.go b/src/pkg/github/workflow_auto_approval_test.go new file mode 100644 index 0000000000..7458223fc8 --- /dev/null +++ b/src/pkg/github/workflow_auto_approval_test.go @@ -0,0 +1,337 @@ +package github + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "reflect" + "strings" + "sync" + "testing" + "time" + + gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/writeback" +) + +func TestWorkflowApprovalTrusted(t *testing.T) { + defaults := (config.GovernorCIConfig{}).WorkflowApprovalActors() + for _, tc := range []struct { + name, author, actor, app string + admitted bool + actors []string + want bool + }{ + {name: "dependabot author", author: "dependabot[bot]", actors: defaults, want: true}, + {name: "renovate trigger", author: "outsider", actor: "renovate[bot]", actors: defaults, want: true}, + {name: "app author", author: "hive[bot]", app: "hive[bot]", want: true}, + {name: "app trigger", author: "outsider", actor: "hive[bot]", app: "hive[bot]", want: true}, + {name: "owner admission", author: "outsider", admitted: true, want: true}, + {name: "case and whitespace", actor: "CUSTOM[bot]", actors: []string{" custom[bot] "}, want: true}, + {name: "unknown human", author: "outsider", actors: defaults}, + {name: "other app", author: "other[bot]", app: "hive[bot]", actors: defaults}, + {name: "empty identity", actors: []string{""}}, + {name: "empty allowlist", author: "dependabot[bot]", actors: []string{}}, + } { + t.Run(tc.name, func(t *testing.T) { + if got := workflowApprovalTrusted(tc.author, tc.actor, tc.app, tc.admitted, tc.actors); got != tc.want { + t.Fatalf("trusted = %v, want %v", got, tc.want) + } + }) + } +} + +type workflowApprovalFixture struct { + mu sync.Mutex + fresh *gh.PullRequest + runs []*gh.WorkflowRun + secondPage []*gh.WorkflowRun + posts []string + probes int + freshReads int + approvalStatus int + listStatus int +} + +func gatedRun(id int64, actor string) *gh.WorkflowRun { + return &gh.WorkflowRun{ + ID: gh.Ptr(id), HeadSHA: gh.Ptr("head1"), Event: gh.Ptr("pull_request"), + Status: gh.Ptr("completed"), Conclusion: gh.Ptr(WorkflowApprovalStatus), + TriggeringActor: &gh.User{Login: gh.Ptr(actor)}, + HeadRepository: &gh.Repository{FullName: gh.Ptr("fork/repo")}, + } +} + +func newWorkflowApprovalFixture(t *testing.T) (*Client, *workflowApprovalFixture, []PullRequest) { + t.Helper() + f := &workflowApprovalFixture{ + fresh: &gh.PullRequest{State: gh.Ptr("open"), Number: gh.Ptr(7), User: &gh.User{Login: gh.Ptr("dependabot[bot]")}, + Head: &gh.PullRequestBranch{SHA: gh.Ptr("head1"), Repo: &gh.Repository{FullName: gh.Ptr("fork/repo")}}}, + runs: []*gh.WorkflowRun{gatedRun(11, "outsider"), gatedRun(12, "outsider")}, + } + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + f.mu.Lock() + defer f.mu.Unlock() + switch { + case r.Method == http.MethodGet && r.URL.Path == "/repos/org/repo/actions/runs": + f.probes++ + if r.URL.Query().Get("head_sha") == "" || r.URL.Query().Get("event") != "pull_request" { + t.Errorf("unscoped probe: %s", r.URL) + } + if f.listStatus != 0 { + http.Error(w, `{"message":"unavailable"}`, f.listStatus) + return + } + if r.URL.Query().Get("page") == "2" { + _ = json.NewEncoder(w).Encode(map[string]any{"workflow_runs": f.secondPage}) + return + } + if len(f.secondPage) > 0 { + w.Header().Set("Link", `; rel="next"`) + } + _ = json.NewEncoder(w).Encode(map[string]any{"workflow_runs": f.runs}) + case r.Method == http.MethodGet && r.URL.Path == "/repos/org/repo/pulls/7": + f.freshReads++ + _ = json.NewEncoder(w).Encode(f.fresh) + case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/approve"): + f.posts = append(f.posts, r.URL.Path) + if f.approvalStatus != 0 { + http.Error(w, `{"message":"Resource not accessible by integration"}`, f.approvalStatus) + return + } + w.WriteHeader(http.StatusNoContent) + default: + t.Errorf("unexpected request: %s %s", r.Method, r.URL) + http.NotFound(w, r) + } + })) + t.Cleanup(srv.Close) + c := newTestClient(t, srv, "org", []string{"repo"}) + return c, f, []PullRequest{{Repo: "org/repo", Number: 7, Author: "dependabot[bot]", HeadSHA: "head1", CIStatus: "pending"}} +} + +func TestSweepWorkflowApprovalsOncePerHeadAndInvalidatesProbe(t *testing.T) { + c, f, prs := newWorkflowApprovalFixture(t) + ctx := context.Background() + c.SweepWorkflowApprovals(ctx, prs, config.GovernorCIConfig{}) + if len(f.posts) != 2 || f.probes != 1 { + t.Fatalf("posts=%v probes=%d", f.posts, f.probes) + } + stats := c.WorkflowApprovalsStatus() + if stats.Approved != 2 || stats.Denied != 0 || stats.Last == nil || stats.Last.RunID != 12 || stats.Last.Head != "head1" { + t.Fatalf("stats=%+v", stats) + } + // A successful POST discards the negative probe immediately, and a + // still-held result must not cause another POST for the same head. + c.SweepWorkflowApprovals(ctx, prs, config.GovernorCIConfig{}) + if len(f.posts) != 2 || f.probes != 2 || f.freshReads != 1 { + t.Fatalf("second pass: posts=%v probes=%d fresh=%d", f.posts, f.probes, f.freshReads) + } + prs[0].HeadSHA, prs[0].CIActivity = "head2", nil + f.fresh.Head.SHA = gh.Ptr("head2") + f.runs = []*gh.WorkflowRun{gatedRun(21, "outsider")} + f.runs[0].HeadSHA = gh.Ptr("head2") + c.SweepWorkflowApprovals(ctx, prs, config.GovernorCIConfig{}) + if len(f.posts) != 3 || c.WorkflowApprovalsStatus().Approved != 3 { + t.Fatalf("new head did not re-arm: posts=%v", f.posts) + } +} + +func TestSweepWorkflowApprovalsPagination(t *testing.T) { + c, f, prs := newWorkflowApprovalFixture(t) + f.secondPage = []*gh.WorkflowRun{gatedRun(13, "outsider")} + c.SweepWorkflowApprovals(context.Background(), prs, config.GovernorCIConfig{}) + if len(f.posts) != 3 || f.probes != 2 { + t.Fatalf("later gated runs omitted: posts=%v probes=%d", f.posts, f.probes) + } +} + +func TestSweepWorkflowApprovalsConcurrentPasses(t *testing.T) { + c, f, _ := newWorkflowApprovalFixture(t) + var wg sync.WaitGroup + for range 8 { + wg.Add(1) + go func() { + defer wg.Done() + c.SweepWorkflowApprovals(context.Background(), []PullRequest{{Repo: "org/repo", Number: 7, HeadSHA: "head1", CIStatus: "pending"}}, config.GovernorCIConfig{}) + }() + } + wg.Wait() + if len(f.posts) != 2 || c.WorkflowApprovalsStatus().Approved != 2 { + t.Fatalf("concurrent passes repeated approvals: posts=%v stats=%+v", f.posts, c.WorkflowApprovalsStatus()) + } +} + +func TestWorkflowApprovalSurvivesCheckEvidenceOverlay(t *testing.T) { + t.Cleanup(ResetCIEvidenceForTest(nil)) + c, f, prs := newWorkflowApprovalFixture(t) + f.approvalStatus = http.StatusForbidden + now := time.Now().UTC() + prs[0].CIActivity = &PRCIActivity{Head: "head1", Source: "github-check-runs", ObservedAt: &now} + sharedCIEvidence().recordListed("org/repo", "head1", prs[0].CIActivity) + c.SweepWorkflowApprovals(context.Background(), prs, config.GovernorCIConfig{}) + got := HeadCIActivity("org/repo", "head1", prs[0].CIActivity) + if got.WorkflowApproval == nil || !got.WorkflowApproval.PermissionDenied { + t.Fatalf("check evidence discarded the later workflow refusal: %+v", got) + } + cleared := *prs[0].CIActivity + cleared.WorkflowApproval = nil + if got := HeadCIActivity("org/repo", "head1", &cleared); got.WorkflowApproval != nil { + t.Fatal("check overlay revived an old approval gate") + } +} + +func TestSweepWorkflowApprovalsObservesApprovalWithoutMutatingOldSnapshot(t *testing.T) { + c, f, prs := newWorkflowApprovalFixture(t) + c.SweepWorkflowApprovals(context.Background(), prs, config.GovernorCIConfig{}) + old := prs[0].CIActivity + f.runs = nil // the approval started the runs; they are no longer gated + c.SweepWorkflowApprovals(context.Background(), prs, config.GovernorCIConfig{}) + if prs[0].CIActivity.WorkflowApproval != nil || old.WorkflowApproval == nil { + t.Fatal("stale gate persisted or an older dashboard snapshot was mutated") + } +} + +func TestSweepWorkflowApprovalsDoesNotRetryOtherAPIErrors(t *testing.T) { + c, f, prs := newWorkflowApprovalFixture(t) + f.approvalStatus = http.StatusInternalServerError + for range 2 { + c.SweepWorkflowApprovals(context.Background(), prs, config.GovernorCIConfig{}) + } + if len(f.posts) != 2 || c.WorkflowApprovalsStatus().PermissionErrors != 0 { + t.Fatalf("unexpected retry or permission verdict: posts=%v stats=%+v", f.posts, c.WorkflowApprovalsStatus()) + } +} + +func TestSweepWorkflowApprovalsPermissionRefusalOnce(t *testing.T) { + for _, code := range []int{http.StatusForbidden, http.StatusNotFound} { + t.Run(http.StatusText(code), func(t *testing.T) { + c, f, prs := newWorkflowApprovalFixture(t) + f.approvalStatus = code + c.SweepWorkflowApprovals(context.Background(), prs, config.GovernorCIConfig{}) + if !prs[0].CIActivity.WorkflowApproval.PermissionDenied { + t.Fatal("missing manual-approval evidence") + } + // CI enrichment creates a new snapshot each cycle. The refusal + // must survive without another POST or even another probe. + prs[0].CIActivity = nil + c.SweepWorkflowApprovals(context.Background(), prs, config.GovernorCIConfig{}) + stats := c.WorkflowApprovalsStatus() + if len(f.posts) != 1 || f.probes != 1 || stats.PermissionErrors != 1 || stats.Approved != 0 || !prs[0].CIActivity.WorkflowApproval.PermissionDenied { + t.Fatalf("posts=%v probes=%d stats=%+v activity=%+v", f.posts, f.probes, stats, prs[0].CIActivity) + } + }) + } +} + +func TestSweepWorkflowApprovalsTrustIsPerRun(t *testing.T) { + c, f, prs := newWorkflowApprovalFixture(t) + f.fresh.User.Login = gh.Ptr("outsider") + f.runs[0].TriggeringActor.Login = gh.Ptr("renovate[bot]") + c.SweepWorkflowApprovals(context.Background(), prs, config.GovernorCIConfig{}) + want := []string{"/repos/org/repo/actions/runs/11/approve"} + if !reflect.DeepEqual(f.posts, want) || c.WorkflowApprovalsStatus().Denied != 1 { + t.Fatalf("one actor authorized another's run: posts=%v stats=%+v", f.posts, c.WorkflowApprovalsStatus()) + } +} + +func TestSweepWorkflowApprovalsDeniedCanBeAdmittedLater(t *testing.T) { + c, f, prs := newWorkflowApprovalFixture(t) + f.fresh.User.Login = gh.Ptr("outsider") + for range 2 { + c.SweepWorkflowApprovals(context.Background(), prs, config.GovernorCIConfig{}) + } + if len(f.posts) != 0 || c.WorkflowApprovalsStatus().Denied != 2 || f.probes != 1 { + t.Fatalf("untrusted: posts=%v stats=%+v probes=%d", f.posts, c.WorkflowApprovalsStatus(), f.probes) + } + f.fresh.Labels = []*gh.Label{{Name: gh.Ptr("lgtm")}} + c.SweepWorkflowApprovals(context.Background(), prs, config.GovernorCIConfig{}) + if len(f.posts) != 2 { + t.Fatalf("owner admission did not release head: %v", f.posts) + } +} + +func TestSweepWorkflowApprovalsGuards(t *testing.T) { + for _, tc := range []struct { + name string + setup func(*Client, *workflowApprovalFixture, []PullRequest, *config.GovernorCIConfig) + }{ + {"disabled", func(_ *Client, _ *workflowApprovalFixture, _ []PullRequest, p *config.GovernorCIConfig) { + p.AutoApproveWorkflows = gh.Ptr(false) + }}, + {"empty actors", func(_ *Client, _ *workflowApprovalFixture, _ []PullRequest, p *config.GovernorCIConfig) { + p.AutoApproveWorkflowActors = gh.Ptr([]string{}) + }}, + {"draft", func(_ *Client, _ *workflowApprovalFixture, p []PullRequest, _ *config.GovernorCIConfig) { + p[0].Draft = true + }}, + {"held", func(_ *Client, _ *workflowApprovalFixture, p []PullRequest, _ *config.GovernorCIConfig) { + p[0].Labels = []string{"hold/review"} + }}, + {"do not merge", func(_ *Client, _ *workflowApprovalFixture, p []PullRequest, _ *config.GovernorCIConfig) { + p[0].Labels = []string{"do-not-merge"} + }}, + {"sentinel", func(c *Client, _ *workflowApprovalFixture, p []PullRequest, _ *config.GovernorCIConfig) { + c.SetSentinelAlertLabel("alert") + p[0].Labels = []string{"alert"} + }}, + {"paused", func(c *Client, _ *workflowApprovalFixture, _ []PullRequest, _ *config.GovernorCIConfig) { + c.SetRepoPausedFunc(func(string) bool { return true }) + }}, + {"fresh hold", func(_ *Client, f *workflowApprovalFixture, _ []PullRequest, _ *config.GovernorCIConfig) { + f.fresh.Labels = []*gh.Label{{Name: gh.Ptr("hold")}} + }}, + {"fresh draft", func(_ *Client, f *workflowApprovalFixture, _ []PullRequest, _ *config.GovernorCIConfig) { + f.fresh.Draft = gh.Ptr(true) + }}, + {"fresh closed", func(_ *Client, f *workflowApprovalFixture, _ []PullRequest, _ *config.GovernorCIConfig) { + f.fresh.State = gh.Ptr("closed") + }}, + {"head changed", func(_ *Client, f *workflowApprovalFixture, _ []PullRequest, _ *config.GovernorCIConfig) { + f.fresh.Head.SHA = gh.Ptr("newhead") + }}, + {"wrong run head", func(_ *Client, f *workflowApprovalFixture, _ []PullRequest, _ *config.GovernorCIConfig) { + for _, r := range f.runs { + r.HeadSHA = gh.Ptr("oldhead") + } + }}, + {"wrong head repository", func(_ *Client, f *workflowApprovalFixture, _ []PullRequest, _ *config.GovernorCIConfig) { + for _, r := range f.runs { + r.HeadRepository.FullName = gh.Ptr("other/repo") + } + }}, + {"wrong PR", func(_ *Client, f *workflowApprovalFixture, _ []PullRequest, _ *config.GovernorCIConfig) { + for _, r := range f.runs { + r.PullRequests = []*gh.PullRequest{{Number: gh.Ptr(8)}} + } + }}, + {"writeback disabled", func(c *Client, _ *workflowApprovalFixture, _ []PullRequest, _ *config.GovernorCIConfig) { + c.SetWritebackGate(writeback.New(writeback.Config{PerRepo: map[string]writeback.RepoOverride{"org/repo": {Mode: writeback.RepoModeOff}}}, writeback.Deps{}, "")) + }}, + {"different event", func(_ *Client, f *workflowApprovalFixture, _ []PullRequest, _ *config.GovernorCIConfig) { + for _, r := range f.runs { + r.Event = gh.Ptr("push") + } + }}, + {"no gated runs", func(_ *Client, f *workflowApprovalFixture, _ []PullRequest, _ *config.GovernorCIConfig) { + for _, r := range f.runs { + r.Conclusion = gh.Ptr("success") + } + }}, + {"probe error", func(_ *Client, f *workflowApprovalFixture, _ []PullRequest, _ *config.GovernorCIConfig) { + f.listStatus = http.StatusServiceUnavailable + }}, + } { + t.Run(tc.name, func(t *testing.T) { + c, f, prs := newWorkflowApprovalFixture(t) + policy := config.GovernorCIConfig{} + tc.setup(c, f, prs, &policy) + c.SweepWorkflowApprovals(context.Background(), prs, policy) + if len(f.posts) != 0 { + t.Fatalf("guard allowed approvals: %v", f.posts) + } + }) + } +} From 3b5874ef30d61d04a79483aeb9cb38c66ef51edb Mon Sep 17 00:00:00 2001 From: thatguywob Date: Wed, 4 Nov 2026 16:23:10 +0000 Subject: [PATCH 2/8] =?UTF-8?q?=F0=9F=90=9B=20fix(ci):=20account=20for=20w?= =?UTF-8?q?orkflow=20approval=20config=20in=20parity=20fixtures?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: thatguywob --- src/pkg/config/testdata/agent_runtime_golden.json | 1 + src/pkg/dashboard/settings_parity_allowlist.txt | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/pkg/config/testdata/agent_runtime_golden.json b/src/pkg/config/testdata/agent_runtime_golden.json index 00fa498043..80962ac227 100644 --- a/src/pkg/config/testdata/agent_runtime_golden.json +++ b/src/pkg/config/testdata/agent_runtime_golden.json @@ -150,6 +150,7 @@ "pr_follow_up": {} }, "Governor": { + "ci": {}, "Modes": { "busy": { "Threshold": 16, diff --git a/src/pkg/dashboard/settings_parity_allowlist.txt b/src/pkg/dashboard/settings_parity_allowlist.txt index 499677130e..3bc01dbd59 100644 --- a/src/pkg/dashboard/settings_parity_allowlist.txt +++ b/src/pkg/dashboard/settings_parity_allowlist.txt @@ -218,6 +218,8 @@ governor.budget.coins..budget # TODO #11854 governor.budget.coins..label # TODO #11854 governor.budget.coins..tokens_per_coin # TODO #11854 governor.budget.coins..usd_per_coin # TODO #11854 +governor.ci.auto_approve_workflow_actors[] # trusted workflow-approval actors (#16603); yaml-only +governor.ci.auto_approve_workflows # gated workflow auto-approval flag (#16603); yaml-only governor.ci_failure_kick_cooldown_s # webhook CI-failure kick cooldown (#15291); yaml-only governor.ci_failure_webhook_kick # webhook CI-failure kick flag (#15291); yaml-only governor.claims.contributor_flap_threshold # contributor flap cool-off (#13579); yaml-only From 9868124df16beebf428db23d6d7224f911c8bbc5 Mon Sep 17 00:00:00 2001 From: thatguywob Date: Wed, 4 Nov 2026 16:45:46 +0000 Subject: [PATCH 3/8] =?UTF-8?q?=F0=9F=90=9B=20fix(dashboard):=20preserve?= =?UTF-8?q?=20workflow=20approval=20guidance=20after=20rebase?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: thatguywob --- src/pkg/dashboard/stage_tracker_activity.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pkg/dashboard/stage_tracker_activity.go b/src/pkg/dashboard/stage_tracker_activity.go index 9413846d4d..239d66cf13 100644 --- a/src/pkg/dashboard/stage_tracker_activity.go +++ b/src/pkg/dashboard/stage_tracker_activity.go @@ -78,7 +78,7 @@ func stageTrackerCIActivity(pr github.PullRequest, now time.Time) *StageTrackerA verb = "needs" } out.Caption = fmt.Sprintf("CI waiting · %s %s approval on GitHub", plural(wa.Pending, "workflow run"), verb) - out.Detail = "GitHub is holding workflow runs for this commit behind Approve and run workflows." + out.Detail = "GitHub is holding workflow runs for this commit until a maintainer can approve workflows on GitHub." if wa.PermissionDenied { out.Caption = "CI waiting · needs workflow approval on GitHub" out.Detail = "The App was refused workflow approval (actions: write is required). A maintainer must click Approve and run workflows on GitHub." From 95f9fdc0ae9501ee2fbccc5c24bd26366efe01fd Mon Sep 17 00:00:00 2001 From: thatguywob Date: Wed, 4 Nov 2026 16:59:32 +0000 Subject: [PATCH 4/8] =?UTF-8?q?=F0=9F=90=9B=20fix(dashboard):=20remove=20u?= =?UTF-8?q?nused=20workflow=20approval=20helper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: thatguywob --- src/pkg/dashboard/stage_tracker_activity.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/pkg/dashboard/stage_tracker_activity.go b/src/pkg/dashboard/stage_tracker_activity.go index 239d66cf13..d5a77f4bdb 100644 --- a/src/pkg/dashboard/stage_tracker_activity.go +++ b/src/pkg/dashboard/stage_tracker_activity.go @@ -253,13 +253,6 @@ func stageTrackerWorkflowApprovalBlocker(n int) string { return fmt.Sprintf("%s%d", stageTrackerWorkflowApprovalBlockerPrefix, n) } -func pluralVerb(n int, one, many string) string { - if n == 1 { - return one - } - return many -} - // stageTrackerCountHeadChecks fills the check counts from every check run // observed on the head when the required set is not known: one entry per // check name, red if any run of that name is red, green only if its latest From 94610bc8a15cea1a45b3d800b18559263e5daf86 Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Wed, 4 Nov 2026 19:50:00 -0500 Subject: [PATCH 5/8] =?UTF-8?q?=F0=9F=90=9B=20fix(dashboard):=20restore=20?= =?UTF-8?q?pluralVerb=20dropped=20by=20the=20v5=20merge?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The v5 base started using pluralVerb in stageTrackerUnreportedChecksSentence after this branch had removed it as unused; the merge kept the removal and build-go failed with 'undefined: pluralVerb'. Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- src/pkg/dashboard/stage_tracker_activity.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/pkg/dashboard/stage_tracker_activity.go b/src/pkg/dashboard/stage_tracker_activity.go index 4e011123b1..4e176128dc 100644 --- a/src/pkg/dashboard/stage_tracker_activity.go +++ b/src/pkg/dashboard/stage_tracker_activity.go @@ -319,6 +319,13 @@ func stageTrackerWorkflowApprovalBlocker(n int) string { return fmt.Sprintf("%s%d", stageTrackerWorkflowApprovalBlockerPrefix, n) } +func pluralVerb(n int, one, many string) string { + if n == 1 { + return one + } + return many +} + // stageTrackerCountHeadChecks fills the check counts from every check run // observed on the head when the required set is not known: one entry per // check name, red if any run of that name is red, green only if its latest From e06da93fb89dc351ca04870b157b4ee42377768f Mon Sep 17 00:00:00 2001 From: ci-maintainer <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Wed, 4 Nov 2026 20:13:28 -0500 Subject: [PATCH 6/8] =?UTF-8?q?=F0=9F=90=9B=20fix(ci):=20bound=20the=20wor?= =?UTF-8?q?kflow-approval=20ledger=20and=20keep=20refusal=20evidence?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review follow-ups for hivecommons/hive#16681: - The attempts ledger is now evicted: a sweep drops superseded heads of the same PR immediately and ages out entries no sweep has observed for 24h, instead of retaining every gated head for the process lifetime. - CI enrichment applies the ledger's permission-refusal flag itself, so budget-mode cycles that skip SweepWorkflowApprovals keep the "needs workflow approval on GitHub" caption instead of regressing to the count. - The workflowApprovalTrusted doc comment now states the #16603 rule: a trusted author authorizes every run on the head; otherwise trust is per triggering actor. Hive-Fix-Attempt: 1/6 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: ci-maintainer <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- src/pkg/github/client.go | 2 +- src/pkg/github/workflow_approval.go | 43 +++++++++++++++++++ src/pkg/github/workflow_auto_approval.go | 18 ++++---- src/pkg/github/workflow_auto_approval_test.go | 35 +++++++++++++++ 4 files changed, 88 insertions(+), 10 deletions(-) diff --git a/src/pkg/github/client.go b/src/pkg/github/client.go index 1def9a633f..0975dc79bd 100644 --- a/src/pkg/github/client.go +++ b/src/pkg/github/client.go @@ -2565,7 +2565,7 @@ func (c *Client) EnrichCIStatus(ctx context.Context, prs []PullRequest) { // approve-workflows gate (dependabot, forks); only a pending head // pays for the extra listing (hivecommons/hive#16599). if prs[i].CIStatus == "pending" && prs[i].CIActivity != nil { - prs[i].CIActivity.WorkflowApproval = c.workflowRunsAwaitingApproval(ctx, prs[i].Repo, prs[i].HeadSHA) + prs[i].CIActivity.WorkflowApproval = c.workflowApprovalForPR(ctx, prs[i].Repo, prs[i].Number, prs[i].HeadSHA) } if prs[i].CIStatus == "failure" { prs[i].SharedCIIncident = c.openSharedCIIncident(ctx, &prs[i], incidents) diff --git a/src/pkg/github/workflow_approval.go b/src/pkg/github/workflow_approval.go index 75cb0ba0a4..5814139398 100644 --- a/src/pkg/github/workflow_approval.go +++ b/src/pkg/github/workflow_approval.go @@ -39,8 +39,13 @@ type workflowApprovalAttempt struct { attempted bool denied bool permissionDenied bool + seen time.Time } +// workflowApprovalAttemptTTL bounds the attempts ledger: an entry no sweep +// has observed for this long belongs to a merged, closed or superseded head. +const workflowApprovalAttemptTTL = 24 * time.Hour + type workflowApprovalState struct { mu sync.Mutex probes map[string]workflowApprovalEntry @@ -98,6 +103,44 @@ func (s *workflowApprovalState) record(key workflowApprovalKey, runID int64, out s.status.Last = &WorkflowApprovalEvent{Repo: key.repo, Number: key.number, Head: key.head, RunID: runID, Outcome: outcome, At: time.Now().UTC()} } +// touchAttempt marks a gated head as live in the attempts ledger and returns +// its entry. Superseded heads of the same PR and entries no sweep has observed +// within workflowApprovalAttemptTTL are evicted. The caller holds s.mu. +func (s *workflowApprovalState) touchAttempt(key workflowApprovalKey, now time.Time) workflowApprovalAttempt { + if s.attempts == nil { + s.attempts = map[workflowApprovalKey]workflowApprovalAttempt{} + } + for other, entry := range s.attempts { + if other == key { + continue + } + if (other.repo == key.repo && other.number == key.number) || now.Sub(entry.seen) >= workflowApprovalAttemptTTL { + delete(s.attempts, other) + } + } + attempt := s.attempts[key] + attempt.seen = now + s.attempts[key] = attempt + return attempt +} + +// workflowApprovalForPR returns a copy of the head's approval probe with the +// attempts ledger applied, so a permission refusal keeps its caption on cycles +// that skip SweepWorkflowApprovals. Cached probes are never mutated. +func (c *Client) workflowApprovalForPR(ctx context.Context, repo string, number int, sha string) *PRWorkflowApproval { + probe := c.workflowRunsAwaitingApproval(ctx, repo, sha) + if probe == nil { + return nil + } + wa := *probe + owner, name := c.splitRepo(repo) + key := workflowApprovalKey{repo: owner + "/" + name, number: number, head: sha} + c.workflowApprovals.mu.Lock() + wa.PermissionDenied = c.workflowApprovals.attempts[key].permissionDenied + c.workflowApprovals.mu.Unlock() + return &wa +} + // workflowRunsAwaitingApproval memoizes the read for a short window. The // caller validates each run's head repository and SHA before any mutation. func (c *Client) workflowRunsAwaitingApproval(ctx context.Context, repo, sha string) *PRWorkflowApproval { diff --git a/src/pkg/github/workflow_auto_approval.go b/src/pkg/github/workflow_auto_approval.go index 29bb939f42..e7023f0e29 100644 --- a/src/pkg/github/workflow_auto_approval.go +++ b/src/pkg/github/workflow_auto_approval.go @@ -6,14 +6,18 @@ import ( "fmt" "net/http" "strings" + "time" gh "github.com/google/go-github/v72/github" "github.com/hivecommons/hive/pkg/config" "github.com/hivecommons/hive/pkg/writeback" ) -// workflowApprovalTrusted is deliberately per run: one trusted triggering -// actor must not authorize other actors' runs on the same PR head. +// workflowApprovalTrusted follows hivecommons/hive#16603: a trusted PR +// author authorizes every gated run on the head, because the code under +// test is theirs. Otherwise trust is per run — a trusted triggering actor +// (or the App) authorizes only the runs it triggered, and an admitted PR +// authorizes all of them. func workflowApprovalTrusted(author, actor, app string, admitted bool, actors []string) bool { if admitted { return true @@ -57,11 +61,11 @@ func (c *Client) SweepWorkflowApprovals(ctx context.Context, prs []PullRequest, activity.WorkflowApproval = nil pr.CIActivity = &activity } - probe := c.workflowRunsAwaitingApproval(ctx, repo, pr.HeadSHA) + probe := c.workflowApprovalForPR(ctx, repo, pr.Number, pr.HeadSHA) if probe == nil { continue } - wa := *probe // never mutate a cached probe or an older dashboard snapshot + wa := *probe if pr.CIActivity == nil { pr.CIActivity = &PRCIActivity{Head: pr.HeadSHA, Source: "github-workflow-runs"} } @@ -71,9 +75,8 @@ func (c *Client) SweepWorkflowApprovals(ctx context.Context, prs []PullRequest, pr.CIActivity.WorkflowApproval = &wa key := workflowApprovalKey{repo: repo, number: pr.Number, head: pr.HeadSHA} c.workflowApprovals.mu.Lock() - previous := c.workflowApprovals.attempts[key] + previous := c.workflowApprovals.touchAttempt(key, time.Now()) c.workflowApprovals.mu.Unlock() - wa.PermissionDenied = previous.permissionDenied if previous.attempted || !policy.AutoApproveWorkflowsEnabled() { continue } @@ -113,9 +116,6 @@ func (c *Client) SweepWorkflowApprovals(ctx context.Context, prs []PullRequest, } } c.workflowApprovals.mu.Lock() - if c.workflowApprovals.attempts == nil { - c.workflowApprovals.attempts = map[workflowApprovalKey]workflowApprovalAttempt{} - } attempt := c.workflowApprovals.attempts[key] recordDenied := !attempt.denied attempt.denied = attempt.denied || len(denied) > 0 diff --git a/src/pkg/github/workflow_auto_approval_test.go b/src/pkg/github/workflow_auto_approval_test.go index 7458223fc8..957272c0a4 100644 --- a/src/pkg/github/workflow_auto_approval_test.go +++ b/src/pkg/github/workflow_auto_approval_test.go @@ -335,3 +335,38 @@ func TestSweepWorkflowApprovalsGuards(t *testing.T) { }) } } + +func TestWorkflowApprovalForPRCarriesRefusalWithoutSweep(t *testing.T) { + c, f, prs := newWorkflowApprovalFixture(t) + f.approvalStatus = http.StatusForbidden + c.SweepWorkflowApprovals(context.Background(), prs, config.GovernorCIConfig{}) + // Budget mode skips the sweep; CI enrichment alone must still surface + // the manual-approval caption from the ledger, without a new POST. + got := c.workflowApprovalForPR(context.Background(), "org/repo", 7, "head1") + if got == nil || !got.PermissionDenied || len(f.posts) != 1 { + t.Fatalf("refusal dropped outside the sweep: got=%+v posts=%v", got, f.posts) + } + if cached := c.workflowApprovalForPR(context.Background(), "org/repo", 8, "head1"); cached == nil || cached.PermissionDenied { + t.Fatalf("ledger leaked across PRs or cached probe was mutated: %+v", cached) + } +} + +func TestWorkflowApprovalAttemptsLedgerEviction(t *testing.T) { + s := &workflowApprovalState{} + now := time.Now() + old := workflowApprovalKey{repo: "org/repo", number: 7, head: "head1"} + other := workflowApprovalKey{repo: "org/repo", number: 9, head: "head9"} + s.touchAttempt(old, now) + s.attempts[old] = workflowApprovalAttempt{attempted: true, seen: now} + s.touchAttempt(other, now) + // A new head on the same PR supersedes the old one immediately. + s.touchAttempt(workflowApprovalKey{repo: "org/repo", number: 7, head: "head2"}, now) + if _, ok := s.attempts[old]; ok || len(s.attempts) != 2 { + t.Fatalf("superseded head retained: %+v", s.attempts) + } + // Live heads are refreshed each sweep; the rest age out after the TTL. + s.touchAttempt(other, now.Add(workflowApprovalAttemptTTL)) + if _, ok := s.attempts[other]; !ok || len(s.attempts) != 1 { + t.Fatalf("ttl eviction wrong: %+v", s.attempts) + } +} From c4b15fc1c3359080848fd5ef217dcea652710bc1 Mon Sep 17 00:00:00 2001 From: ci-maintainer <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Wed, 4 Nov 2026 20:29:51 -0500 Subject: [PATCH 7/8] =?UTF-8?q?=F0=9F=93=9D=20docs(ci):=20document=20gover?= =?UTF-8?q?nor.ci=20workflow-approval=20keys=20in=20operator=20reference?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review finding (docs-currency): the new governor.ci.auto_approve_workflows and governor.ci.auto_approve_workflow_actors keys were only in hive.yaml.example and the settings parity allowlist. Add them beside the sibling governor.ci_failure_webhook_kick entries in docs/operator-reference/runtime-and-quota.md (behaviour, trust set, guards, default-on note, status counts, log lines) and the configuration table in docs/webhooks.md. Hive-Fix-Attempt: 2/6 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: ci-maintainer <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- .../operator-reference/runtime-and-quota.md | 24 +++++++++++++++++++ src/docs/webhooks.md | 2 ++ 2 files changed, 26 insertions(+) diff --git a/src/docs/operator-reference/runtime-and-quota.md b/src/docs/operator-reference/runtime-and-quota.md index b2f0a62153..6274fc8436 100644 --- a/src/docs/operator-reference/runtime-and-quota.md +++ b/src/docs/operator-reference/runtime-and-quota.md @@ -262,6 +262,30 @@ v5 and v6 spokes) tracking one repo all stay on webhook mode. records `ci_failure_kick` — "CI failed on #N (check) — kicked agent X" / "nudged contributor lane". **Agents never watch or poll CI themselves**: detection is out-of-band via this webhook path, or the sweep as fallback. +- **Gated workflow runs are approved for trusted heads.** After each CI + enrichment pass the sweep looks for open, non-draft PR heads whose + workflow runs sit behind GitHub's "Approve and run workflows" gate + (`action_required`) and approves them **at most once per PR head** + ([#16603](https://github.com/hivecommons/hive/issues/16603)). A head is + trusted when the PR is admitted by the owner (carries the automerge + label), when its author or the run's triggering actor is this hive's App, + or when that login is in `governor.ci.auto_approve_workflow_actors` + (default `['dependabot[bot]', 'renovate[bot]']`; an explicit `[]` trusts + only the App and owner admission). Guards: `governor.ci.auto_approve_workflows` + (default `true` — **unset means on**; `false` keeps the gate read-only and + still surfaces `waiting on workflow approval`), the repo pause, hold / + exempt / sentinel labels, a fresh `GET /pulls/N` re-read (head SHA, open, + non-draft, author, labels) before any write, a head-repository match on + every run, and the writeback gate (`ci_rerun`). Approval starts CI only; + the review, hold and merge gates are unchanged. A `403`/`404` from + `POST /actions/runs/{id}/approve` is recorded once as a permission error + and the Activity Monitor shows the manual-action caption instead of + retrying. Counts are published at `/api/status.workflow_approvals` + (`approved`, `denied`, `permission_errors`, `last`), and the spoke logs + `approved gated workflow run` / `workflow approval refused; needs workflow + approval on GitHub`. Because approved runs execute the head's workflow + files on the repository's runners, operators on self-hosted runners who + want an explicit opt-in should set `governor.ci.auto_approve_workflows: false`. - **A recorded review verdict also wakes the governor loop.** When the relay records a verdict bound to a review dispatch, the head that wrote it is free and the PR may be ready for its next head or its decision kick, so diff --git a/src/docs/webhooks.md b/src/docs/webhooks.md index d46de916a5..9b05667a64 100644 --- a/src/docs/webhooks.md +++ b/src/docs/webhooks.md @@ -115,6 +115,8 @@ These are the acceptance criteria of #13373 and the tests that hold them. | `governor.webhook_wake_min_gap_s` | `30` | minimum spacing between webhook-triggered cycles, and between the cycles recorded review verdicts trigger | | `governor.ci_failure_webhook_kick` | `true` | a failed `check_run`/`check_suite`/`status` delivery kicks the owning agent (hive-authored PR) or runs a targeted contributor-lane pass (contributor PR) at once; `false` records the signal for the sweep only | | `governor.ci_failure_kick_cooldown_s` | `600` | at most one webhook-triggered CI-failure kick or nudge per PR head in this window | +| `governor.ci.auto_approve_workflows` | `true` (unset = on) | after CI enrichment, approve GitHub-gated (`action_required`) workflow runs once per trusted PR head; `false` keeps the gate read-only (`waiting on workflow approval` still shows). Starts CI only — review, hold and merge gates are unchanged. See [runtime-and-quota.md](operator-reference/runtime-and-quota.md) | +| `governor.ci.auto_approve_workflow_actors` | `['dependabot[bot]', 'renovate[bot]']` | PR authors / triggering actors trusted to have gated runs approved, in addition to this hive's App and owner-admitted (automerge-labelled) PRs; an explicit `[]` trusts only the App and owner admission | | `forgestate.reconcile_interval_s` | `900` | ForgeState's own full-list interval per repo, shortened while drift is high | | `forgestate.freshness_bound_s` | `60` | maximum item age before merge/close/claim/kick re-reads it | | `forgestate.drift_threshold` | `0.05` | fraction of items a reconcile may find wrong before the interval shortens | From 746fa2b02df05be3c524cb3ffc0b1b12aca7c654 Mon Sep 17 00:00:00 2001 From: ci-maintainer <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Wed, 4 Nov 2026 20:49:21 -0500 Subject: [PATCH 8/8] =?UTF-8?q?=F0=9F=93=9D=20docs(ci):=20state=20when=20t?= =?UTF-8?q?he=20workflow-approval=20sweep=20is=20shed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit runtime-and-quota.md and webhooks.md said the approval sweep runs after every CI enrichment pass. main_eval_cycle.go only calls SweepWorkflowApprovals when the eval budget decision does not shed optional steps, so it is skipped in conserve/critical API-budget mode, on off-cadence cycles and on cached (webhook-wake) cycles. Say so, and note that EnrichCIStatus still runs on those cycles so the `waiting on workflow approval` state stays visible until the next full normal-mode cycle approves the head. Hive-Fix-Attempt: 3/6 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: ci-maintainer <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- src/docs/operator-reference/runtime-and-quota.md | 10 ++++++++-- src/docs/webhooks.md | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/docs/operator-reference/runtime-and-quota.md b/src/docs/operator-reference/runtime-and-quota.md index 6274fc8436..8499e07181 100644 --- a/src/docs/operator-reference/runtime-and-quota.md +++ b/src/docs/operator-reference/runtime-and-quota.md @@ -262,8 +262,9 @@ v5 and v6 spokes) tracking one repo all stay on webhook mode. records `ci_failure_kick` — "CI failed on #N (check) — kicked agent X" / "nudged contributor lane". **Agents never watch or poll CI themselves**: detection is out-of-band via this webhook path, or the sweep as fallback. -- **Gated workflow runs are approved for trusted heads.** After each CI - enrichment pass the sweep looks for open, non-draft PR heads whose +- **Gated workflow runs are approved for trusted heads.** After the CI + enrichment pass of a full (non-cached) cycle in `normal` API-budget + mode, the sweep looks for open, non-draft PR heads whose workflow runs sit behind GitHub's "Approve and run workflows" gate (`action_required`) and approves them **at most once per PR head** ([#16603](https://github.com/hivecommons/hive/issues/16603)). A head is @@ -286,6 +287,11 @@ v5 and v6 spokes) tracking one repo all stay on webhook mode. approval on GitHub`. Because approved runs execute the head's workflow files on the repository's runners, operators on self-hosted runners who want an explicit opt-in should set `governor.ci.auto_approve_workflows: false`. + The sweep is an optional enrichment step: it is shed in `conserve` / + `critical` mode, on off-cadence cycles and on cached (webhook-wake) + cycles, so a gated head waits for the next full `normal`-mode cycle. + `EnrichCIStatus` still runs on those cycles, so the + `waiting on workflow approval` state remains visible meanwhile. - **A recorded review verdict also wakes the governor loop.** When the relay records a verdict bound to a review dispatch, the head that wrote it is free and the PR may be ready for its next head or its decision kick, so diff --git a/src/docs/webhooks.md b/src/docs/webhooks.md index 9b05667a64..6100edf86f 100644 --- a/src/docs/webhooks.md +++ b/src/docs/webhooks.md @@ -115,7 +115,7 @@ These are the acceptance criteria of #13373 and the tests that hold them. | `governor.webhook_wake_min_gap_s` | `30` | minimum spacing between webhook-triggered cycles, and between the cycles recorded review verdicts trigger | | `governor.ci_failure_webhook_kick` | `true` | a failed `check_run`/`check_suite`/`status` delivery kicks the owning agent (hive-authored PR) or runs a targeted contributor-lane pass (contributor PR) at once; `false` records the signal for the sweep only | | `governor.ci_failure_kick_cooldown_s` | `600` | at most one webhook-triggered CI-failure kick or nudge per PR head in this window | -| `governor.ci.auto_approve_workflows` | `true` (unset = on) | after CI enrichment, approve GitHub-gated (`action_required`) workflow runs once per trusted PR head; `false` keeps the gate read-only (`waiting on workflow approval` still shows). Starts CI only — review, hold and merge gates are unchanged. See [runtime-and-quota.md](operator-reference/runtime-and-quota.md) | +| `governor.ci.auto_approve_workflows` | `true` (unset = on) | after CI enrichment on a full `normal`-budget cycle (shed in `conserve`/`critical`, off-cadence and cached cycles), approve GitHub-gated (`action_required`) workflow runs once per trusted PR head; `false` keeps the gate read-only (`waiting on workflow approval` still shows). Starts CI only — review, hold and merge gates are unchanged. See [runtime-and-quota.md](operator-reference/runtime-and-quota.md) | | `governor.ci.auto_approve_workflow_actors` | `['dependabot[bot]', 'renovate[bot]']` | PR authors / triggering actors trusted to have gated runs approved, in addition to this hive's App and owner-admitted (automerge-labelled) PRs; an explicit `[]` trusts only the App and owner admission | | `forgestate.reconcile_interval_s` | `900` | ForgeState's own full-list interval per repo, shortened while drift is high | | `forgestate.freshness_bound_s` | `60` | maximum item age before merge/close/claim/kick re-reads it |