Repository navigation
feat(coderd): poll pull requests once per replica off the wake-up path - #363
Conversation
A pull request poller per replica claims the due pull_request watches, reads each pull request once per chat owner with conditional requests, keeps the token's login in the watch spec, and fires watches with their message so the waker only sends stored text. Closes #296
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: fb58857985
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7a65683d91
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 62d68ea19a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 62d68ea19a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
@codex review |
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…r-poller-4ace9527
@codex review |
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
The poller sets the retry from the claim time, which is the pass start plus the elapsed time. Postgres rounds that to the microsecond, so a pass of half a microsecond or more put the retry just past wantNext and the poll at wantNext read nothing.
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
A fired watch whose wake-up is not delivered yet stays claimable. Each read replaces fired_payload and keeps fired_at, so a chat that stays busy gets the activity up to the last read, and the waker is poked only on the first fire. The claim already skips watches of ended wake-ups. A test seeds 10,000 ended wake-ups and checks that EXPLAIN uses the partial indexes and no sequential scan. Add TestWakeupReadFits for the delivery read budget.
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7fccedd7ee
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…very A fired pull request watch is read on until its delivery, so a final poll that sends its stored text before the poller read through the expiry could drop the activity since the read before. The final poll now waits for that read, fired or not, within pullRequestFinalRetryWindow.
…r-poller-4ace9527
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0ab3deb38c
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
@codex review |
|
Codex Review: Didn't find any major issues. Nice work! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Merged at head Gates on this exact head:
Tests I ran on
Review notes: an independent Opus review of 871024e found no P1. Its two regression P2s (stale fired text, a final poll that could send before the last read) were fixed in 7fccedd and a00c0a8, and the controls above cover both. I read the delta 871024e..0ab3deb: the claim no longer skips fired watches, the poll record keeps the first Not exercised: a running server with real GitHub pull requests, several replicas sharing one claim, and GitHub rate limits. |
Closes #296
What changed
coderd/x/chatd/fork_pull_request_poller.go: one pull request poller per replica, started next to the waker instartForkLoops, so only withfork-chat-wakeups. Every 15 seconds it claims duepull_requestwatches (ClaimDuePullRequestWatches: undelivered, fired or not, active wake-up, unarchived chat,spec.next_poll_atdue, not yet read through the expiry;FOR UPDATE SKIP LOCKEDplus a 5 minute lease innext_poll_at). Rows are grouped by (pull request URL, chat owner) and each group is one read. When a claim batch is full, its last group is held back and joined to the next claim, so a group split by the row limit is still read once. Every claim of a pass takes the watches due at the pass start, so a pass never claims a watch it already read; the lease of each claim starts from the pass start plus the elapsed time. Within a group, watches that watch checks are read apart from those that do not, so a failed checks read does not hold back the others. At most 4 reads run at once, each with a 30 second deadline.pullRequestWakeupTextis now pure). New activity writesfired_atandfired_payload({"text": ...}) withRecordPullRequestWatchPolland pokes the waker for that chat. Every read movesspec.next_poll_ata minute on, capped at the expiry. A fired watch is read on until its delivery: each read with activity replacesfired_payloadand keeps the firstfired_at, and only the first fire pokes the waker. So a chat that stays busy gets the activity up to the last read, in one message. A failed read backs off inspec.next_poll_at(rate limit:RetryAfter, at least a minute; other errors: 5 minutes).deliverEventsWakeupsends the stored text of a fired watch.ListDueChatWakeupsnow treats a firedpull_requestwatch as ready, likechild_done. The final poll at the expiry waits (15 second retries, withinpullRequestFinalRetryWindow) until the poller read through the expiry (spec.polled_at).gitprovider.FetchPullRequestActivitytakes aviewerLoginand skipsGET /userwhen it is set, and returnsViewerLogin. The baseline read ofwait_for_eventsstores it inspec.viewer_login, so polls do not read/user. coderd binds the stored value to the external auth link (<link updated_at>:<login>), so a relink or token refresh reads/useragain. coderd also no longer validates an unexpired token on each read (that validation was another/userrequest per poll); an expired token is still refreshed.If-None-Matchfrom its in-memory ETag cache. The poller keeps the request URLs stable (samesincefor a watch set), so an unchanged pull request answers 304 on every endpoint. The ETags live in replica memory, so the first poll after a restart reads in full.ARCHITECTURE.md: the fork-owned pull request polling bullets now describe the poller in final prose, and the fork-owned sentences that put GitHub reads on the delivery path are corrected. No upstream line changed.Tests
New
fork_pull_request_poller_internal_test.goruns a fake GitHub (TLS httptest, ETag and 304) through the real provider:TestPullRequestPollerConditionalRequests: 3 polls of an unchanged PR sendIf-None-Matchon all 4 endpoints and get 304;/useris requested once, by the baseline.TestPullRequestPollerSharesReads: two chats of one owner watching one PR cost one request per endpoint per poll.TestPullRequestPollerHangDoesNotDelayDelivery: with the waker at one delivery slot and a GitHub read hanging, an interval wake-up of another chat is delivered while the read still hangs.TestPullRequestPollerSharesReadsAcrossClaims: 27 watches of one PR and owner (batch size 25 plus 2) cost one request per endpoint, and every watch is marked polled.TestPullRequestPollerClaimsOncePerPass: 26 watches whose reads each advance the clock 2 minutes are each read once in one pass.TestPullRequestPollerChecksFailureIsolated: a failed checks read backs off the checks watch, and the comment watch of the same pull request is still polled.TestPullRequestPollerDeliversOncePerChat: a new comment sends exactly one message to each of two watching chats through the poke, with no clock advance; the next poll makes no requests.TestWakeupPullRequestFiredTextStaysFresh: the watch fires on comment 1 while the chat runs. Comment 2 arrives and the PR merges, and the next poll reads again. When the chat waits, one message is sent with both comment URLs andstate: merged, and no read follows the delivery.TestWakeupPullRequestFinalPollWaitsForFiredRead: the watch fires while the chat is busy, and another comment arrives before the expiry. The waker runs after the expiry before the poller and sends nothing. After the poller reads through the expiry, one message has both comments.TestClaimDuePullRequestWatchesPlan(forkdb): 10,000 chats with ended wake-ups (completed, canceled, paused) and undelivered watches, plus one live watch, thenANALYZE. The claim plan has no Seq Scan onchat_wakeups,chatsorchat_wakeup_watches, and the claim returns only the live watch.TestWakeupReadFits: no deadline fits,2*processReadTimeout+1mfits,2*processReadTimeout-1sdoes not, and2*processReadTimeout <= wakeupDeliveryTimeout.Claim plan from
TestClaimDuePullRequestWatchesPlan(key lines). The existing partial indexes serve it, so no migration is needed:Existing pull request wake-up tests now run the poller and then the waker.
TestWakeupPullRequestReadDeadlineandTestWakeupPullRequestReadNearDeliveryDeadlineare removed because the delivery no longer reads pull requests.Commands run (all under
flock ~/.fork-go.lock):go test ./coderd/x/chatd/ ./coderd/externalauth/... ./coderd/database/forkdb/... -count=1: okgo test ./coderd/database/dbauthz/ -run 'TestForkQuerier|TestMethodTestSuite' -count=1: okgo test ./coderd/ -run PullRequest -count=1: okgo test ./coderd/ -run 'PullRequest|ExternalAuth' -count=1andgo test ./coderd/externalauth/... -count=1on b0f1eb2: ok.TestForkChatPullRequestActivityViewer: one/userand no validation across two reads of one link, a second/userafter a relink../coderd/database/forkdb/generate.shafter merging main: no drift.go test ./coderd/x/chatd/ -count=1on 51a4969: ok.-run TestPullRequestPollerClaimsOncePerPass -count=30: ok.generate.sh: no drift.go build ./coderd/...: ok.go test -count=1 ./coderd/x/chatd/ ./coderd/database/forkdb/... ./coderd/externalauth/gitprovider/: ok. dbauthz-run 'TestForkQuerier|TestMethodTestSuite': ok../coderd/ -run PullRequest: ok.golangci-lint run --fix=false ./coderd/x/chatd/...: nothing in changed lines.go test -count=1 ./coderd/x/chatd/ ./coderd/database/forkdb/... ./coderd/externalauth/gitprovider/: ok. dbauthz-run 'TestForkQuerier|TestMethodTestSuite': ok../coderd/ -run PullRequest: ok.go test -race -count=10 ./coderd/x/chatd/ -run 'TestWakeupPullRequest|TestPullRequestPoller|TestWakeupReadFits': ok.generate.sh: no drift.go veton the changed packages,gofmt -l: clean.golangci-lint run --fix=falseon the changed packages reports nothing in changed lines.Negative controls (each reverted afterwards):
""as viewer login:ConditionalRequestsandSharesReadsfail (/userper poll).SharesReadsfails (8 requests instead of 4).nowinstead of the baseline:ConditionalRequestsfails (comments endpoint not conditional),DeliversOncePerChatfails.SharesReadsAcrossClaimsfails (requests repeated for the split group).ForkChatPullRequestActivityViewerfails (1/userread instead of 2 after a relink).RefreshTokencalled for an unexpired token:ForkChatPullRequestActivityViewerfails (2 validations instead of 0).ClaimsOncePerPassfails (the pass re-claims read watches until cancelled).ChecksFailureIsolatedfails (the comment watch is not polled).fired_at IS NULL:FiredTextStaysFreshfails (the fired watch is not read again).fired_payloadkept from the first fire:FiredTextStaysFreshfails (comment 2 is missing from the message).w.status = 'active':ClaimDuePullRequestWatchesPlanfails (Seq Scans onchats,chat_wakeup_watchesandchat_wakeups).FinalPollWaitsForFiredReadfails (the waker sends the old text).Review answers (P2)
pullRequestPollerBatchnow says a batch and its carried group read well within the lease unless most reads hitpullRequestReadTimeout. A read past the lease can be repeated by another replica. That costs requests only:fired_atis kept byCOALESCE, the poke fires only on the first fire, and delivery is guarded by the deliveries row, so no second message is sent.viewer_loginafter a GitHub login rename with a non-expiring token: accepted./useris read again on a relink or token refresh. Until then the stale login only affects the filter of the user's own activity.spec.polled_atby the waker, with no GitHub read. The poller still reads at most once a minute, and backs off on failures.wakeupReadFitscoverage: restored byTestWakeupReadFits.TestClaimDuePullRequestWatchesPlanpins the index plan above.TestWakeupPullRequestFiredTextStaysFresh.pullRequestFinalRetryWindow. SeeTestWakeupPullRequestFinalPollWaitsForFiredRead.polled_atpast the expiry, written in the same update as the text.Not verified