From 5c23fc4aebd8092b5323de3b4e54e72ffd60add5 Mon Sep 17 00:00:00 2001 From: Danathar Date: Wed, 28 Oct 2026 16:34:58 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(automerge):=20keep=20contrib?= =?UTF-8?q?utor=20auto-promotion=20inside=20the=20search=20budget?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The hive-wide merged-history search behind contributor-lane auto-promotion (#14517) ran for every open PR by an author on every sweep, and also for `[bot]` authors, which can never be promoted. On a 19-repository hive that is 3–4 search/issues calls per PR, and it tripped GitHub's search secondary rate limit five times in one evening on a production hive, sending the whole hive into its rate-limit caution window each time. - Exempt `[bot]` logins from auto-promotion: a bot is trusted only through the trusted bot authors setting, never by counting its merges. - Cache each (repository, author) answer for ten minutes, mirroring the maintainerPerm cache; a failed search is not cached. Fixes #15088 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SnzFs53Qcer1xDySyxv1Vj Signed-off-by: Danathar --- .../fixed-15088-autopromo-search-budget.md | 1 + .../automerge/automerge_contributor_lane.go | 90 ++++++++++++--- .../automerge_contributor_lane_test.go | 105 ++++++++++++++++++ src/pkg/github/automerge/automerge_sweep.go | 8 ++ 4 files changed, 187 insertions(+), 17 deletions(-) create mode 100644 changelog.d/fixed-15088-autopromo-search-budget.md diff --git a/changelog.d/fixed-15088-autopromo-search-budget.md b/changelog.d/fixed-15088-autopromo-search-budget.md new file mode 100644 index 0000000000..e54864128a --- /dev/null +++ b/changelog.d/fixed-15088-autopromo-search-budget.md @@ -0,0 +1 @@ +- **Contributor-lane auto-promotion no longer trips GitHub's search secondary rate limit** ([#15088](https://github.com/hivecommons/hive/issues/15088)). The hive-wide merged-history search ([#14517](https://github.com/hivecommons/hive/issues/14517)) ran again for every open PR by an author on every sweep, and also ran for `[bot]` authors such as `github-actions[bot]`. On a 19-repository hive that took 3–4 Search API calls per PR, and the resulting secondary-limit refusals sent the hive into its rate-limit caution window five times in one evening, pausing merges and dashboard refreshes each time. Bots are now exempt from auto-promotion (they are trusted only through the trusted bot authors setting, never by counting merges), and each (repository, author) answer is cached for ten minutes; a failed search is not cached. diff --git a/src/pkg/github/automerge/automerge_contributor_lane.go b/src/pkg/github/automerge/automerge_contributor_lane.go index 4991610b2f..111711bec3 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane.go +++ b/src/pkg/github/automerge/automerge_contributor_lane.go @@ -626,7 +626,7 @@ func (c *Engine) admitContributor(ctx context.Context, owner, repo string, pr *g if match.ObservedTier == "" { match.ObservedTier = c.observedRegistryTier(author) } - if err == nil && match.Reason != "" && policy.AutoPromoteAfterMergedPR > 0 && !contributorRegistryTierRevoked(c.observedRegistryTier(author)) { + if err == nil && match.Reason != "" && policy.AutoPromoteAfterMergedPR > 0 && !contributorAutoPromotionExempt(author) && !contributorRegistryTierRevoked(c.observedRegistryTier(author)) { history, promoteErr := c.contributorCleanMergedHistory(ctx, owner, repo, author, policy.AutoPromoteAfterMergedPR, policy.hiveWideAutoPromotion()) if promoteErr != nil { return contributorClaimMatch{Reason: "contributor-lane:auto-promote-history"}, promoteErr @@ -797,14 +797,39 @@ type contributorRepoRef struct { // contributorSearchQueryMaxLen is GitHub's limit on a search `q` parameter. const contributorSearchQueryMaxLen = 256 +// contributorMergedReposTTL is how long contributorMergedRepos trusts an +// author's search answer. Merged history only grows, so a stale answer at +// worst delays a promotion by one window; without the cache every open PR by +// the author repeated the same Search API calls on every sweep, and on a +// 19-repository hive that tripped GitHub's search secondary rate limit +// several times a day (#15088). +const contributorMergedReposTTL = 10 * time.Minute + +type contributorMergedReposEntry struct { + found map[string]bool + at time.Time +} + +// contributorAutoPromotionExempt reports whether author can never be +// auto-promoted: automation accounts (the GitHub `[bot]` suffix, which covers +// the hive's own App). A bot is trusted only through the explicit trusted +// bot authors setting, never by counting its merges, and asking the Search +// API about one only spends the search budget. +func contributorAutoPromotionExempt(author string) bool { + return strings.HasSuffix(strings.ToLower(strings.TrimSpace(author)), "[bot]") +} + // contributorMergedRepos returns the hive's other active repositories that // hold at least one merged PR by author, in the order the transport lists // them, excluding the PR's own (owner, repo). It asks search/issues // `is:pr is:merged author: repo:… repo:…`, chunked to GitHub's // 256-character query limit, so a ten-repo hive costs one or two search -// calls instead of ten closed-PR listings. A search failure degrades to the -// PR's own repository with a warning rather than blocking the sweep: the -// result is then exactly the pre-#14517 per-repo count. +// calls instead of ten closed-PR listings. The answer is cached per +// (repository, author) for contributorMergedReposTTL, so the author's other +// PRs in the same repository reuse it instead of searching again on every +// sweep. A search failure is not cached and degrades to the PR's own +// repository with a warning rather than blocking the sweep: the result is +// then exactly the pre-#14517 per-repo count. func (c *Engine) contributorMergedRepos(ctx context.Context, owner, repo, author string) []contributorRepoRef { if c == nil || c.gh == nil || c.transport == nil { return nil @@ -827,7 +852,46 @@ func (c *Engine) contributorMergedRepos(ctx context.Context, owner, repo, author if len(candidates) == 0 { return nil } - base := "is:pr is:merged author:" + strings.TrimPrefix(strings.TrimSpace(author), "@") + login := strings.TrimPrefix(strings.TrimSpace(author), "@") + key := own + "#" + strings.ToLower(login) + now := c.clock() + c.mergedReposCacheMu.Lock() + cached, ok := c.mergedReposCache[key] + c.mergedReposCacheMu.Unlock() + found := cached.found + if !ok || now.Sub(cached.at) >= contributorMergedReposTTL { + var err error + found, err = c.searchContributorMergedRepos(ctx, login, candidates) + if err != nil { + c.warn("contributor-lane auto-promotion repo search failed; counting the PR's repository only", "repo", owner+"/"+repo, "author", author, "error", err) + return nil + } + c.mergedReposCacheMu.Lock() + if c.mergedReposCache == nil { + c.mergedReposCache = map[string]contributorMergedReposEntry{} + } + for k, e := range c.mergedReposCache { + if now.Sub(e.at) >= contributorMergedReposTTL { + delete(c.mergedReposCache, k) + } + } + c.mergedReposCache[key] = contributorMergedReposEntry{found: found, at: now} + c.mergedReposCacheMu.Unlock() + } + out := make([]contributorRepoRef, 0, len(found)) + for _, ref := range candidates { + if found[strings.ToLower(ref.Owner+"/"+ref.Repo)] { + out = append(out, ref) + } + } + return out +} + +// searchContributorMergedRepos runs the chunked search/issues query for +// login's merged PRs across repos and returns the lower-cased owner/repo of +// every repository that answered with at least one. +func (c *Engine) searchContributorMergedRepos(ctx context.Context, login string, repos []contributorRepoRef) (map[string]bool, error) { + base := "is:pr is:merged author:" + login found := map[string]bool{} query := base flush := func() error { @@ -849,27 +913,19 @@ func (c *Engine) contributorMergedRepos(ctx context.Context, owner, repo, author } return nil } - for _, ref := range candidates { + for _, ref := range repos { qualifier := " repo:" + ref.Owner + "/" + ref.Repo if len(query)+len(qualifier) > contributorSearchQueryMaxLen { if err := flush(); err != nil { - c.warn("contributor-lane auto-promotion repo search failed; counting the PR's repository only", "repo", owner+"/"+repo, "author", author, "error", err) - return nil + return nil, err } } query += qualifier } if err := flush(); err != nil { - c.warn("contributor-lane auto-promotion repo search failed; counting the PR's repository only", "repo", owner+"/"+repo, "author", author, "error", err) - return nil + return nil, err } - out := make([]contributorRepoRef, 0, len(found)) - for _, ref := range candidates { - if found[strings.ToLower(ref.Owner+"/"+ref.Repo)] { - out = append(out, ref) - } - } - return out + return found, nil } // repoFromRepositoryURL extracts lower-cased owner/repo from a search diff --git a/src/pkg/github/automerge/automerge_contributor_lane_test.go b/src/pkg/github/automerge/automerge_contributor_lane_test.go index f48318722e..84535900ee 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane_test.go +++ b/src/pkg/github/automerge/automerge_contributor_lane_test.go @@ -1666,3 +1666,108 @@ func TestContributorLaneAssociationTrusted(t *testing.T) { t.Fatal("nil PR must not be trusted") } } + +// TestContributorAutoPromotionSearchBudget pins the Search API budget of the +// hive-wide auto-promotion check (#15088): a bot author is never searched +// for, and one (repository, author) answer serves every sweep inside +// contributorMergedReposTTL, while a failed search is retried. +func TestContributorAutoPromotionSearchBudget(t *testing.T) { + hiveRepos := []string{"widget", "hive", "docs", "homebrew-hive"} + history := func(author string, n int) []map[string]any { + out := make([]map[string]any, 0, n) + for i := 0; i < n; i++ { + out = append(out, mergedHistoryPR(author, "fix: clean change")) + } + return out + } + sweep := func(t *testing.T, engine *Engine, policy ContributorLanePolicy) string { + t.Helper() + _, reason, err := engine.trySweepContributorLanePR(context.Background(), "widget", "acme", "widget", 7, policy) + if err != nil { + t.Fatalf("err = %v", err) + } + return reason + } + setup := func(t *testing.T, fx *contributorLaneFixture) (*Engine, ContributorLanePolicy, *contributorLaneClock, func()) { + t.Helper() + api := newContributorLaneAPI(t, fx) + policy := defaultContributorLanePolicy() + policy.AutoPromoteAfterMergedPR = 5 + clock := &contributorLaneClock{at: time.Date(2026, 10, 28, 20, 0, 0, 0, time.UTC)} + engine := newContributorLaneEngineRepos(api.URL, hiveRepos, policy, clock, Options{ContributorSoak: soakedTracker(clock)}) + return engine, policy, clock, api.Close + } + + t.Run("bot author is never searched or promoted", func(t *testing.T) { + const bot = "github-actions[bot]" + fx := validContributorFixture() + fx.author = bot + fx.issueComments[0]["body"] = contributorClaimMarkerTier(bot, "contributor", contributorLaneTestHive, " tier=contributor verified=true") + fx.mergedHistory = history(bot, 1) + fx.otherHistory = map[string][]map[string]any{"hive": history(bot, 9)} + engine, policy, _, done := setup(t, &fx) + defer done() + sweep(t, engine, policy) + if fx.searches != 0 { + t.Fatalf("searches = %d, want 0 for a bot author (q=%q)", fx.searches, fx.searchQuery) + } + if fx.merges != 0 { + t.Fatalf("merges = %d, want 0: a bot must not be auto-promoted by merge count", fx.merges) + } + }) + + t.Run("one search per repository and author per window", func(t *testing.T) { + fx := validContributorFixture() + fx.issueComments[0]["body"] = contributorClaimMarkerTier(contributorLaneTestAuthor, "contributor", contributorLaneTestHive, " tier=contributor verified=true") + fx.mergedHistory = history(contributorLaneTestAuthor, 1) + fx.otherHistory = map[string][]map[string]any{"hive": history(contributorLaneTestAuthor, 2)} + engine, policy, clock, done := setup(t, &fx) + defer done() + for i := 0; i < 3; i++ { + if reason := sweep(t, engine, policy); reason != contributorLaneReasonClaimTier { + t.Fatalf("sweep %d reason = %q, want %q", i, reason, contributorLaneReasonClaimTier) + } + } + if fx.searches != 1 { + t.Fatalf("searches = %d after three sweeps in one window, want 1", fx.searches) + } + if strings.Join(fx.otherLists, ",") != "hive,hive,hive" { + t.Fatalf("other repos listed = %v, want the cached answer reused each sweep", fx.otherLists) + } + clock.advance(contributorMergedReposTTL) + sweep(t, engine, policy) + if fx.searches != 2 { + t.Fatalf("searches = %d after the window expired, want 2", fx.searches) + } + }) + + t.Run("failed search is not cached", func(t *testing.T) { + fx := validContributorFixture() + fx.issueComments[0]["body"] = contributorClaimMarkerTier(contributorLaneTestAuthor, "contributor", contributorLaneTestHive, " tier=contributor verified=true") + fx.mergedHistory = history(contributorLaneTestAuthor, 1) + fx.otherHistory = map[string][]map[string]any{"hive": history(contributorLaneTestAuthor, 9)} + fx.searchStatus = http.StatusUnprocessableEntity + engine, policy, _, done := setup(t, &fx) + defer done() + sweep(t, engine, policy) + sweep(t, engine, policy) + if fx.searches != 2 { + t.Fatalf("searches = %d, want 2: a failed search must be retried, not cached", fx.searches) + } + }) +} + +func TestContributorAutoPromotionExempt(t *testing.T) { + for login, want := range map[string]bool{ + "github-actions[bot]": true, + "Mergeraptor[Bot]": true, + " hivecommons-hive[bot]": true, + "mrbobbytables": false, + "bot-fan": false, + "": false, + } { + if got := contributorAutoPromotionExempt(login); got != want { + t.Errorf("contributorAutoPromotionExempt(%q) = %v, want %v", login, got, want) + } + } +} diff --git a/src/pkg/github/automerge/automerge_sweep.go b/src/pkg/github/automerge/automerge_sweep.go index 99e7a1654a..51c99c906e 100644 --- a/src/pkg/github/automerge/automerge_sweep.go +++ b/src/pkg/github/automerge/automerge_sweep.go @@ -209,6 +209,14 @@ type Engine struct { maintainerPermMu sync.Mutex maintainerPerm map[string]maintainerPermEntry + // mergedReposCache caches, per (repository, author), which of the hive's + // repositories hold a merged PR by them (contributorMergedRepos): the + // hive-wide auto-promotion search runs once per (repository, author) per + // window, not + // once per PR per sweep (#15088). + mergedReposCacheMu sync.Mutex + mergedReposCache map[string]contributorMergedReposEntry + requiredChecksMu sync.RWMutex requiredChecks map[string]bool requiredChecksForRepo func(repo string) (map[string]bool, bool)