From ed1330f5fcbd4e371b8611755ee645c78f311fa7 Mon Sep 17 00:00:00 2001 From: Danathar Date: Sat, 7 Nov 2026 21:11:42 -0500 Subject: [PATCH] fix(github): the un-park sweep reuses comment and timeline reads of unchanged items The issue un-park sweep re-read the comment list of every parked issue, every PR waiting at 085/070 and every recently updated PR, plus the label timeline of every approval-labelled issue, on every pass. Almost none of those items change between passes, yet most reads were charged: on the knuckle hive `hive:issue_unpark` made 1222 comment reads (878 charged) and 480 timeline reads (363 charged) in eight passes, the largest consumer. The sweep now keeps the last comment list and label timeline per item, keyed by the item's updated_at from the issue list it starts from, and reuses them while updated_at has not moved. Only raw data is cached: the seven-day command window, repo permission and label-trust checks still run every pass. An item is read again when updated_at moves, when the read was within two minutes of its last update, after the sweep writes to it, and at least every six hours. The cache is in memory, pruned to listed items and bounded by item and comment count. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BQYnPGFJx81c8JXR3o1sg1 Signed-off-by: Danathar --- .../fixed-issue-unpark-skip-unchanged.md | 1 + src/pkg/github/cacheable_since_test.go | 5 + src/pkg/github/client.go | 3 + src/pkg/github/issue_unpark_command.go | 39 ++- src/pkg/github/issue_unpark_command_test.go | 22 +- src/pkg/github/issue_unpark_read_cache.go | 290 ++++++++++++++++++ .../issue_unpark_read_cache_internal_test.go | 55 ++++ .../github/issue_unpark_read_cache_test.go | 205 +++++++++++++ src/pkg/github/label_approval.go | 11 +- src/pkg/github/pr_approve_command.go | 2 +- src/pkg/github/pr_unpark_decision.go | 2 +- 11 files changed, 624 insertions(+), 11 deletions(-) create mode 100644 changelog.d/fixed-issue-unpark-skip-unchanged.md create mode 100644 src/pkg/github/issue_unpark_read_cache.go create mode 100644 src/pkg/github/issue_unpark_read_cache_internal_test.go create mode 100644 src/pkg/github/issue_unpark_read_cache_test.go diff --git a/changelog.d/fixed-issue-unpark-skip-unchanged.md b/changelog.d/fixed-issue-unpark-skip-unchanged.md new file mode 100644 index 0000000000..6090818b90 --- /dev/null +++ b/changelog.d/fixed-issue-unpark-skip-unchanged.md @@ -0,0 +1 @@ +- github: the issue un-park sweep reuses an item's comment list and label timeline while the item's `updated_at` in the issue list has not moved, instead of re-reading both for every parked issue and waiting PR on every pass. The seven-day command window, repo-permission and label-trust checks still run on every pass against the reused data; an item is read again when it changes, when the read was taken within two minutes of its last update, after the sweep writes to it, and at least every six hours. On the knuckle hive these reads were the largest REST consumer (`hive:issue_unpark`: 878 comment and 363 timeline reads charged in 50 minutes). diff --git a/src/pkg/github/cacheable_since_test.go b/src/pkg/github/cacheable_since_test.go index 226937f893..1a4f817100 100644 --- a/src/pkg/github/cacheable_since_test.go +++ b/src/pkg/github/cacheable_since_test.go @@ -144,6 +144,11 @@ func TestSweepIssueUnparkCommands_CommentListSinceIsCacheable(t *testing.T) { if _, err := client.SweepIssueUnparkCommands(context.Background(), opts); err != nil { t.Fatalf("sweep at %v: %v", now, err) } + // The item never changes, so the sweep's per-item read cache would + // answer passes 2 and 3 without any request at all + // (issue_unpark_read_cache.go). Drop it: this test is about the URL + // a read sends when one is needed. + client.unparkReads.prune("org", "repo", nil) } assertCacheablePasses(t, srv.requests(), "2026-10-31T19:00:00Z", "2026-10-31T20:00:00Z") } diff --git a/src/pkg/github/client.go b/src/pkg/github/client.go index 95dd99c15f..6a678f5226 100644 --- a/src/pkg/github/client.go +++ b/src/pkg/github/client.go @@ -513,6 +513,9 @@ type Client struct { // prReplyOptions tracks the PRs whose status comment carries the "Your // move" section (pr_reply_options.go). prReplyOptions prReplyOptions + // unparkReads is the un-park sweep's per-item read cache + // (issue_unpark_read_cache.go). Zero value is ready to use. + unparkReads unparkReadCache // noPRParkChecks paces the issue stage sweep's look at stalled 010 // issues the hive may have answered without a PR (issue_no_pr_park.go). noPRParkChecks noPRParkChecks diff --git a/src/pkg/github/issue_unpark_command.go b/src/pkg/github/issue_unpark_command.go index 5701cc1f49..41d25b7542 100644 --- a/src/pkg/github/issue_unpark_command.go +++ b/src/pkg/github/issue_unpark_command.go @@ -325,6 +325,9 @@ func (c *Client) SweepIssueUnparkCommands(ctx context.Context, opts IssueUnparkS // it round their since down to the hour so repeat passes stay cacheable // (cacheableSince). cutoff := now.Add(-unparkCommandScanWindow) + // Comment and timeline reads of an item whose updated_at has not moved + // since the last pass are reused (issue_unpark_read_cache.go). + ctx = withUnparkReadScope(ctx, &c.unparkReads, now) for _, repo := range c.getRepos() { if result.actions() >= maxActions { @@ -350,6 +353,13 @@ func (c *Client) SweepIssueUnparkCommands(ctx context.Context, opts IssueUnparkS if err != nil { return result, err } + listed := make(map[int]bool, len(issues)) + for _, issue := range issues { + if issue != nil { + listed[issue.GetNumber()] = true + } + } + c.unparkReads.prune(owner, repoName, listed) unparkedBefore := len(result.Unparked) // waitingPRs are the PRs this pass saw at 085 or 070; the rest of // the repo's PRs lose their "Your move" section after a full pass. @@ -368,6 +378,9 @@ func (c *Client) SweepIssueUnparkCommands(ctx context.Context, opts IssueUnparkS // Items without an open episode cost no API call. n, err := c.replyAttentionCommands(ctx, owner+"/"+repoName, owner, repoName, issue, cutoff) result.Replies += n + if n > 0 || err != nil { + c.forgetUnparkRead(ctx, owner, repoName, issue.GetNumber()) + } if err != nil && c.logger != nil { c.logger.Warn("unpark sweep skipped attention commands", "repo", repo, "item", issue.GetNumber(), "error", err) } @@ -380,6 +393,9 @@ func (c *Client) SweepIssueUnparkCommands(ctx context.Context, opts IssueUnparkS // against the issue path. if opts.PRApproveReply != nil && issue.GetComments() > 0 && !issue.GetUpdatedAt().Time.Before(cutoff) { acted, err := c.replyPRApproveCommand(ctx, repo, owner, repoName, issue, cutoff, opts.PRApproveReply) + if acted || err != nil { + c.forgetUnparkRead(ctx, owner, repoName, issue.GetNumber()) + } if err != nil { // One PR's failure must not stop un-parking for the rest of the pass. if c.logger != nil { @@ -402,6 +418,9 @@ func (c *Client) SweepIssueUnparkCommands(ctx context.Context, opts IssueUnparkS result.Seen++ waitingPRs[issue.GetNumber()] = true acted, err := c.tryUnparkPullRequest(ctx, repo, owner, repoName, issue, cutoff, opts, result) + if acted || err != nil { + c.forgetUnparkRead(ctx, owner, repoName, issue.GetNumber()) + } // After the command, so an approval it just read clears the // section in the same pass. Never counted as an action. if opts.PRReplyOptions { @@ -427,6 +446,9 @@ func (c *Client) SweepIssueUnparkCommands(ctx context.Context, opts IssueUnparkS } result.Seen++ acted, err := c.tryUnparkIssue(ctx, repo, owner, repoName, issue, cutoff, labelDriven, unparkLabelApproval{policy: policy, enabled: labelApproval, audit: opts.LabelApprovalAudit}, releasePolicy, result, opts.Audit, opts.ClosedAudit) + if acted || err != nil { + c.forgetUnparkRead(ctx, owner, repoName, issue.GetNumber()) + } if err != nil { if c.noteArchivedWrite(strings.ToLower(owner+"/"+repoName), err) { if c.logger != nil { @@ -551,7 +573,7 @@ func unparkTextsProposeOwnerStep(issue *gh.Issue, comments []*gh.IssueComment) b // remove only the park labels the hive added itself. func (c *Client) tryUnparkIssue(ctx context.Context, displayRepo, owner, repo string, issue *gh.Issue, cutoff time.Time, labelDriven bool, la unparkLabelApproval, releasePolicy unparkReleasePolicy, result *IssueUnparkSweepResult, audit, closedAudit func(IssueUnparkEvent)) (bool, error) { number := issue.GetNumber() - comments, err := c.listIssueComments(ctx, owner, repo, number) + comments, err := c.unparkComments(ctx, owner, repo, issue, time.Time{}) if err != nil { return false, err } @@ -723,7 +745,9 @@ func (c *Client) tryUnparkIssue(ctx context.Context, displayRepo, owner, repo st func (c *Client) tryLabelApproval(ctx context.Context, displayRepo, owner, repo string, issue *gh.Issue, comments []*gh.IssueComment, la unparkLabelApproval, result *IssueUnparkSweepResult) (bool, error) { number := issue.GetNumber() labels := issueLabelNames(issue.Labels) - f, err := c.evaluateLabelApproval(ctx, owner, repo, issue, la.policy) + f, err := c.evaluateLabelApprovalFrom(ctx, owner, repo, issue, la.policy, func() ([]issueLabelEvent, error) { + return c.unparkLabelEvents(ctx, owner, repo, issue) + }) if err != nil { return false, err } @@ -1688,6 +1712,13 @@ func unparkOptionLetter(line string) string { // park re-check note does); a refusal keeps the in-place edit. func (c *Client) ensureUnparkNoticeComment(ctx context.Context, owner, repo string, issue *gh.Issue, comments []*gh.IssueComment, releaseOnApprove bool) error { number := issue.GetNumber() + // A write here changes the comments the sweep cached for the item. + wrote := false + defer func() { + if wrote { + c.forgetUnparkRead(ctx, owner, repo, number) + } + }() desired := renderUnparkNoticeForPolicy(c.unparkMenuFor(issue.GetBody(), issue.GetUser(), comments), releaseOnApprove) var notices []*gh.IssueComment last := -1 @@ -1698,6 +1729,7 @@ func (c *Client) ensureUnparkNoticeComment(ctx context.Context, owner, repo stri } } if last < 0 { + wrote = true if _, _, err := c.client.Issues.CreateComment(ctx, owner, repo, number, &gh.IssueComment{Body: gh.Ptr(desired)}); err != nil { return fmt.Errorf("creating unpark notice on %s/%s#%d: %w", owner, repo, number, err) } @@ -1711,6 +1743,7 @@ func (c *Client) ensureUnparkNoticeComment(ctx context.Context, owner, repo stri if d := c.WritebackAllowed(owner+"/"+repo, item, writeback.KindNeedsDecisionComment, desired); !d.Allowed { stale = nil } else if buried { + wrote = true if _, _, err := c.client.Issues.CreateComment(ctx, owner, repo, number, &gh.IssueComment{Body: gh.Ptr(desired)}); err != nil { // writeback:gated return fmt.Errorf("moving unpark notice to the bottom of %s/%s#%d: %w", owner, repo, number, err) } @@ -1718,11 +1751,13 @@ func (c *Client) ensureUnparkNoticeComment(ctx context.Context, owner, repo stri } } if keep := notices[len(notices)-1]; !moved && keep.GetBody() != desired { + wrote = true if _, _, err := c.client.Issues.EditComment(ctx, owner, repo, keep.GetID(), &gh.IssueComment{Body: gh.Ptr(desired)}); err != nil { return fmt.Errorf("editing unpark notice on %s/%s#%d: %w", owner, repo, number, err) } } for _, cm := range stale { + wrote = true if _, err := c.client.Issues.DeleteComment(ctx, owner, repo, cm.GetID()); err != nil { // writeback:gated return fmt.Errorf("deleting buried unpark notice on %s/%s#%d: %w", owner, repo, number, err) } diff --git a/src/pkg/github/issue_unpark_command_test.go b/src/pkg/github/issue_unpark_command_test.go index 8d77b9429f..8bbc503707 100644 --- a/src/pkg/github/issue_unpark_command_test.go +++ b/src/pkg/github/issue_unpark_command_test.go @@ -30,6 +30,12 @@ type unparkWireIssue struct { PullRequest *unparkWirePRLink `json:"pull_request,omitempty"` HeadSHA string `json:"-"` State string `json:"state,omitempty"` + // UpdatedAt is the item's updated_at in the list; empty omits it, which + // keeps the sweep's per-item read cache out of the test. + UpdatedAt string `json:"updated_at,omitempty"` + // CommentCount is the list's comments count; the PR approve-command + // read is skipped for a PR without comments. + CommentCount int `json:"comments,omitempty"` } type unparkWirePRLink struct { @@ -85,6 +91,8 @@ type unparkServer struct { register func(unparkWireIssue) // orgLookups counts membership lookups, permLookups permission lookups. orgLookups, permLookups, timelineReads int + // commentReads counts comment-list GETs per issue number. + commentReads map[int]int } type unparkWireTimelineEvent struct { @@ -108,12 +116,13 @@ func unlabeledAt(id int64, actor, label string, at time.Time) unparkWireTimeline func newUnparkServer(t *testing.T, owner, repo string, issues []unparkWireIssue, permissions map[string]string) (*httptest.Server, *unparkServer) { t.Helper() rec := &unparkServer{ - posted: map[int][]string{}, - edited: map[int64]string{}, - added: map[int][]string{}, - removed: map[int][]string{}, - patched: map[int][]string{}, - permissions: permissions, + posted: map[int][]string{}, + edited: map[int64]string{}, + added: map[int][]string{}, + removed: map[int][]string{}, + patched: map[int][]string{}, + permissions: permissions, + commentReads: map[int]int{}, } byNum := map[int]unparkWireIssue{} for _, issue := range issues { @@ -201,6 +210,7 @@ func newUnparkServer(t *testing.T, owner, repo string, issues []unparkWireIssue, case strings.HasSuffix(path, "/comments") && r.Method == http.MethodGet: var n int fmt.Sscanf(path, "issues/%d/comments", &n) + rec.commentReads[n]++ _ = json.NewEncoder(w).Encode(byNum[n].Comments) case strings.HasSuffix(path, "/comments") && r.Method == http.MethodPost: diff --git a/src/pkg/github/issue_unpark_read_cache.go b/src/pkg/github/issue_unpark_read_cache.go new file mode 100644 index 0000000000..b60b9e413e --- /dev/null +++ b/src/pkg/github/issue_unpark_read_cache.go @@ -0,0 +1,290 @@ +package github + +import ( + "context" + "strconv" + "strings" + "sync" + "time" + + gh "github.com/google/go-github/v72/github" +) + +// The un-park sweep's per-item read cache. +// +// Every pass (every five minutes, sooner after a webhook) the sweep read +// the comment list of every parked issue, of every PR waiting at 085 or +// 070, and of every PR updated in the last week, plus the label timeline of +// every issue carrying an approval label on a label-approval repo. Almost +// none of those items change between two passes, but the reads were charged +// anyway whenever the ETag cache could not answer them: after every +// installation-token refresh (the ETag cache is keyed by identity) and +// whenever the hour-rounded since moved. On the knuckle hive (2026-11-08, +// 00:55–01:45Z) that was 1222 comment reads (878 charged) and 480 timeline +// reads (363 charged) in eight passes, the largest single consumer. +// +// Each item the sweep starts from already carries its updated_at in the +// issue list response, and GitHub moves it on every change these reads can +// see: a new, edited or deleted comment, and a label added or removed. So a +// result read while the item had the same updated_at is still that item's +// data, and the sweep reuses it instead of asking again. +// +// Only raw data is cached, never a decision. Every check that depends on +// the clock or on live state (the seven-day command window, the +// commenter's repo permission, an approval label applier's trust) runs on +// every pass against the cached comments and events, exactly as it would +// on a fresh read. +// +// A cached result is reused only when all of these hold; otherwise the item +// is read again: +// - the listed updated_at is exactly the one the result was read under; +// - the result was read at least unparkReadSettle after that updated_at, +// so a list and a comment read that disagree for a moment after a change +// cannot pin a stale result; +// - the result is younger than unparkReadTTL, a backstop for any change +// GitHub does not stamp; +// - the sweep wrote nothing to the item since (forgetUnparkRead). +// +// The cache lives in memory only: after a restart it is empty and the +// first pass reads everything, which is the old behaviour. Entries for items +// that leave a repo's open list are dropped after that repo's pass, and the +// whole cache is bounded by unparkReadMaxItems. + +const ( + // unparkReadTTL bounds how long one read is reused for an item whose + // updated_at has not moved. + unparkReadTTL = 6 * time.Hour + // unparkReadSettle is how long after an item's updated_at a read must + // have happened before the sweep trusts it to contain that update. + unparkReadSettle = 2 * time.Minute + // unparkReadMaxItems and unparkReadMaxComments bound the cache; past + // either the oldest read is dropped. The knuckle hive's sweep reads about + // 220 items holding about 1,200 comments (a few MB). + unparkReadMaxItems = 4096 + unparkReadMaxComments = 50000 +) + +// unparkReadEntry is what the sweep last read for one item. +type unparkReadEntry struct { + // updatedAt is the item's updated_at the reads below were taken under. + updatedAt time.Time + + // comments is the item's comment list; commentsAt is when it was read + // (zero: not read). commentsSince is the since that read sent, already + // rounded by cacheableSince; zero means the full list. + comments []*gh.IssueComment + commentsAt time.Time + commentsSince time.Time + + // labelEvents is the item's label timeline; labelEventsAt is when it + // was read (zero: not read). + labelEvents []issueLabelEvent + labelEventsAt time.Time +} + +// unparkReadCache maps "owner/repo#n" to its entry. The zero value is ready +// to use. +type unparkReadCache struct { + mu sync.Mutex + entries map[string]*unparkReadEntry +} + +func unparkReadKey(owner, repo string, number int) string { + return strings.ToLower(owner+"/"+repo) + "#" + strconv.Itoa(number) +} + +// usable reports whether a read taken at readAt under the entry's +// updated_at may stand in for a fresh one for an item listed at updatedAt. +func (e *unparkReadEntry) usable(readAt, updatedAt, now time.Time) bool { + if e == nil || readAt.IsZero() || updatedAt.IsZero() || !e.updatedAt.Equal(updatedAt) { + return false + } + if readAt.Sub(updatedAt) < unparkReadSettle { + return false + } + return now.Sub(readAt) < unparkReadTTL +} + +// entryFor returns the item's entry for updatedAt, resetting it when the +// item moved. The caller holds mu, fills the entry, then calls evictLocked. +func (rc *unparkReadCache) entryFor(key string, updatedAt time.Time) *unparkReadEntry { + if rc.entries == nil { + rc.entries = map[string]*unparkReadEntry{} + } + e := rc.entries[key] + if e == nil || !e.updatedAt.Equal(updatedAt) { + e = &unparkReadEntry{updatedAt: updatedAt} + rc.entries[key] = e + } + return e +} + +// evictLocked drops the entries read longest ago until the cache is within +// unparkReadMaxItems and unparkReadMaxComments. The caller holds mu. +func (rc *unparkReadCache) evictLocked() { + comments := 0 + for _, e := range rc.entries { + comments += len(e.comments) + } + for len(rc.entries) > unparkReadMaxItems || comments > unparkReadMaxComments { + oldestKey, oldest := "", time.Time{} + for k, e := range rc.entries { + at := e.commentsAt + if e.labelEventsAt.After(at) { + at = e.labelEventsAt + } + if oldestKey == "" || at.Before(oldest) { + oldestKey, oldest = k, at + } + } + comments -= len(rc.entries[oldestKey].comments) + delete(rc.entries, oldestKey) + } +} + +func (rc *unparkReadCache) comments(key string, updatedAt, since, now time.Time) ([]*gh.IssueComment, bool) { + rc.mu.Lock() + defer rc.mu.Unlock() + e := rc.entries[key] + if e == nil || !e.usable(e.commentsAt, updatedAt, now) { + return nil, false + } + // A full list answers any since; a since list answers a later one. + // Callers filter on their own cutoff (listIssueCommentsSince). + if !e.commentsSince.IsZero() && (since.IsZero() || since.Before(e.commentsSince)) { + return nil, false + } + return append([]*gh.IssueComment(nil), e.comments...), true +} + +func (rc *unparkReadCache) storeComments(key string, updatedAt, since, readAt time.Time, comments []*gh.IssueComment) { + rc.mu.Lock() + defer rc.mu.Unlock() + e := rc.entryFor(key, updatedAt) + // Keep a full list over a narrower since list read later in the pass. + if !e.commentsAt.IsZero() && e.commentsSince.IsZero() && !since.IsZero() { + return + } + e.comments = append([]*gh.IssueComment(nil), comments...) + e.commentsAt = readAt + e.commentsSince = since + rc.evictLocked() +} + +func (rc *unparkReadCache) labelEvents(key string, updatedAt, now time.Time) ([]issueLabelEvent, bool) { + rc.mu.Lock() + defer rc.mu.Unlock() + e := rc.entries[key] + if e == nil || !e.usable(e.labelEventsAt, updatedAt, now) { + return nil, false + } + return append([]issueLabelEvent(nil), e.labelEvents...), true +} + +func (rc *unparkReadCache) storeLabelEvents(key string, updatedAt, readAt time.Time, events []issueLabelEvent) { + rc.mu.Lock() + defer rc.mu.Unlock() + e := rc.entryFor(key, updatedAt) + e.labelEvents = append([]issueLabelEvent(nil), events...) + e.labelEventsAt = readAt + rc.evictLocked() +} + +func (rc *unparkReadCache) forget(key string) { + rc.mu.Lock() + defer rc.mu.Unlock() + delete(rc.entries, key) +} + +// prune drops every entry of owner/repo whose number is not in listed: the +// item closed or left the list. +func (rc *unparkReadCache) prune(owner, repo string, listed map[int]bool) { + rc.mu.Lock() + defer rc.mu.Unlock() + prefix := strings.ToLower(owner+"/"+repo) + "#" + for k := range rc.entries { + if !strings.HasPrefix(k, prefix) { + continue + } + n, err := strconv.Atoi(strings.TrimPrefix(k, prefix)) + if err != nil || !listed[n] { + delete(rc.entries, k) + } + } +} + +func (rc *unparkReadCache) len() int { + rc.mu.Lock() + defer rc.mu.Unlock() + return len(rc.entries) +} + +// unparkReadScope carries the cache and the pass's clock through one sweep. +// Reads made outside a sweep (no scope on ctx) always go to GitHub. +type unparkReadScope struct { + cache *unparkReadCache + now time.Time +} + +type unparkReadScopeKey struct{} + +func withUnparkReadScope(ctx context.Context, cache *unparkReadCache, now time.Time) context.Context { + return context.WithValue(ctx, unparkReadScopeKey{}, unparkReadScope{cache: cache, now: now}) +} + +func unparkReadScopeFrom(ctx context.Context) (unparkReadScope, bool) { + s, ok := ctx.Value(unparkReadScopeKey{}).(unparkReadScope) + return s, ok && s.cache != nil +} + +// unparkComments is listIssueCommentsSince for an item the sweep listed: +// it reuses the last read while the item's updated_at has not moved. +func (c *Client) unparkComments(ctx context.Context, owner, repo string, issue *gh.Issue, since time.Time) ([]*gh.IssueComment, error) { + number := issue.GetNumber() + scope, ok := unparkReadScopeFrom(ctx) + updatedAt := issue.GetUpdatedAt().Time + if !ok || updatedAt.IsZero() { + return c.listIssueCommentsSince(ctx, owner, repo, number, since) + } + key := unparkReadKey(owner, repo, number) + rounded := cacheableSince(since) + if comments, hit := scope.cache.comments(key, updatedAt, rounded, scope.now); hit { + return comments, nil + } + comments, err := c.listIssueCommentsSince(ctx, owner, repo, number, since) + if err != nil { + return nil, err + } + scope.cache.storeComments(key, updatedAt, rounded, scope.now, comments) + return comments, nil +} + +// unparkLabelEvents is listIssueLabelEvents for an item the sweep listed, +// reused the same way as unparkComments. +func (c *Client) unparkLabelEvents(ctx context.Context, owner, repo string, issue *gh.Issue) ([]issueLabelEvent, error) { + number := issue.GetNumber() + scope, ok := unparkReadScopeFrom(ctx) + updatedAt := issue.GetUpdatedAt().Time + if !ok || updatedAt.IsZero() { + return c.listIssueLabelEvents(ctx, owner, repo, number) + } + key := unparkReadKey(owner, repo, number) + if events, hit := scope.cache.labelEvents(key, updatedAt, scope.now); hit { + return events, nil + } + events, err := c.listIssueLabelEvents(ctx, owner, repo, number) + if err != nil { + return nil, err + } + scope.cache.storeLabelEvents(key, updatedAt, scope.now, events) + return events, nil +} + +// forgetUnparkRead drops the item's cached reads after the sweep wrote to +// it, or failed part-way, so the next pass reads it fresh even if the list +// it starts from has not caught up with the write yet. +func (c *Client) forgetUnparkRead(ctx context.Context, owner, repo string, number int) { + if scope, ok := unparkReadScopeFrom(ctx); ok { + scope.cache.forget(unparkReadKey(owner, repo, number)) + } +} diff --git a/src/pkg/github/issue_unpark_read_cache_internal_test.go b/src/pkg/github/issue_unpark_read_cache_internal_test.go new file mode 100644 index 0000000000..e92a29a1b2 --- /dev/null +++ b/src/pkg/github/issue_unpark_read_cache_internal_test.go @@ -0,0 +1,55 @@ +package github + +import ( + "testing" + "time" + + gh "github.com/google/go-github/v72/github" +) + +// Entries for items that left the open list are dropped after the repo's +// pass, so the cache tracks only what is still listed. +func TestUnparkSweepPrunesItemsNoLongerListed(t *testing.T) { + now := time.Now().UTC() + c, rec, _ := unparkCacheFixture(t, now.Add(-time.Hour)) + unparkCacheSweep(t, c, now) + if got := c.unparkReads.len(); got != 3 { + t.Fatalf("cache holds %d items after the first pass, want 3", got) + } + c.unparkReads.prune("org", "repo", map[int]bool{41: true}) + if got := c.unparkReads.len(); got != 1 { + t.Fatalf("cache holds %d items after pruning to one listed item, want 1", got) + } + _ = rec +} + +// The cache is bounded: past unparkReadMaxItems the oldest read goes. +func TestUnparkReadCacheBounded(t *testing.T) { + var rc unparkReadCache + base := time.Now().Add(-time.Hour) + for i := 0; i < unparkReadMaxItems+10; i++ { + rc.storeComments(unparkReadKey("o", "r", i), base, time.Time{}, base.Add(time.Duration(i)*time.Second), nil) + } + if got := rc.len(); got != unparkReadMaxItems { + t.Fatalf("cache holds %d items, want the bound %d", got, unparkReadMaxItems) + } + if _, ok := rc.entries[unparkReadKey("o", "r", 0)]; ok { + t.Fatal("the oldest read should have been evicted") + } +} + +// A few very long threads cannot grow the cache past the comment bound. +func TestUnparkReadCacheBoundsComments(t *testing.T) { + var rc unparkReadCache + base := time.Now().Add(-time.Hour) + thread := make([]*gh.IssueComment, unparkReadMaxComments/2+1) + for i := 0; i < 3; i++ { + rc.storeComments(unparkReadKey("o", "r", i), base, time.Time{}, base.Add(time.Duration(i)*time.Second), thread) + } + if got := rc.len(); got != 1 { + t.Fatalf("cache holds %d long threads, want only the newest", got) + } + if _, ok := rc.entries[unparkReadKey("o", "r", 2)]; !ok { + t.Fatal("the newest read should be kept") + } +} diff --git a/src/pkg/github/issue_unpark_read_cache_test.go b/src/pkg/github/issue_unpark_read_cache_test.go new file mode 100644 index 0000000000..f120f4b3aa --- /dev/null +++ b/src/pkg/github/issue_unpark_read_cache_test.go @@ -0,0 +1,205 @@ +package github + +import ( + "context" + "testing" + "time" + + gh "github.com/google/go-github/v72/github" +) + +// The un-park sweep reuses an item's comment and label-timeline reads while +// the item's updated_at in the issue list has not moved +// (issue_unpark_read_cache.go). On the knuckle hive these per-item reads +// were the largest charged consumer: 878 comment and 363 timeline reads +// charged in 50 minutes, nearly all of items that had not changed. + +// unparkCacheFixture is one repo with three items the sweep reads every +// pass: a label-approval issue (comments + timeline), a PR at 085 (approve +// read + full comment read) and a plain PR with comments (approve read). +// Nothing in it makes the sweep write. +func unparkCacheFixture(t *testing.T, updated time.Time) (*Client, *unparkServer, []unparkWireIssue) { + t.Helper() + issue := laIssue(40, "triage/accepted", "needs-human") + issue.UpdatedAt = updated.UTC().Format(time.RFC3339) + waiting := unparkWireIssue{ + Number: 41, Title: "waiting PR", User: wireUser{Login: "contributor"}, + Labels: []wireLabel{{Name: PRNeedsDecisionStageLabel}}, + PullRequest: &unparkWirePRLink{URL: "https://example.test/pull/41"}, + Comments: []unparkWireComment{humanComment(4101, "contributor", "thanks for the review")}, + UpdatedAt: updated.UTC().Format(time.RFC3339), CommentCount: 1, + } + plain := waiting + plain.Number, plain.Labels = 42, nil + plain.PullRequest = &unparkWirePRLink{URL: "https://example.test/pull/42"} + plain.Comments = []unparkWireComment{humanComment(4201, "contributor", "rebased")} + issues := []unparkWireIssue{issue, waiting, plain} + c, rec := laServer(t, issues) + // The acceptance label was added by someone the policy does not trust, + // so the label-approval path reads the timeline and does nothing. + rec.timelines[40] = []unparkWireTimelineEvent{labeledAt(1, laReader, "triage/accepted", updated)} + return c, rec, issues +} + +func unparkCacheSweep(t *testing.T, c *Client, now time.Time) *IssueUnparkSweepResult { + t.Helper() + result, err := c.SweepIssueUnparkCommands(context.Background(), IssueUnparkSweepOptions{ + LabelDriven: func(string) bool { return true }, + LabelApproval: laPolicyFn(laPolicy(), true), + PRApproveReply: func(context.Context, string, int, *gh.IssueComment) (string, error) { return "", nil }, + Now: func() time.Time { return now }, + }) + if err != nil { + t.Fatalf("sweep: %v", err) + } + return result +} + +type unparkReadCounts struct { + comments map[int]int + timeline int +} + +func unparkReads(rec *unparkServer) unparkReadCounts { + rec.mu.Lock() + defer rec.mu.Unlock() + out := unparkReadCounts{comments: map[int]int{}, timeline: rec.timelineReads} + for n, k := range rec.commentReads { + out.comments[n] = k + } + return out +} + +func TestUnparkSweepReusesReadsWhileUpdatedAtUnchanged(t *testing.T) { + now := time.Now().UTC() + c, rec, _ := unparkCacheFixture(t, now.Add(-time.Hour)) + + unparkCacheSweep(t, c, now) + first := unparkReads(rec) + // The 085 PR is read twice in a pass (approve read, then the full list). + if first.comments[40] != 1 || first.comments[41] != 2 || first.comments[42] != 1 || first.timeline != 1 { + t.Fatalf("first pass reads = %+v, want comments 40:1 41:2 42:1 and one timeline read", first) + } + + unparkCacheSweep(t, c, now.Add(5*time.Minute)) + second := unparkReads(rec) + for _, n := range []int{40, 41, 42} { + if second.comments[n] != first.comments[n] { + t.Errorf("item %d: comments re-read with updated_at unchanged (%d -> %d)", n, first.comments[n], second.comments[n]) + } + } + if second.timeline != first.timeline { + t.Errorf("timeline re-read with updated_at unchanged (%d -> %d)", first.timeline, second.timeline) + } + if got := len(rec.posted) + len(rec.edited) + len(rec.added) + len(rec.removed); got != 0 { + t.Fatalf("fixture must not write, got posted=%v edited=%v added=%v removed=%v", rec.posted, rec.edited, rec.added, rec.removed) + } +} + +func TestUnparkSweepRereadsWhenUpdatedAtMoves(t *testing.T) { + now := time.Now().UTC() + c, rec, issues := unparkCacheFixture(t, now.Add(-time.Hour)) + + unparkCacheSweep(t, c, now) + first := unparkReads(rec) + + // A comment or label change moves updated_at; issues backs the list. + rec.mu.Lock() + moved := now.Add(-10 * time.Minute).Format(time.RFC3339) + issues[0].UpdatedAt, issues[2].UpdatedAt = moved, moved + rec.mu.Unlock() + + unparkCacheSweep(t, c, now.Add(5*time.Minute)) + second := unparkReads(rec) + if second.comments[40] != first.comments[40]+1 || second.timeline != first.timeline+1 { + t.Errorf("issue 40 moved: want one more comment and timeline read, got %+v -> %+v", first, second) + } + if second.comments[42] != first.comments[42]+1 { + t.Errorf("PR 42 moved: want one more comment read, got %d -> %d", first.comments[42], second.comments[42]) + } + if second.comments[41] != first.comments[41] { + t.Errorf("PR 41 did not move but was re-read (%d -> %d)", first.comments[41], second.comments[41]) + } +} + +func TestUnparkSweepTTLBackstopRereads(t *testing.T) { + now := time.Now().UTC() + c, rec, _ := unparkCacheFixture(t, now.Add(-time.Hour)) + + unparkCacheSweep(t, c, now) + first := unparkReads(rec) + unparkCacheSweep(t, c, now.Add(5*time.Hour)) + if mid := unparkReads(rec); mid.comments[40] != first.comments[40] || mid.timeline != first.timeline { + t.Fatalf("inside the TTL the first read is reused, got %+v -> %+v", first, mid) + } + // Seven hours is past the six-hour unparkReadTTL. + unparkCacheSweep(t, c, now.Add(7*time.Hour)) + second := unparkReads(rec) + if second.comments[40] != first.comments[40]+1 || second.comments[42] != first.comments[42]+1 || second.timeline != first.timeline+1 { + t.Fatalf("past the TTL every item must be read again, got %+v -> %+v", first, second) + } +} + +// A read taken moments after the item's updated_at may predate the change +// the list already shows; it is not trusted for the next pass. +func TestUnparkSweepRereadsJustUpdatedItem(t *testing.T) { + now := time.Now().UTC() + c, rec, _ := unparkCacheFixture(t, now.Add(-30*time.Second)) + + unparkCacheSweep(t, c, now) + first := unparkReads(rec) + unparkCacheSweep(t, c, now.Add(5*time.Minute)) + second := unparkReads(rec) + if second.comments[40] != first.comments[40]+1 || second.timeline != first.timeline+1 { + t.Fatalf("a read inside the settle window must not be reused, got %+v -> %+v", first, second) + } +} + +// Reused comments still get every live and clock-based check on every pass: +// a maintainer granted write access after the first pass is answered on the +// second without a new comment read, and a command that slid out of the +// seven-day window on the cached data is not. +func TestUnparkSweepDecidesFromCachedCommentsWithCurrentState(t *testing.T) { + now := time.Now().UTC() + updated := now.Add(-time.Hour) + run := func(t *testing.T, commandAge time.Duration, second time.Time) (*unparkServer, *IssueUnparkSweepResult) { + t.Helper() + cmd := humanComment(501, "maintainer", "/hive approve") + cmd.CreatedAt = now.Add(-commandAge).Format(time.RFC3339) + cmd.UpdatedAt = cmd.CreatedAt + issue := parkedIssue(50, cmd) + issue.UpdatedAt = updated.Format(time.RFC3339) + server, rec := newUnparkServer(t, "org", "repo", []unparkWireIssue{issue}, map[string]string{"maintainer": "read"}) + c := newTestClient(t, server, "org", []string{"repo"}) + opts := func(at time.Time) IssueUnparkSweepOptions { + return IssueUnparkSweepOptions{LabelDriven: func(string) bool { return true }, Now: func() time.Time { return at }} + } + if res, err := c.SweepIssueUnparkCommands(context.Background(), opts(now)); err != nil || res.Replies != 0 { + t.Fatalf("first pass: a read-only commenter must get nothing, replies=%v err=%v", res, err) + } + rec.mu.Lock() + rec.permissions["maintainer"] = "admin" + rec.mu.Unlock() + res, err := c.SweepIssueUnparkCommands(context.Background(), opts(second)) + if err != nil { + t.Fatalf("second pass: %v", err) + } + if got := unparkReads(rec).comments[50]; got != 1 { + t.Fatalf("comments read %d times, want once (second pass from cache)", got) + } + return rec, res + } + + t.Run("permission granted later is answered", func(t *testing.T) { + rec, res := run(t, time.Hour, now.Add(5*time.Minute)) + if res.Replies != 1 || len(rec.posted[50]) != 1 { + t.Fatalf("want one reply from cached comments, replies=%d posted=%v", res.Replies, rec.posted[50]) + } + }) + t.Run("command outside the window on the current clock is ignored", func(t *testing.T) { + rec, res := run(t, unparkCommandScanWindow-time.Minute, now.Add(5*time.Minute)) + if res.Replies != 0 || len(rec.posted[50]) != 0 { + t.Fatalf("a command older than the window must not be answered, replies=%d posted=%v", res.Replies, rec.posted[50]) + } + }) +} diff --git a/src/pkg/github/label_approval.go b/src/pkg/github/label_approval.go index 5a832e0677..6048162f64 100644 --- a/src/pkg/github/label_approval.go +++ b/src/pkg/github/label_approval.go @@ -326,6 +326,15 @@ func (f labelApprovalFinding) Blocked() bool { return len(f.BlockingParkLabels) // The label must still be on the issue (R11: a withdrawn acceptance is not // acted on) and only the newest `labeled` event for it is considered. func (c *Client) evaluateLabelApproval(ctx context.Context, owner, repo string, issue *gh.Issue, policy LabelApprovalPolicy) (labelApprovalFinding, error) { + return c.evaluateLabelApprovalFrom(ctx, owner, repo, issue, policy, func() ([]issueLabelEvent, error) { + return c.listIssueLabelEvents(ctx, owner, repo, issue.GetNumber()) + }) +} + +// evaluateLabelApprovalFrom is evaluateLabelApproval with the label +// timeline read by loadEvents, which the un-park sweep answers from its +// per-item read cache (issue_unpark_read_cache.go). +func (c *Client) evaluateLabelApprovalFrom(ctx context.Context, owner, repo string, issue *gh.Issue, policy LabelApprovalPolicy, loadEvents func() ([]issueLabelEvent, error)) (labelApprovalFinding, error) { var f labelApprovalFinding labels := issueLabelNames(issue.Labels) var present []string @@ -337,7 +346,7 @@ func (c *Client) evaluateLabelApproval(ctx context.Context, owner, repo string, if len(present) == 0 { return f, nil } - events, err := c.listIssueLabelEvents(ctx, owner, repo, issue.GetNumber()) + events, err := loadEvents() if err != nil { return f, err } diff --git a/src/pkg/github/pr_approve_command.go b/src/pkg/github/pr_approve_command.go index ba224a6fd0..2fc7e15541 100644 --- a/src/pkg/github/pr_approve_command.go +++ b/src/pkg/github/pr_approve_command.go @@ -16,7 +16,7 @@ import ( // a command older than that is ignored anyway, and Hive's reply is always // newer than the command it answers, so the dedup markers stay visible. func (c *Client) replyPRApproveCommand(ctx context.Context, displayRepo, owner, repo string, issue *gh.Issue, cutoff time.Time, reply func(context.Context, string, int, *gh.IssueComment) (string, error)) (bool, error) { - comments, err := c.listIssueCommentsSince(ctx, owner, repo, issue.GetNumber(), cutoff) + comments, err := c.unparkComments(ctx, owner, repo, issue, cutoff) if err != nil { return false, err } diff --git a/src/pkg/github/pr_unpark_decision.go b/src/pkg/github/pr_unpark_decision.go index c00849086c..a7bcf3e227 100644 --- a/src/pkg/github/pr_unpark_decision.go +++ b/src/pkg/github/pr_unpark_decision.go @@ -46,7 +46,7 @@ import ( // never weaker than the review it stands in for. Prose gets no reply. func (c *Client) tryUnparkPullRequest(ctx context.Context, displayRepo, owner, repo string, issue *gh.Issue, cutoff time.Time, opts IssueUnparkSweepOptions, result *IssueUnparkSweepResult) (bool, error) { number := issue.GetNumber() - comments, err := c.listIssueComments(ctx, owner, repo, number) + comments, err := c.unparkComments(ctx, owner, repo, issue, time.Time{}) if err != nil { return false, err }