Repository navigation
feat(coderd/x/chatd): deliver due chat wake-ups to idle chats - #91
Conversation
Adds the fork-chat-wakeups experiment and the storage for chat wake-ups: a chatdmig node with chatd.chat_wakeups and chatd.chat_wakeup_runs, forkdb queries to create, list, pause, cancel and claim due wake-ups with a lease (FOR UPDATE SKIP LOCKED), run records unique per occurrence and daily run counts, with their dbauthz and dbmetrics wrappers. Refs #11.
…ke-up InsertChatWakeupRun now selects chat_id and organization_id from the wake-up and authorizes on the wake-up's chat, so a caller cannot record a run of one chat's wake-up against another chat. Adds the chatd architecture TODO for the new state.
chat_wakeups gets organization_id, copied from the chat by InsertChatWakeup, so organization-scoped lists and caps need no join. A wake-up run now takes its organization from the wake-up.
…chema # Conflicts: # coderd/database/dbauthz/fork_forkdb.go # coderd/database/dbmetrics/fork_forkdb.go # coderd/database/forkdb/forkdbmock/forkdbmock.go # coderd/database/forkdb/querier.go # coderd/database/forkdb/sqlc.yaml
… hook denial Start the worker to prove the hook line runs the wake-up loop on start and on each poll tick, cover all three busy statuses, an expired wake-up, a hook denial that pauses the schedule, and assert that concurrent workers never queue a duplicate prompt behind the turn the first delivery starts.
…oder into mike/chat-wakeups-2-worker
|
@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: 389cbbf5ac
ℹ️ 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".
Insert the delivered run and end the claim inside SendMessage's transaction, so a chat that turned busy, an occurrence that already ran, or a canceled or taken-over claim rolls the message back. Hook dispatch failures no longer pause the wake-up, and each claimed row uses the time it is processed.
|
@codex review
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5e110fc4b9
ℹ️ 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".
…occurrences Busy chats are no longer claimed, so their rows cannot fill the claim batch. A full queue keeps the occurrence due like any other busy race, and a failed send records its run only after the worker proves it still holds the lease.
|
@codex review
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8f5e38e692
ℹ️ 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 paused chat, or an errored chat with a non-empty queue, would queue the wake-up prompt, so its due rows filled the claim batch on every poll. Delivery times now also count the time the claim query took.
|
@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". |
…orker # Conflicts: # coderd/database/forkdb/chat_wakeups.sql.go # coderd/database/forkdb/querier.go # coderd/database/forkdb/queries/chat_wakeups.sql
|
@codex review
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: da5552642a
ℹ️ 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".
…NoKeyUpdate The busy-chat filter in ClaimDueChatWakeups skipped an archived errored chat with a queued message forever, so the worker never canceled its wake-up. Archived chats are now always claimed. Also restores the LockChatForNoKeyUpdate querier method that the merge with main dropped.
|
@codex review
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 32c3d8ba31
ℹ️ 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 The open finding on 32c3d8b was declined with a reply (a hook denial pausing the wake-up is intended).
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 32c3d8ba31
ℹ️ 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".
…nd schedule from admission A wake-up that resumes a finished child now carries the message_agent resume marker, so the family live subagent limit applies. The next run is computed from the admission time, so a hook that delays admission past the next slot does not leave an already-due schedule. A failed run is recorded in the transaction that ends the claim.
|
@codex review
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6e91a81543
ℹ️ 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". |
Closing and reopening only to make GitHub recompute the merge ref against current main ( |
The merge ref now points at current main ( |
Closing and reopening only to make GitHub recompute the merge ref against current main ( |
The merge ref now points at current main ( |
Merge evidence for head
|
Part of #11. Builds on #82, which is merged, and targets
main.Adds the worker that delivers chat wake-ups behind
fork-chat-wakeups. Nothing creates wake-ups yet: theschedule_wakeuptool and/loopcome in the next PR.How delivery works
chatWorker.Startgets one hook line,w.startForkLoops(workerCtx), which starts the wake-up loop only when the experiment is on. Every replica runs it. Upstream files get only that line, the 5-lineforkAdmitcall inSendMessage, and the unexported option field.ClaimDueChatWakeups,FOR UPDATE SKIP LOCKED), so two replicas never claim the same row.eventswake-ups stay active and are retried a minute later; PR4 delivers them.running,interruptingorrequires_action), apausedchat, or anerrorchat with a non-empty queue. The claim query skips its wake-ups, so busy rows never fill a batch and starve idle chats. Archived chats are always claimed, so their wake-ups get canceled. The wake-up stays due with itsnext_run_atunchanged, and the next tick after the turn ends delivers it. A wake-up never queues behind, steers or interrupts a turn (PRD R4.2).SendMessageas the chat owner. A fork hook (SendMessageOptions.forkAdmit, 5 lines inSendMessage) runs inside the send's transaction, under the chat lock. It inserts thedeliveredrun, keyed by the scheduled time, and ends the claim. If any of those steps fails, the message rolls back with them:hook_denied), an archive race cancels it, and anything else (including a hook dispatch failure, which is transient) marks the runfailedand moves on to the next slot.message_agent. A refused resume records afailedrun and the next occurrence tries again.max_runsandexpires_atcomplete the wake-up.coderd/x/chatd/ARCHITECTURE.mdgets one TODO line in the chat worker section for the owner to describe this loop.Validation
go build ./coderd/... ./enterprise/...,go vet ./coderd/x/chatd/, andgo test -run 'Wakeup|TestWorker|TestForkQuerier|FamilyLock'overcoderd/x/chatd,coderd/database/forkdbandcoderd/database/dbauthzagainst Postgres. The wake-up tests cover the interval without replay, busy chats, two concurrent workers delivering once, takeover after an expired lease, the end of a schedule, the loop running in a started worker, next-run math, running, paused and errored-with-queue chats not starving an idle one, an archived errored chat with a queue getting its wake-up canceled, six admission races staged from a user prompt hook (the chat turns busy, the queue fills, the claim is paused, the hook dispatch fails, the lease is taken over before a failure, and admission is held past the next slot), and a wake-up held back by the family live subagent limit. Each race test fails when its fix is reverted, and so do the takeover test when the in-transaction run conflict is ignored and the family limit test when the resume marker is dropped.next_run_atmoves by exactly one interval.eventsrows, delay,max_runs,requires_action, a member-owned chat, and cron in Europe/Warsaw.invalid_cron, because the spec is parsed only when computing the next run. The next PR validates cron when the wake-up is created.Size
About 330 lines of worker code, 9 lines in upstream files, one TODO line, and about 600 lines of tests.