Skip to content

feat(chatd): store wait_for_events watches as rows - #340

Merged
ibetitsmike merged 10 commits into
mainfrom
mike/issue-293-watch-rows-switch
Nov 15, 2026
Merged

ibetitsmike merged 10 commits into
mainfrom
mike/issue-293-watch-rows-switch

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Closes #293. Builds on #339, which is merged.

What changes

wait_for_events wake-ups keep their watched entries in chatd.chat_wakeup_watches rows (added in #339) instead of the chat_wakeups.events JSON, and delivery no longer reads run keys to know what an entry already sent.

  • Tool and worker (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 from ListReadyChatWakeupWatches. 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), then MarkChatWakeupWatchDelivered sets delivered_at or bumps deliveries and the output cursor, then inserts the run row, all in the transaction of the chat message. The write checks the deliveries count the poll read, so a duplicate delivery loses. A failed send sets failed_at.
  • chat_wakeup_runs stays 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 now watch:<id>:<deliveries> and are never read back.
  • The "already watched by wake-up X" guard reads open watch rows.
  • GET /chats/fork/{chat}/wakeups and the PATCH route rebuild the events list of the API response from watch rows. The API shape is unchanged.
  • Removed: chat_wakeups.events, childDoneRunKey, pullRequestRunKey, watchedRunKeys, the queries ListChatWakeupRunKeys and ListChildChatIDsWithActiveEventsWakeups, and their dbauthz and dbmetrics wrappers.
  • New chatdmig node 01a1df59-b98e-720a-869b-586a3dcd3799_chat_wakeup_watches_backfill converts every events wake-up into watch rows, one per entry in order, with baselines in spec. It cancels and deletes nothing. An entry whose run key exists in chat_wakeup_runs gets delivered_at; a :failed run key sets failed_at on an undelivered entry; process_output:<pid>:<n> keys become deliveries and output_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 reason invalid_events when its events are not a JSON array or hold an entry that is not a well-formed child_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 drops chat_wakeups.events.
  • ARCHITECTURE.md describes 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 ./...: ok
  • go test ./coderd/x/chatd/... -count=1: ok
  • go 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: ok
  • CHATDMIG_TEST_DATABASE_URL=... go test ./coderd/database/migrations/... -count=1: ok, including the new TestWakeupWatchesBackfill (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 become paused:invalid_events; an empty, a deleted-children-only and a paused unknown-only wake-up that keep their status)
  • chatdmig check: exit 0
  • git grep -n 'childDoneRunKey\|ListChatWakeupRunKeys\|watchedRunKeys': no output
  • gofmt -l on the touched packages: only coderd/x/chatd/fork_prompt_load_internal_test.go, which this PR does not touch (it is unformatted on main)

Negative controls, reverted afterwards:

  • With the MarkChatWakeupWatchDelivered call replaced by a no-op, 8 tests fail: TestWakeupEventsArchivedAndMissingChildren, TestWakeupEventsRetriesFailedSend, TestWakeupEventsDeliversOncePerChild, TestWakeupPullRequestAfterChild, TestWakeupEventsProcessExit, TestWakeupEventsProcessOutput, TestWakeupPullRequestEvents, TestWakeupEventsProcessOutcomes.
  • With the backfill node reduced to the DROP COLUMN, TestWakeupWatchesBackfill fails: "[]" should have 4 item(s), but has 0.
  • With the invalid_events pause removed from the backfill, TestWakeupWatchesBackfill fails: expected "paused:invalid_events:0:" actual "active::0:2026-11-01 12:01:00+00".

Not verified

  • No run against a copy of the production database. The backfill was exercised only with the rows the migration test seeds.
  • No live wait_for_events call against a real workspace agent or GitHub. Those paths ran through the existing fakes.
  • make lint was not run. CI runs the linters.

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
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-15T14:47:08.440198Z 1137330 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread coderd/x/chatd/fork_chat_todos.go Resolved
Comment thread coderd/fork_chat_wakeups.go Resolved
… 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.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: f60ec61427

ℹ️ 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".

# 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
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 1137330888

ℹ️ 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".

@ibetitsmike
ibetitsmike merged commit cdf3579 into main Nov 15, 2026
10 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-293-watch-rows-switch branch November 15, 2026 15:24
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged as cdf3579 (squash, --match-head-commit). Top of the #293 stack; #339 merged earlier as 61fcbfd.

Gates on head 1137330, with main at 61fcbfd (live on the server at merge time):

  • Every check run on the head passed. fork-ci run 43725887944 ran on this exact head and passed.
  • Codex receipt on the head: feat(chatd): store wait_for_events watches as rows #340 (comment) ("Didn't find any major issues", Reviewed commit: 1137330888).
  • Review threads: 0 unresolved. Earlier Codex P2s were fixed (completed and canceled wake-ups keep their events as watch rows) or declined with a reason on the thread.
  • Behind main: 0.
  • Opus review, read-only, of f60ec61: no P1. Old run keys map exactly onto delivered_at, failed_at, deliveries and output_cursor; delivery checks the lease and the deliveries count in the message transaction. One P2 (an active wake-up with no convertible entry would complete silently) was fixed in 1137330: the backfill pauses it with invalid_events. Opus delta review of 1137330: mergeable, no P1 or P2. Nits not acted on: the comments say the pause matches the old decoder, but the new rule also pauses a list of only unknown types, which the old reader completed (safer, not identical); the unknownOnly fixture mixes two cases.

Tests I ran on this head:

  • go test -count=1 -v -run 'TestWakeup|TestWaitForEvents|TestScheduleWakeupTool|TestNextWakeupRun' ./coderd/x/chatd/: 40 pass, 0 fail.
  • go test -count=1 -v -run TestWakeupWatchesBackfill ./coderd/database/migrations/chatdmig/: pass.
  • go test -count=1 -v -run 'TestForkChatWakeup|TestForkChatOrganizationWakeupCaps|TestForkChatPullRequestActivity' ./coderd/: 7 pass.
  • Negative control: making the backfill never find a delivered run key (r.run_key = k.run_key || ':nope') makes TestWakeupWatchesBackfill fail. Restored, it passes.
  • The workstream also ran go build ./..., the chatd, forkdb, dbauthz, dbmetrics and migrations packages, and chatdmig check, all ok, plus its own negative controls (no-op MarkChatWakeupWatchDelivered fails 8 chatd tests; removing the pause fails the backfill test).

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 wait_for_events call against a workspace agent or GitHub; those paths ran through fakes.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

chatd: store wake-up watches as rows instead of an events JSON and run keys

1 participant