Skip to content

feat(chatd): deliver chat wake-ups from a state-driven waker - #344

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

ibetitsmike merged 6 commits into
mainfrom
mike/issue-294-waker

Conversation

@ibetitsmike

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

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

  • One forkWaker per chatd Server (coderd/x/chatd/fork_waker.go), behind fork-chat-wakeups.
    • Pokes: New wraps the Server pubsub (withForkWaker, one hook line in chatd.go). Each chat: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.
    • Timer at the earliest next_run_at, plus a 60 second sweep.
    • At most 8 deliveries at once, one per chat, each with a 2 minute deadline.
  • Due query ListDueChatWakeups: one plain SELECT that skips busy chats and also returns the next upcoming row for the timer.
  • Delivery runs in one transaction: chat row lock, then LockActiveChatWakeup (FOR UPDATE SKIP LOCKED, status = 'active'), then a re-check that run_count, next_run_at and updated_at are unchanged. Otherwise it writes nothing (errWakeupNotDue).
  • Removed: wakeupLoop, ClaimDueChatWakeups, FinishChatWakeupClaim and the lease reads. A busy chat now writes nothing; the end of its turn pokes the waker.
  • SetChatWakeupStatus locks chat then wake-up, the same order as deliveries.
  • coderd/x/chatd/ARCHITECTURE.md: new "Wake-up delivery" section.

Deploy safety alone

  • No migration. lease_expires_at stays and is ignored. No wake-up changes status on deploy.
  • A row the old binary claimed mid-deploy is listed again. The run key dedupes the occurrence, so it is not delivered twice.
  • Events wake-ups keep their 15 second poll reschedule in this PR.

Design note

The brief asked for a forkAfterCommit field on ChatMachine. I used a pubsub wrapper instead. chatstate already publishes chat:update after 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

  • New 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.
  • Adapted tests that asserted the claim or lease: PausedDuringSend, ChangedDuringSendThenFailed, ChatBusyAfterListAtCapStaysDue, TestWakeupPullRequestReadNearDeliveryDeadline.
  • Commands run:
    • go test ./coderd/x/chatd/... -count=1: ok
    • go test ./coderd/x/chatd/ -race -run 'TestWak|TestWaitForEvents|TestScheduleWakeupTool|TestNextWakeupRun' -count=3: ok
    • go test ./coderd/database/forkdb/ -count=1: ok
    • go test ./coderd/database/dbauthz/ -run 'TestMethodTestSuite|Fork' -count=1: ok
    • go test ./coderd/ -run 'TestForkChatWakeup.*|TestForkChatLoopErrors|TestForkChatOrganizationWakeupCaps' -count=1: ok
    • make lint/go/new-code: 0 issues. Full golangci-lint shows no findings in changed lines. lint/markdown, lint/emdash, lint/architecture pass.
  • Negative controls, both reverted:
    • Poke replaced with a no-op: TestWakerDeliversWhenTurnEnds fails.
    • Re-check in lockDueWakeup disabled: TestWakeupAdmissionRaces/ChangedDuringSendThenFailed fails.

Not verified

  • No run against a live multi-replica deployment.
  • Creating a wake-up does not poke on its own. It happens inside a turn whose end pokes, and the sweep is the backstop.

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
@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-15T17:20:08.057239Z 3380a23 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.

@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. Keep them coming!

Reviewed commit: f1e90269dc

ℹ️ 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 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
@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: d7f5c05c07

ℹ️ 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/forkdbmock/forkdbmock.go
#	coderd/database/forkdb/querier.go
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged main in to clear a conflict in generated forkdb files. @codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: ec9e08302a

ℹ️ 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 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.
@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: 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.
@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. Nice work!

Reviewed commit: d16e782b73

ℹ️ 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. Keep it up!

Reviewed commit: 3380a238a4

ℹ️ 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 7c10f9f into main Nov 15, 2026
9 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-294-waker branch November 15, 2026 17:55
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged as 7c10f9f at 17:55:06Z. Gates on head 3380a23:

  1. Head 3380a23 matched the verified SHA (--match-head-commit).
  2. All check runs passed, including fork-ci run 43740153864 on this exact head.
  3. Codex receipt on 3380a23: "Didn't find any major issues" (issuecomment-6898305602).
  4. No unresolved review threads (two Codex P1 threads were fixed or answered and resolved).
  5. Not behind main (main e993903, live on the server at 17:54Z before the merge).
  6. Evidence run by the maintainer on the exact head:
    • go test -count=1 -v -run 'TestWaker|TestWakeup|TestWaitForEvents|TestScheduleWakeupTool' ./coderd/x/chatd/: 46 passed, 0 failed, including all 8 TestWaker*.
    • go test -count=1 -v -run Wakeup ./coderd/database/forkdb/: 7 passed.
    • Negative control: with forkWaker.poke made a no-op, TestWakerDeliversWhenTurnEnds fails (25 s timeout) while the timer and sweep tests still pass. Reverted.
    • Opus 5.5 read-only review: mergeable, no P1. Its P2s and nits are filed as chatd waker: follow-ups from the #294 review (stalled reads, slot stall, poke gaps) #347.

Not exercised: no live deploy with active wake-ups has run on this code; the coderd TestForkChat* wake-up tests and the dbauthz suite were run by the workstream, not by the maintainer.

Note: #336 merged 3 s after this PR (17:55:09Z) from another chat, so both land in one deploy.

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.

1 participant