Skip to content

πŸ› fix(automerge): cache contributor merged-repo search across sweeps - #15371

Merged
hivecommons-hive[bot] merged 1 commit into
v6from
fix/contributor-merged-search-cache
Oct 29, 2026
Merged

hivecommons-hive[bot] merged 1 commit into
v6from
fix/contributor-merged-search-cache

Conversation

@Danathar

Copy link
Copy Markdown
Contributor

Summary

Contributor auto-promotion searched for an author's merges on every sweep. On a 19-repo hive that is 3 to 4 search/issues calls per author, per sweep.

Evidence from hosted-projectbluefin-knuckle-gjvq, running f950551 (before #15125):

  • contributorMergedRepos ran is:pr is:merged author:<login> repo:… repo:… for every contributor-lane PR on every sweep.
  • For github-actions[bot] (utah-packages#763) this caused 16 secondary 403s in 13 hours.
  • Each 403 opened a slow-start caution window, and merge reads were shed during it.

#15125 fixed the bot case. It skips [bot] and App logins, and it reuses one author's result within a sweep. A human author outside the registry was still searched again on the next sweep. That includes authors who already passed auto-promotion. This PR keeps the result across sweeps.

Refs #15088

Change

  • Cache on the Engine (automerge_contributor_lane.go, automerge_sweep.go). The search result for each author is kept in a mutex-guarded map on Engine for 1 hour. The result is the list of active repositories that hold a merge by the author.
  • Failures are cached too, for 15 minutes. The PR's own repository is counted alone meanwhile, as it already was on the failing sweep. A secondary limit is no longer re-tripped on every sweep. The warning is logged once per failure, not once per sweep.
  • Keyed by the active-repository set. If a repository is added, removed or paused, the cached entry is not used and the search runs again.
  • One entry per author, not per PR repository. The search now covers the PR's own repository too, and that repository is dropped when the list is read back. The sweep already did this after πŸ› fix: limit contributor promotion searches per sweepΒ #15125. One search serves the author's PRs in every repository.
  • The per-sweep context cache from πŸ› fix: limit contributor promotion searches per sweepΒ #15125 is removed. The Engine cache does the same job within a sweep.
  • Clock: uses Engine.clock() (the injectable now), so the tests step time forward.
  • Size bound: the map starts over past 1024 authors, like the closing-link cache.
  • Docs: scale-envelope.md cited automerge_sweep.go:375 for DefaultAutoMergeSweepMaxMerges. It is at line 505 after this change, and the citation now says so.
  • Changelog: changelog.d/fixed-15088-promotion-search-ttl.md.

Why caching the list is safe

Only one thing is cached: which other repositories hold a merge by the author. I checked this in contributorCleanMergedHistory:

  • The PR's own repository is always walked first, with a fresh closed-PR listing.
  • Every repository on the list is walked fresh each sweep by contributorCleanMergesInRepo. That walk counts clean merges and looks for reverts and the sentinel label.
  • So the cache cannot add a merge that was not read this sweep. A revert in a listed repository still blocks promotion on the next sweep. TestContributorPromotionSearchCacheWalksListedReposFresh checks this.

What a stale list can miss: a repository the author first merged into during the last hour. Its merges count up to an hour late. A revert of one of those merges is also seen up to an hour late. The walk already stopped at the first need clean merges before this change, so it never read every repository.

Not included, and why

  • Webhook invalidation. Clearing the cache from a merge webhook was considered. Because every listed repository is read fresh, the cache can only delay promotion by up to an hour. That did not seem worth new webhook wiring.
  • A separate "already promoted, skip the check" memo. Auto-promotion is not stored anywhere, so admitContributor checks the history again on each sweep. If the PR's own repository already has enough clean merges, no search runs at all. Otherwise the cache cuts it to one search per hour. A memo that skipped the whole check would also skip the fresh revert walk. A memo that kept the walk would save nothing beyond that one hourly search.

Testing

From src/, TZ=UTC, -count=1:

  • go test ./pkg/github/automerge/ -run 'Contributor' passes, and so does the whole ./pkg/github/automerge/ package.
  • go vet ./pkg/github/automerge/ is clean, and gofmt -l is clean on the touched files.
  • check-changelog-fragment.sh v6 and check-docs-citations.py pass.

Tests:

  • TestContributorPromotionSearchCachePerSweep (from πŸ› fix: limit contributor promotion searches per sweepΒ #15125, extended): 3 lookups for alice and 1 for bob make 2 searches. A later sweep 59 minutes on makes none. At 60 minutes alice is searched again. Its second half used to expect a new search on every sweep. That is the behaviour this PR changes, so the assertion moved.
  • TestContributorPromotionSearchFailureCached: a 403 is remembered for 15 minutes, then searched again. The success after it is kept for the full hour.
  • TestContributorPromotionSearchCacheIsPerAuthor: one search serves PRs in widget and hive, and each gets the other repository. A changed repository set searches again.
  • TestContributorPromotionSearchCacheWalksListedReposFresh: a revert added after the search blocks promotion on the next sweep, with no new search.
  • TestTrySweepContributorLanePRAutoPromotionScope: the query check changed from "must not include the PR's own repository" to "must include it", because the entry is per author.
  • TestContributorPromotionSkipsBots from πŸ› fix: limit contributor promotion searches per sweepΒ #15125 passes unchanged.

Mutation checks: 8 mutants, each restored from a backup copy. Every one fails at least one test:

Still open

  • Not run on a live hive. The fix should cut a promoted human author to one search chunk set per hour, but I have not measured that.
  • Concurrent lookups for the same author can both search before either stores the result. The mutex keeps the map safe, and a duplicate search is rare, so I left that alone.
  • Some automerge_sweep.go citations were already stale on v6. In labels-and-control-signals.md and design/github-mention-triggers.md, the cited lines (987-1057, 1242) do not hold the code they describe. The citation check still passes, so I left them for a separate docs pass.

Contributor checklist

  • Changelog: changelog.d/fixed-15088-promotion-search-ttl.md.
  • Targets v6.
  • The title uses the emoji convention.
  • Commits include DCO sign-off.
  • Docs updated (one citation move in src/docs/scale-envelope.md).
  • No secrets, credentials or local runtime state are committed.

πŸ€– Generated with Claude Code

https://claude.ai/code/session_01BQYnPGFJx81c8JXR3o1sg1

#15125 memoized contributorMergedRepos within one sweep and skipped bot
authors. A human author outside the registry was still searched again on
every sweep, 3-4 search/issues calls per author on a 19-repo hive, even
after they had passed auto-promotion.

The per-author answer (which active repositories hold a merge by them) is
now kept on the Engine for an hour, keyed by the active-repository set. A
failed search is kept for 15 minutes, so a secondary rate limit is not
re-tripped every sweep; the PR's own repository is counted alone meanwhile,
as before. The per-sweep context cache is subsumed and removed.

Only the list is cached. The PR's own repository and every listed one are
still walked fresh each sweep for clean merges and revert/sentinel
evidence, so a stale list can only leave out a repository the author first
merged into within the hour.

Refs #15088

Signed-off-by: Danathar <Danathar@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BQYnPGFJx81c8JXR3o1sg1
@kubestellar-prow kubestellar-prow Bot added the dco-signoff: yes Indicates the PR's author has signed the DCO. label Oct 29, 2026
@kubestellar-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign clubanderson for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubestellar-prow kubestellar-prow Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Oct 29, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Open PR overlap check

Base branch: v6

Conflicts are checked with every PR merged onto the current tip of v6, so a PR that only needs a rebase is not reported as conflicting with its siblings.

No conflicting open PRs or same-file overlaps were found.

@hivecommons-hive hivecommons-hive Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed β€” no findings from this perspective.

β€” hive: agent=ci-maintainer backend=copilot model=claude-fable-5.1 copilot=1.0.88

@hivecommons-hive hivecommons-hive Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed β€” no findings from this perspective.

β€” hive: agent=reviewer-2 backend=copilot model=claude-fable-5.1 copilot=1.0.88

@hivecommons-hive hivecommons-hive Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed β€” no findings from this perspective.

β€” hive: agent=reviewer backend=copilot model=claude-fable-5.1 copilot=1.0.88

@hivecommons-hive hivecommons-hive Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correctness β€” one in-scope finding (reviewed head 748175c).

The cache does not survive sweeps on the governor run-loop path

The cache is a field on Engine (src/pkg/github/automerge/automerge_sweep.go:336-337), but the contributor lane runs from two places in production, and one of them builds a throwaway Engine every sweep:

  • cmd/hive/run_loop_deps.go:89 β†’ runAutoMergeSweepIfDue β†’ automerge.SweepContributorLaneAutoMerges(ctx, ghClient, opts, …) at cmd/hive/main.go:10052 β†’ return New(transport, opts).SweepContributorLaneAutoMerges(ctx, sweepOpts) at src/pkg/github/automerge/automerge_sweep.go:492-493.

On that path c.contributorMergedReposCache is nil at the start of every sweep, so a human author outside the registry is still searched on every governor sweep β€” the exact behaviour the PR title and summary say is fixed ("This PR keeps the result across sweeps"). Only the long-lived self-authored loop (sweepPass, automerge_sweep.go:1684) benefits. The failure TTL is likewise lost there, so a 403 is re-tripped per sweep as before.

The package already documents this engine-per-call shape and the fix pattern: automerge_stage_labels.go:218-220 ("One tracker is shared process-wide (sharedStageTracker) because the governor sweep builds a fresh engine per lane") with stageTrackerOrShared() at :338-343. The tests can't catch this because every test uses a single Engine instance.

Suggested shape (tests keep an isolated cache via the Engine field; production falls through to the shared one):

--- a/src/pkg/github/automerge/automerge_contributor_lane.go
+++ b/src/pkg/github/automerge/automerge_contributor_lane.go
@@
 type contributorMergedReposEntry struct {
 	refs    []contributorRepoRef
 	repos   string
 	expires time.Time
 }
+
+// contributorMergedReposCache is shared process-wide by default because the
+// governor sweep builds a fresh Engine per lane (automerge_sweep.go:492,
+// cmd/hive/main.go:10052); see sharedStageTracker for the same reason.
+type contributorMergedReposCache struct {
+	mu      sync.Mutex
+	entries map[string]contributorMergedReposEntry
+}
+
+var sharedContributorMergedReposCache = &contributorMergedReposCache{}
+
+func (c *Engine) contributorMergedReposCacheOrShared() *contributorMergedReposCache {
+	if c != nil && c.contributorMergedRepos != nil {
+		return c.contributorMergedRepos
+	}
+	return sharedContributorMergedReposCache
+}
@@ func (c *Engine) contributorMergedRepos(
-	c.contributorMergedReposMu.Lock()
-	entry, ok := c.contributorMergedReposCache[key]
-	c.contributorMergedReposMu.Unlock()
+	cache := c.contributorMergedReposCacheOrShared()
+	cache.mu.Lock()
+	entry, ok := cache.entries[key]
+	cache.mu.Unlock()
 	if ok && entry.repos == repos && now.Before(entry.expires) {
 		return others(entry.refs)
 	}
 	remember := func(refs []contributorRepoRef, ttl time.Duration) {
-		c.contributorMergedReposMu.Lock()
-		defer c.contributorMergedReposMu.Unlock()
-		if c.contributorMergedReposCache == nil || len(c.contributorMergedReposCache) >= contributorMergedReposCacheMax {
-			c.contributorMergedReposCache = map[string]contributorMergedReposEntry{}
+		cache.mu.Lock()
+		defer cache.mu.Unlock()
+		if cache.entries == nil || len(cache.entries) >= contributorMergedReposCacheMax {
+			cache.entries = map[string]contributorMergedReposEntry{}
 		}
-		c.contributorMergedReposCache[key] = contributorMergedReposEntry{refs: refs, repos: repos, expires: now.Add(ttl)}
+		cache.entries[key] = contributorMergedReposEntry{refs: refs, repos: repos, expires: now.Add(ttl)}
 	}
--- a/src/pkg/github/automerge/automerge_sweep.go
+++ b/src/pkg/github/automerge/automerge_sweep.go
@@ type Engine struct {
-	contributorMergedReposMu    sync.Mutex
-	contributorMergedReposCache map[string]contributorMergedReposEntry
+	// contributorMergedRepos overrides the process-shared cache (tests).
+	contributorMergedRepos *contributorMergedReposCache

Then have newContributorLaneEngineRepos (test helper) set contributorMergedRepos: &contributorMergedReposCache{} so tests stay isolated, and add one test that calls the package-level SweepContributorLaneAutoMerges (or New(...) twice) and asserts fx.searches == 1 across the two engines β€” that is the case the current suite cannot see.

Minor, same area: with a shared cache the now := c.clock() used for expires must be comparable across engines; both paths use real time in production, so that holds, but worth a comment.

β€” hive: agent=adjudicator backend=copilot model=claude-fable-5.1 copilot=1.0.88

@hivecommons-hive hivecommons-hive Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed (docs-currency) β€” no blocking findings. Verified src/docs/scale-envelope.md:137 now cites automerge_sweep.go:505, which holds DefaultAutoMergeSweepMaxMerges at head 748175c.

One advisory, non-blocking: changelog.d/fixed-15088-promotion-search-ttl.md:1 states the TTL but not the operator-visible trade-off documented in the code comment (automerge_contributor_lane.go on contributorMergedReposTTL): a repository the author first merged into during the last hour is counted, and its reverts seen, up to an hour late. changelog.d/README.md asks fragments to say "why an operator cares", so consider:

- Contributor auto-promotion now remembers each author's merged-repository search for an hour (a failed search for 15 minutes) instead of searching again on every sweep, so a promoted human author no longer trips secondary rate limits sweep after sweep ([#15088](https://github.com/hivecommons/hive/issues/15088)). Every listed repository is still walked fresh each sweep; only a repository the author first merged into within the last hour can be counted up to an hour late.

β€” hive: agent=ci-maintainer backend=copilot model=claude-fable-5.1 copilot=1.0.88

@hivecommons-hive

Copy link
Copy Markdown
Contributor

@Danathar this PR is back in your court (stage/072-returned-to-contributor). The hive will not merge it until the head changes.

Review asks:

  • the review swarm requested changes

Reply here or push a new head to this branch (same sign-off); CI and the review swarm re-run automatically and the PR returns to the normal path.

Queued in your relay fix queue (1 waiting): fixes are handed out before new work as soon as you have capacity.

@hivecommons-hive
hivecommons-hive Bot merged commit f81b0e4 into v6 Oct 29, 2026
76 of 78 checks passed
@kubestellar-prow
kubestellar-prow Bot deleted the fix/contributor-merged-search-cache branch October 29, 2026 10:50
@hivecommons-hive

Copy link
Copy Markdown
Contributor
Evidence bundle `#15371748175c` sha256:a170f8b912701a99330ecdd9512ed9579b18d8bdffc22e25312d0aa5b53c378d β€” download from the dashboard or `hivectl review evidence #15371`.

@hivecommons-hive

Copy link
Copy Markdown
Contributor

merged by Hive trusted-author auto-merge: author @Danathar holds role owner and GitHub permission maintain on repo; CI green at 748175c.

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution! Your PR has been merged.

We'd love to hear how your experience was: share feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dco-signoff: yes Indicates the PR's author has signed the DCO. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant