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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-15088-autopromo-search-budget.md
Original file line number Diff line number Diff line change
@@ -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.
90 changes: 73 additions & 17 deletions src/pkg/github/automerge/automerge_contributor_lane.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package automerge

// Contributor-lane auto-merge tier.
Expand Down Expand Up @@ -626,7 +626,7 @@
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
Expand Down Expand Up @@ -797,14 +797,39 @@
// 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:<login> 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
Expand All @@ -827,7 +852,46 @@
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 {
Expand All @@ -849,27 +913,19 @@
}
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
Expand Down
105 changes: 105 additions & 0 deletions src/pkg/github/automerge/automerge_contributor_lane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
8 changes: 8 additions & 0 deletions src/pkg/github/automerge/automerge_sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading