Repository navigation
feat(chatd): store wait_for_events watches as rows - #340
Conversation
Adds a chatdmig node with one row per thing an events wake-up watches, and the forkdb queries that find the ready watches of the whole deployment in one statement. Nothing reads the table yet; the switch of chatd follows in a stacked change. Refs #293
Move wait_for_events entries from the chat_wakeups.events JSON into chat_wakeup_watches rows. A chatdmig node converts every active and paused events wake-up, marks entries that already ran as delivered, and drops the events column. Delivery sets delivered_at in the transaction of the chat message, after the wake-up row is locked. chat_wakeup_runs stays for cap counting only. Closes #293
@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: 30990176fc
ℹ️ 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".
The wake-up list rebuilds events from watch rows, so a completed or canceled wake-up without rows would list no events after the upgrade. The watch queries read active wake-ups only.
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f3a82bea70
ℹ️ 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".
… chats MarkChatWakeupWatchDelivered clears fired_at and fired_payload, so an open watch is not ready again for the event it just delivered. ListReadyChatWakeupWatches leaves out chats in error with queued messages, as ClaimDueChatWakeups does.
@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". |
# Conflicts: # coderd/database/dbauthz/fork_forkdb_test.go # coderd/database/forkdb/chat_wakeup_watches_test.go # coderd/database/forkdb/querier.go # coderd/database/forkdb/sqlc.yaml
@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". |
Merged as cdf3579 (squash, Gates on head 1137330, with main at 61fcbfd (live on the server at merge time):
Tests I ran on this head:
Not exercised: the backfill has not run on production-like data; it runs at startup on this deploy and converts the live waits of this server. No live |
Closes #293. Builds on #339, which is merged.
What changes
wait_for_eventswake-ups keep their watched entries inchatd.chat_wakeup_watchesrows (added in #339) instead of thechat_wakeups.eventsJSON, and delivery no longer reads run keys to know what an entry already sent.fork_wait_for_events.go,fork_wakeups.go,fork_process_events.go,fork_pull_request_events.go,fork_schedule_wakeup.go): creating a wake-up inserts the wake-up and one watch per entry in one transaction. A poll reads the watch rows; ready child watches come fromListReadyChatWakeupWatches. Process and pull request watches are still polled (chatd: replace the 15 s wake-up claim loop with a waker driven by the chat state machine #294, chatd, agent: the agent reports process exits and matching output instead of chatd dialing it every 15 s #295, chatd: one shared pull request poller with conditional requests, off the wake-up delivery path #296 are out of scope), but read and write watch rows. A delivery ends the claim (which locks the wake-up row), thenMarkChatWakeupWatchDeliveredsetsdelivered_ator bumpsdeliveriesand the output cursor, then inserts the run row, all in the transaction of the chat message. The write checks thedeliveriescount the poll read, so a duplicate delivery loses. A failed send setsfailed_at.chat_wakeup_runsstays for cap counting only (chatd: wake-up caps run five aggregates per delivery, one over a day of organization chat messages #297 is not done). Run keys are nowwatch:<id>:<deliveries>and are never read back.GET /chats/fork/{chat}/wakeupsand the PATCH route rebuild theeventslist of the API response from watch rows. The API shape is unchanged.chat_wakeups.events,childDoneRunKey,pullRequestRunKey,watchedRunKeys, the queriesListChatWakeupRunKeysandListChildChatIDsWithActiveEventsWakeups, and their dbauthz and dbmetrics wrappers.01a1df59-b98e-720a-869b-586a3dcd3799_chat_wakeup_watches_backfillconverts every events wake-up into watch rows, one per entry in order, with baselines inspec. It cancels and deletes nothing. An entry whose run key exists inchat_wakeup_runsgetsdelivered_at; a:failedrun key setsfailed_aton an undelivered entry;process_output:<pid>:<n>keys becomedeliveriesandoutput_cursor. Completed and canceled wake-ups get rows too, so the wake-up list keeps their events; every watch query reads active wake-ups only, so those rows wake nothing. Entries with an unknown type, no target or a deleted child chat are skipped. An active events wake-up that got no watch row is paused with reasoninvalid_eventswhen its events are not a JSON array or hold an entry that is not a well-formedchild_done(so an unknown type or a bad target), as the old decoder paused events it could not read. An empty array or only deleted child chats stays active and completes on the next poll, as before. Paused, completed and canceled wake-ups keep their status. Then it dropschat_wakeups.events.ARCHITECTURE.mddescribes the watch rows, the delivery transaction and the cap-only run keys.Tests
All run locally against Postgres 16, each under
flock ~/.fork-go.lock:go build ./...: okgo test ./coderd/x/chatd/... -count=1: okgo test ./coderd/ -run ForkChatWakeup -count=1: ok (the list test now inserts child, PR and process watch rows out of ordinal order)go test ./coderd/database/dbauthz/ ./coderd/database/dbmetrics/ ./coderd/database/forkdb/... -count=1: okCHATDMIG_TEST_DATABASE_URL=... go test ./coderd/database/migrations/... -count=1: ok, including the newTestWakeupWatchesBackfill(active wake-up with child, process and PR entries, the child one delivered through its run key; plus a paused, a completed and a canceled wake-up that keep their status and get their rows; a non-array and an unknown-only active wake-up that becomepaused:invalid_events; an empty, a deleted-children-only and a paused unknown-only wake-up that keep their status)chatdmig check: exit 0git grep -n 'childDoneRunKey\|ListChatWakeupRunKeys\|watchedRunKeys': no outputgofmt -lon the touched packages: onlycoderd/x/chatd/fork_prompt_load_internal_test.go, which this PR does not touch (it is unformatted onmain)Negative controls, reverted afterwards:
MarkChatWakeupWatchDeliveredcall replaced by a no-op, 8 tests fail:TestWakeupEventsArchivedAndMissingChildren,TestWakeupEventsRetriesFailedSend,TestWakeupEventsDeliversOncePerChild,TestWakeupPullRequestAfterChild,TestWakeupEventsProcessExit,TestWakeupEventsProcessOutput,TestWakeupPullRequestEvents,TestWakeupEventsProcessOutcomes.DROP COLUMN,TestWakeupWatchesBackfillfails:"[]" should have 4 item(s), but has 0.invalid_eventspause removed from the backfill,TestWakeupWatchesBackfillfails:expected "paused:invalid_events:0:" actual "active::0:2026-11-01 12:01:00+00".Not verified
wait_for_eventscall against a real workspace agent or GitHub. Those paths ran through the existing fakes.make lintwas not run. CI runs the linters.