Repository navigation
feat(chatd): deliver chat wake-ups from a state-driven waker - #344
Conversation
Each chatd Server gets one waker. Chat state updates poke it in-process, it arms a timer at the earliest next_run_at and sweeps every minute. A plain SELECT lists due wake-ups, and each delivery locks the chat and then the wake-up row (FOR UPDATE SKIP LOCKED, status active) and re-checks it is unchanged. This replaces the 15 second claim loop and its lease. Events wake-ups still poll every 15 seconds. Refs #294
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. |
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! 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 delivery that finds its chat busy writes nothing. Poking the chat again would list the same wake-up and spin until the chat turns idle; the end of the turn pokes instead. Refs #294
@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". |
…iven-waker-b9396e7f # Conflicts: # coderd/database/forkdb/forkdbmock/forkdbmock.go # coderd/database/forkdb/querier.go
Merged main in to clear a conflict in generated forkdb files. @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". |
The final SELECT of ListDueChatWakeups filtered chat_wakeups by an OR of two IN lists, which Postgres answers with a sequential scan. Join the due and upcoming ids instead, and extend the plan test to the sweep.
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 80ab2ef7c8
ℹ️ 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".
A replica of the previous version may still hold a claim on a wake-up during a deploy. Clearing its lease in UpdateChatWakeupRun makes that replica's FinishChatWakeupClaim match no row, so it cannot overwrite the result of the new waker.
@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". |
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! 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 as 7c10f9f at 17:55:06Z. Gates on head 3380a23:
Not exercised: no live deploy with active wake-ups has run on this code; the coderd Note: #336 merged 3 s after this PR (17:55:09Z) from another chat, so both land in one deploy. |
Part 1 of 2 for #294. The top PR (
mike/issue-294-waker-events) moves events wake-ups onto the due query and drops the lease column.What changed
forkWakerper chatd Server (coderd/x/chatd/fork_waker.go), behindfork-chat-wakeups.Newwraps the Server pubsub (withForkWaker, one hook line inchatd.go). Eachchat:update:{id}publish pokes the waker with that chat. chatstate publishes after the outermost commit, so a poke never sees uncommitted state. Wake-up controls and finished deliveries poke directly. Pokes are in-process only.next_run_at, plus a 60 second sweep.ListDueChatWakeups: one plain SELECT that skips busy chats and also returns the next upcoming row for the timer.LockActiveChatWakeup(FOR UPDATE SKIP LOCKED,status = 'active'), then a re-check thatrun_count,next_run_atandupdated_atare unchanged. Otherwise it writes nothing (errWakeupNotDue).wakeupLoop,ClaimDueChatWakeups,FinishChatWakeupClaimand the lease reads. A busy chat now writes nothing; the end of its turn pokes the waker.SetChatWakeupStatuslocks chat then wake-up, the same order as deliveries.coderd/x/chatd/ARCHITECTURE.md: new "Wake-up delivery" section.Deploy safety alone
lease_expires_atstays and is ignored. No wake-up changes status on deploy.Design note
The brief asked for a
forkAfterCommitfield onChatMachine. I used a pubsub wrapper instead. chatstate already publisheschat:updateafter the outermost commit. A machine field needs a hook line at each of about 10 construction sites, and in nested transactions it would fire before the outer commit.Tests
fork_waker_internal_test.go(quartz mock clock):TestWakerDeliversWhenTurnEnds: busy chat across a due interval slot, the turn ends, delivery with no clock advance.TestWakerTimerDeliversAtRunTime.TestWakerSweepDeliversWithoutPokes: no pokes, delivery after one sweep.TestWakerTwoWakersDeliverOnce: two wakers on one DB, one message.TestWakerPausedBeforeDelivery: paused between the list and the delivery tx, no message.TestWakerWaitingWakeupsCostOneStatementPerSweep: 50 waiting wake-ups over 10 sweeps, 10 statements.TestWakerRunsInStartedWorker.PausedDuringSend,ChangedDuringSendThenFailed,ChatBusyAfterListAtCapStaysDue,TestWakeupPullRequestReadNearDeliveryDeadline.go test ./coderd/x/chatd/... -count=1: okgo test ./coderd/x/chatd/ -race -run 'TestWak|TestWaitForEvents|TestScheduleWakeupTool|TestNextWakeupRun' -count=3: okgo test ./coderd/database/forkdb/ -count=1: okgo test ./coderd/database/dbauthz/ -run 'TestMethodTestSuite|Fork' -count=1: okgo test ./coderd/ -run 'TestForkChatWakeup.*|TestForkChatLoopErrors|TestForkChatOrganizationWakeupCaps' -count=1: okmake lint/go/new-code: 0 issues. Fullgolangci-lintshows no findings in changed lines.lint/markdown,lint/emdash,lint/architecturepass.TestWakerDeliversWhenTurnEndsfails.lockDueWakeupdisabled:TestWakeupAdmissionRaces/ChangedDuringSendThenFailedfails.Not verified