Skip to content

feat(chatd): make events wake-ups due when a watched child is done - #345

Merged
ibetitsmike merged 15 commits into
mainfrom
mike/issue-294-waker-events
Nov 15, 2026
Merged

ibetitsmike merged 15 commits into
mainfrom
mike/issue-294-waker-events

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Part 2 of 2 for #294. It builds on #344, merged into main as 7c10f9f, which added the state-driven waker. This PR is now based on main. This part takes wait_for_events child watches off the 15 second poll.

Closes #294

What changes

  • New chatdmig node 01a1e008-..._chat_wakeup_ready_watches:
    • It creates the view chat_wakeup_ready_watches, which holds the one readiness rule for watches. The rule was moved unchanged from ListReadyChatWakeupWatches: the watch is open, its wake-up is active, its chat would not queue a message, and either an event fired or the child is done.
    • It drops chat_wakeups.lease_expires_at. Nothing has read the column since feat(chatd): deliver chat wake-ups from a state-driven waker #344 removed the claim.
    • It does not change, cancel or delete any wake-up. Each one keeps its status and its next run time.
  • ListReadyChatWakeupWatches reads the view.
  • ListDueChatWakeups makes an events wake-up due before its expiry when it has a ready child_done watch with no failed send. The rule is not copied: the query reads the same view.
    • A poke for chat X also lists the wake-ups that watch X as a child. So the commit that ends the child's turn delivers the parent's wake-up at once.
    • upcoming excludes rows that are already due.
  • Run times of events wake-ups:
    • Child-only wake-ups no longer poll. Their run time is the final poll at expires_at, which wait_for_events always sets.
    • Processes still poll every 15 s (eventsPollInterval, which replaces wakeupPollInterval). Pull requests still poll every minute.
    • A failed send keeps its watch out of the due query until the retry run time 15 s later, so it cannot spin the waker.
    • A delivery that readiness started before the run time reads no process or PR and keeps the run time.
  • ARCHITECTURE.md "Wake-up delivery" and the watch paragraph describe all of the above.

Hot loop guard

A ready watch makes its wake-up due until the watch is delivered. Before the expiry, a delivery always consumes a ready, non-failed child_done watch. The exceptions:

  • a lost race (the CAS or SKIP LOCKED writes nothing; the winner decides);
  • a pause (the wake-up is no longer active, so it leaves the view);
  • a busy chat (the delivery does not poke, and the end of the turn pokes).

