Skip to content

chatd: one shared pull request poller with conditional requests, off the wake-up delivery path #296

Closed

Description

@ibetitsmike

Filed by Xum (an AI coding agent) on behalf of @ibetitsmike. Part of #311. Line numbers are at 607a95b.

Summary

A pull request watch re-reads the pull request from GitHub once a minute, per wake-up, on the wake-up worker's delivery goroutine. Each read is up to 7 unconditional API requests on the chat owner's token, including one to /user whose answer never changes. Two chats that watch the same pull request read it twice.

Webhooks are not an option for this deployment (the daily server is reachable on loopback only), so polling stays. It should be one poller, off the delivery path, that uses conditional requests.

Evidence

  • fork_pull_request_events.go:29-31: pullRequestPollInterval = time.Minute.
  • fork_wait_for_events.go:425-452: the read runs inside deliverEventsWakeup, "at most one read per tick", bounded by pullRequestReadTimeout = 30 s (fork_pull_request_events.go:40-42).
  • fork_github_activity.go:107-114: every poll starts with GET /user.
  • FetchPullRequestActivity in the same file then reads the pull request, reviews, issue comments, review comments, and, when checks are watched, check runs and the combined status, each paged up to 10 pages of 100.
  • No request sends If-None-Match, so every poll counts against the owner's rate limit: about 420 requests per hour per watched pull request.
  • Upstream already polls pull request state per chat for the diff status (worker.go:24-25), so a chat that watches its own pull request is polled by two independent loops.

Proposed design

New file coderd/x/chatd/fork_pull_request_poller.go: one poller goroutine per replica.

  1. Work list from the database. Each tick (30 s), one query lists the distinct (pull_request_url, owner) pairs of open pull_request watches (index chat_wakeup_watches_open_pr_idx, chatd: store wake-up watches as rows instead of an events JSON and run keys #293) whose spec->>'next_poll_at' is due. A lease per URL is not needed: a duplicate read by two replicas is harmless because step 4 is idempotent.
  2. One read per pull request, not per wake-up. All watches of the same URL and owner share the result.
  3. Conditional requests. Store the ETag of each endpoint in the watch spec and send If-None-Match. GitHub answers 304 without charging the rate limit. Cache the viewer login for the life of the watch; drop the per-poll GET /user.
  4. Write a fact, then poke. When the comparison with the baseline finds new activity, set fired_at and fired_payload (the summary text built today by pullRequestWakeupText) on the watch rows, and forkWaker.Poke(chat_id). The poller never sends a chat message.
  5. Backoff in the row. Rate limit and error handling keep today's rules (fork_pull_request_events.go:227-252) but write next_poll_at to the watch spec instead of rescheduling the wake-up.
  6. Bounded concurrency. At most 4 reads in flight per replica, each with the 30 s deadline.

Removed from the delivery path: pullRequestWakeupText as a blocking call, pullRequestReadFailed rescheduling of the wake-up, the half-lease guard for pull request reads.

Acceptance criteria

  • A test with a fake GitHub server asserts that a poll with unchanged state sends If-None-Match on every endpoint and that /user is requested once per watch, not once per poll.
  • Two chats watching one pull request with the same owner cause one set of requests per poll.
  • A fake server that hangs for 30 s does not delay delivery of an interval wake-up of another chat.
  • New activity produces exactly one message per watching chat.

Not in scope

Reusing upstream's chat_diff_statuses refresh as the event source. It does not carry reviews, comments or checks, and changing it means editing upstream code.

Depends on

#293, #294.

Activity

  1. added
    enhancement
    performance
    on Nov 14, 2026
  2. ibetitsmike commented on Nov 16, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    Started: workstream chat 4ace9527 (Opus 5.5, high) on branch mike/issue-296-pr-poller. It reports back to the lead for verification and merge.

  3. ibetitsmike commented on Nov 16, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    PR #363 merged as 3a63653 at 09:36:20Z. The pull request poller now reads each pull request once per replica, off the wake-up path, and shares one read across the watches of the same owner. Gate evidence: #363 (comment)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions