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/fixed-issue-unpark-skip-unchanged.md
Original file line number Diff line number Diff line change
@@ -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).
5 changes: 5 additions & 0 deletions src/pkg/github/cacheable_since_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
3 changes: 3 additions & 0 deletions src/pkg/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 37 additions & 2 deletions src/pkg/github/issue_unpark_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand All @@ -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)
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -1711,18 +1743,21 @@ 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)
}
stale, moved = notices, true
}
}
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)
}
Expand Down
22 changes: 16 additions & 6 deletions src/pkg/github/issue_unpark_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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:
Expand Down
Loading
Loading