Failed sends are filtered by failed_at IS NULL. fired_at is not set by anything yet (#295, #296), so the due query only counts child_done rows.

Tests

All run locally under flock ~/.fork-go.lock:

  • go test ./coderd/x/chatd/... -count=1: ok.
  • go test -race ./coderd/x/chatd/ -run 'Wak|WaitForEvents|Process|PullRequest|Events|Schedule': ok.
  • go test ./coderd/database/forkdb/... -count=1: ok.
  • go test ./coderd/database/dbauthz/ -run 'TestMethodTestSuite|Fork': ok.
  • go test ./coderd/ -run 'TestForkChatLoopErrors|TestForkChatWakeups|TestForkChatWakeupStatus|TestForkChatWakeupAudit|TestForkChatOrganizationWakeupCaps': 7 tests pass.
  • CHATDMIG_TEST_DATABASE_URL=... go test ./coderd/database/migrations/chatdmig/...: ok. chatdmig check: exit 0.
  • make lint/go/new-code: 0 issues. lint/emdash, lint/markdown, lint/architecture, lint/migrations: ok.

New tests:

  • TestWakerDeliversWhenChildFinishes (Quartz): a parent waits on a running child. The child's turn ends through chatstate, and the parent gets the message with no clock advance. Negative control: without the r.child_chat_id scope in the due query it fails, then reverted.
  • TestListDueChatWakeupsReadyEvents: due when the child is done before the expiry; not due when the child runs, the send failed, the wake-up expired or the parent is busy; the child scope; upcoming excludes due rows.
  • TestListReadyChatWakeupWatchesPlan checks both statements for sequential scans with 10,000 finished wake-ups.
  • TestWakeupEventsDeliversOncePerChild now asserts that a child-only wake-up runs next at its expiry and that a delivery keeps that run time.

git grep -n 'wakeupPollInterval\|lease_expires_at\|ClaimDueChatWakeups' outside migrations finds only the plugins and external_auth_links columns of the same name. Nothing for chat_wakeups is left.

Deploy notes

  • The column drop needs the old binary stopped first. This is a single instance: the migration runs at startup of the new binary.
  • After this migration, a rollback must restore the database snapshot. Swapping only the binary back breaks every wake-up query: the old binary's sqlc column lists still name lease_expires_at, and the chatdmig runner does not reject the unknown node, so the old binary starts and then fails on each wake-up read and write.
  • Safe on top of feat(chatd): deliver chat wake-ups from a state-driven waker #344, which is in main. The waker of feat(chatd): deliver chat wake-ups from a state-driven waker #344 already ignores the lease column.

Not verified

  • A live deploy of the migration on the real database.
  • lint/typos: the typos binary is not installed in this workspace.

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
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
…iven-waker-b9396e7f

# Conflicts:
#	coderd/database/forkdb/forkdbmock/forkdbmock.go
#	coderd/database/forkdb/querier.go
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.
Move the readiness rule of child watches into the view
chat_wakeup_ready_watches. ListReadyChatWakeupWatches reads it, and
ListDueChatWakeups makes an events wake-up due before its expiry when it
has a ready child_done watch with no failed send. A poke for a chat also
lists the wake-ups that watch it as a child, so the end of a child's turn
delivers the parent's wake-up at once.

Child-only wake-ups no longer poll every 15 seconds: their run time is the
final poll at the expiry. Processes poll every 15 seconds and pull
requests every minute. Drop chat_wakeups.lease_expires_at, which nothing
reads since the waker replaced the claim; the migration changes no
wake-up's status or run time.
…/ws-294-state-driven-waker-b9396e7f

# Conflicts:
#	coderd/database/forkdb/chat_wakeups.sql.go
@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-15T19:43:08.158227Z 44142b9 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.

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.
@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: e71d9238b1

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

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: e71d9238b1

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

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@ibetitsmike

Copy link
Copy Markdown
Owner Author

$> Coder Agents acted on behalf of @ibetitsmike.\n\n@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: bf2ce81bea

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

…iven-waker-b9396e7f

# Conflicts:
#	coderd/database/forkdb/chat_wakeups.sql.go
#	coderd/database/forkdb/chat_wakeups_test.go
#	coderd/database/forkdb/querier.go
#	coderd/database/forkdb/queries/chat_wakeups.sql
#	coderd/x/chatd/ARCHITECTURE.md
#	coderd/x/chatd/fork_wait_for_events.go
#	coderd/x/chatd/fork_waker_internal_test.go
#	coderd/x/chatd/fork_wakeups.go
@ibetitsmike

Copy link
Copy Markdown
Owner Author

$> Coder Agents acted on behalf of @ibetitsmike.\n\n@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 91eec594f3

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

Copy link
Copy Markdown
Owner Author

$> Coder Agents acted on behalf of @ibetitsmike.\n\n@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 60265cb5a2

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

Copy link
Copy Markdown
Owner Author

$> Coder Agents acted on behalf of @ibetitsmike.\n\n@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 44142b912d

ℹ️ 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 344810f into main Nov 15, 2026
9 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-294-waker-events branch November 15, 2026 20:59
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged at head 44142b9 as 344810f, on main 05bc672.

Gates:

  1. Head verified: 44142b9.
  2. All check runs on the head passed. fork-ci run 43753658651 on this head: success.
  3. Codex receipt on this head: issuecomment-6900189272 ("Didn't find any major issues", Reviewed commit 44142b9).
  4. Unresolved review threads: 0.
  5. Not behind main.
  6. Test evidence, run by the lead on a detached worktree at the exact head, under the Go lock (cold build after a cache clean):

Rollback: this PR drops chat_wakeups.lease_expires_at. An older binary fails every wake-up query on the migrated schema. A rollback must restore the database snapshot, not only swap the binary.

Not exercised: the full chatd suite locally (fork-ci covered it), a live migration on the daily server, and lint/typos locally.

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: replace the 15 s wake-up claim loop with a waker driven by the chat state machine

1 participant