Skip to content
Merged
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/changed-rfcnotify-pr-negative-cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- The RFC notice poll now remembers pull requests found not to be RFCs by head SHA, so unrelated busy PRs no longer have their files listed on every poll ([#15960](https://github.com/hivecommons/hive/issues/15960)).
33 changes: 32 additions & 1 deletion src/cmd/hive/rfcnotify_wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const rfcNotifyStatePath = "/data/rfc-notify.json"
// trusted before it is looked up again.
const rfcRepoVisibilityTTL = time.Hour

// rfcNotPRCacheMax bounds the negative PR classification cache; it is
// cleared when it grows past this many entries.
const rfcNotPRCacheMax = 512

// rfcNotifyState is the process-lifetime half of the RFC lifecycle notices
// (notifications.discord.rfc_channel_id): the sweeper and the Discord
// poster, built once from the config that enabled them.
Expand All @@ -38,6 +42,10 @@ type rfcNotifyState struct {
// visibility caches each repo's resolved privacy, refreshed at most
// every rfcRepoVisibilityTTL. Failed lookups are not cached.
visibility map[string]rfcRepoVisibility
// notRFCPRs remembers, per "owner/name#number", the head SHA at which a
// PR was classified as not an RFC, so its files are not listed again on
// every poll. A new head SHA is classified afresh. In memory only.
notRFCPRs map[string]string
// lookupPrivate overrides the GitHub visibility lookup in tests.
lookupPrivate func(ctx context.Context, repo string) (bool, error)
// now is the clock for the visibility cache; nil means time.Now.
Expand Down Expand Up @@ -165,6 +173,22 @@ func (s *rfcNotifyState) repoPrivate(ctx context.Context, c *github.Client, repo
return private, nil
}

// notRFCPR reports whether key was classified as not an RFC at sha.
func (s *rfcNotifyState) notRFCPR(key, sha string) bool {
return s != nil && sha != "" && s.notRFCPRs[key] == sha
}

// rememberNotRFCPR records a negative classification of key at sha.
func (s *rfcNotifyState) rememberNotRFCPR(key, sha string) {
if s == nil || sha == "" {
return
}
if s.notRFCPRs == nil || (len(s.notRFCPRs) >= rfcNotPRCacheMax && s.notRFCPRs[key] == "") {
s.notRFCPRs = map[string]string{}
}
s.notRFCPRs[key] = sha
}

// githubRepoPrivate resolves an "owner/name" repo's visibility. Anything
// but a public repository counts as private.
func githubRepoPrivate(c *github.Client) func(ctx context.Context, repo string) (bool, error) {
Expand Down Expand Up @@ -213,7 +237,7 @@ func (s rfcNotifySource) ListUpdatedSince(ctx context.Context, since time.Time)
// ListPRsUpdatedSince implements rfcnotify.PRSource. Per repo it pages the
// pulls list newest-updated first and stops at the first PR updated before
// since. A PR whose title does not mark it as an RFC has its files listed,
// so the lookup is bounded to PRs updated since the watermark; a new open
// so the lookup is bounded to PRs updated since the watermark, and a PR found not to be an RFC is not looked up again until its head SHA changes; a new open
// one that proposes an RFC doc also has the doc read for its first heading.
func (s rfcNotifySource) ListPRsUpdatedSince(ctx context.Context, since time.Time) ([]rfcnotify.Issue, error) {
api := s.c.GoGitHub()
Expand Down Expand Up @@ -264,6 +288,10 @@ func (s rfcNotifySource) rfcNotifyPR(ctx context.Context, api *gh.Client, owner,
if rfcnotify.IsRFCPR(is.Title, nil) {
return is, nil
}
cacheKey, sha := fmt.Sprintf("%s/%s#%d", owner, name, is.Number), pr.GetHead().GetSHA()
if s.state.notRFCPR(cacheKey, sha) {
return is, nil
}
fopts := &gh.ListOptions{PerPage: 100}
for {
files, resp, err := api.PullRequests.ListFiles(ctx, owner, name, is.Number, fopts)
Expand All @@ -280,6 +308,9 @@ func (s rfcNotifySource) rfcNotifyPR(ctx context.Context, api *gh.Client, owner,
}
fopts.Page = resp.NextPage
}
if !rfcnotify.IsRFCPR(is.Title, is.Files) {
s.state.rememberNotRFCPR(cacheKey, sha)
}
if !is.Open() || !is.CreatedAt.After(since) {
return is, nil
}
Expand Down
51 changes: 51 additions & 0 deletions src/cmd/hive/rfcnotify_wire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,57 @@ func TestRFCNotifySourceListsPRs(t *testing.T) {
}
}

func TestRFCNotifySourceCachesNotRFCPRsByHeadSHA(t *testing.T) {
since := time.Date(2026, 10, 27, 0, 0, 0, 0, time.UTC)
sha, fileCalls := "sha1", 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/repos/org/repo/pulls":
_, _ = w.Write([]byte(`[{"number":40,"title":"🐛 fix: typo","state":"open","head":{"sha":"` + sha + `"},"created_at":"2026-10-20T00:00:00Z","updated_at":"2026-10-28T00:00:00Z"}]`))
case "/repos/org/repo/pulls/40/files":
fileCalls++
_, _ = w.Write([]byte(`[{"filename":"README.md","status":"modified"}]`))
default:
t.Errorf("unexpected request %s", r.URL.Path)
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()
c := github.NewClientForTest(server.URL, "org", []string{"repo"}, slog.New(slog.NewTextHandler(io.Discard, nil)))
src := newRFCNotifySource(c, &rfcNotifyState{lookupPrivate: func(context.Context, string) (bool, error) { return false, nil }}).(rfcnotify.PRSource)
poll := func(wantCalls int) {
t.Helper()
prs, err := src.ListPRsUpdatedSince(context.Background(), since)
if err != nil || len(prs) != 1 || prs[0].RFC() {
t.Fatalf("ListPRsUpdatedSince = %+v %v", prs, err)
}
if fileCalls != wantCalls {
t.Fatalf("ListFiles calls = %d, want %d", fileCalls, wantCalls)
}
}
poll(1)
poll(1)
sha = "sha2"
poll(2)
poll(2)
}

func TestRFCNotifyNotRFCPRCacheIsBounded(t *testing.T) {
s := &rfcNotifyState{}
for i := 0; i < rfcNotPRCacheMax*2; i++ {
s.rememberNotRFCPR(fmt.Sprintf("org/repo#%d", i), "sha")
}
if len(s.notRFCPRs) > rfcNotPRCacheMax {
t.Errorf("cache grew to %d entries, cap %d", len(s.notRFCPRs), rfcNotPRCacheMax)
}
var nilState *rfcNotifyState
nilState.rememberNotRFCPR("k", "sha")
if nilState.notRFCPR("k", "sha") {
t.Error("nil state must not cache")
}
}

type rfcNotifyPRFakeSource struct {
rfcNotifyFakeSource
prs []rfcnotify.Issue
Expand Down
Loading