From 1494ba89972aed10931de7886c90ec3a913e94c0 Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Sat, 31 Oct 2026 01:28:21 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=B1=20chore:=20cache=20negative=20RFC?= =?UTF-8?q?=20classification=20of=20PRs=20by=20head=20SHA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- .../changed-rfcnotify-pr-negative-cache.md | 1 + src/cmd/hive/rfcnotify_wire.go | 33 +++++++++++- src/cmd/hive/rfcnotify_wire_test.go | 51 +++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 changelog.d/changed-rfcnotify-pr-negative-cache.md diff --git a/changelog.d/changed-rfcnotify-pr-negative-cache.md b/changelog.d/changed-rfcnotify-pr-negative-cache.md new file mode 100644 index 0000000000..239cb8d812 --- /dev/null +++ b/changelog.d/changed-rfcnotify-pr-negative-cache.md @@ -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)). diff --git a/src/cmd/hive/rfcnotify_wire.go b/src/cmd/hive/rfcnotify_wire.go index 0733302f89..685803bd10 100644 --- a/src/cmd/hive/rfcnotify_wire.go +++ b/src/cmd/hive/rfcnotify_wire.go @@ -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. @@ -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. @@ -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) { @@ -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() @@ -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) @@ -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 } diff --git a/src/cmd/hive/rfcnotify_wire_test.go b/src/cmd/hive/rfcnotify_wire_test.go index ccaca92950..5731bdb755 100644 --- a/src/cmd/hive/rfcnotify_wire_test.go +++ b/src/cmd/hive/rfcnotify_wire_test.go @@ -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