Skip to content

feat(coderd/x/chatd): deliver due chat wake-ups to idle chats - #91

Merged
ibetitsmike merged 15 commits into
mainfrom
mike/chat-wakeups-2-worker
Nov 10, 2026
Merged

ibetitsmike merged 15 commits into
mainfrom
mike/chat-wakeups-2-worker

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

Xum opened this PR on behalf of @ibetitsmike.

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: the schedule_wakeup tool and /loop come in the next PR.

How delivery works

chatWorker.Start gets 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-line forkAdmit call in SendMessage, and the unexported option field.

  • Every 15 s the loop claims up to 20 due wake-ups with a 2 minute lease (ClaimDueChatWakeups, FOR UPDATE SKIP LOCKED), so two replicas never claim the same row.
  • An archived chat cancels the wake-up and an expired one completes it. events wake-ups stay active and are retried a minute later; PR4 delivers them.
  • A chat that would queue a new message gets nothing: a busy chat (running, interrupting or requires_action), a paused chat, or an error chat 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 its next_run_at unchanged, and the next tick after the turn ends delivers it. A wake-up never queues behind, steers or interrupts a turn (PRD R4.2).
  • On an idle chat the worker sends the prompt through SendMessage as the chat owner. A fork hook (SendMessageOptions.forkAdmit, 5 lines in SendMessage) runs inside the send's transaction, under the chat lock. It inserts the delivered run, keyed by the scheduled time, and ends the claim. If any of those steps fails, the message rolls back with them:
    • The chat turned busy after the first check, so the send would queue or the queue is full: nothing is sent, and the wake-up stays due.
    • The occurrence already has a run: nothing is sent, and the schedule advances.
    • The claim was canceled, paused or taken over, so its lease no longer matches: nothing is sent.
  • A failed send ends the claim, fenced by the lease, and records the run in one transaction, so a worker whose lease ran out mid-send never writes a run for the new owner's occurrence. A hook denial pauses the wake-up (hook_denied), an archive race cancels it, and anything else (including a hook dispatch failure, which is transient) marks the run failed and moves on to the next slot.
  • Missed interval or cron slots fire once, then the schedule continues from the time the message was admitted: no catch-up burst after downtime or a slow prompt hook.
  • A wake-up that resumes a finished child agent counts against the family live subagent limit, like message_agent. A refused resume records a failed run and the next occurrence tries again. max_runs and expires_at complete the wake-up.
  • Each claimed row uses the time it is processed, measured from before the claim query, not the claim time. A row whose lease ran out during a slow batch is left for the next claim. An error loading the chat leaves the lease to expire, which acts as a 2 minute backoff.

coderd/x/chatd/ARCHITECTURE.md gets one TODO line in the chat worker section for the owner to describe this loop.

Validation

  • Ran in Colima: go build ./coderd/... ./enterprise/..., go vet ./coderd/x/chatd/, and go test -run 'Wakeup|TestWorker|TestForkQuerier|FamilyLock' over coderd/x/chatd, coderd/database/forkdb and coderd/database/dbauthz against 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.
  • UAT in Colima ran a fork server against a fresh Postgres 17, with oMLX GLM-5.3-Flash as the chat model and wake-ups inserted with psql:
    • Without the experiment, a due wake-up is never claimed or delivered.
    • With it, an idle chat gets the prompt as an owner message within a second, and next_run_at moves by exactly one interval.
    • A chat running a 14 minute turn: 56 ticks skipped it, no queued message, delivered once 6 s after the turn ended.
    • Server down for three slots: exactly one delivery at restart, then back on schedule.
    • Two replicas, 8 due wake-ups: each delivered exactly once.
    • Both replicas killed mid-turn: the restarted replica resumed the turn, and the wake-up waited for it and was delivered once.
    • Also checked: archived, expired and events rows, delay, max_runs, requires_action, a member-owned chat, and cron in Europe/Warsaw.
  • A second UAT round at 5e110fc passed again: idle delivery over 8 slots, an 11 minute busy turn with one delivery after it, two replicas delivering 8 rows once each, a row paused while due, and no delivery without the experiment. The later commits, which keep busy, paused and errored-with-queue chats from being claimed, keep a lost claim from dropping an occurrence, schedule from admission and apply the family live limit, are covered by the Postgres tests above.
  • Known: a row with an invalid cron spec is delivered once before it pauses with 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.

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

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this on behalf of @ibetitsmike.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 10, 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-10T04:42:39.238590Z 6e91a81 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: 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".

Comment thread coderd/x/chatd/fork_wakeups.go Resolved
Comment thread coderd/x/chatd/fork_wakeups.go Resolved
Comment thread coderd/x/chatd/fork_wakeups.go Resolved
Comment thread coderd/x/chatd/ARCHITECTURE.md Resolved
Comment thread coderd/x/chatd/fork_wakeups.go Resolved
Comment thread coderd/x/chatd/fork_wakeups.go Resolved
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.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this on behalf of @ibetitsmike.

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

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

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this on behalf of @ibetitsmike.

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

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

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this on behalf of @ibetitsmike.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 0f08d26814

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

Base automatically changed from mike/chat-wakeups-1-schema to main November 10, 2026 03:46
…orker

# Conflicts:
#	coderd/database/forkdb/chat_wakeups.sql.go
#	coderd/database/forkdb/querier.go
#	coderd/database/forkdb/queries/chat_wakeups.sql
@ibetitsmike
ibetitsmike marked this pull request as ready for review November 10, 2026 03:47
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this on behalf of @ibetitsmike.

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

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this on behalf of @ibetitsmike.

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

Comment thread coderd/x/chatd/fork_wakeups.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

The open finding on 32c3d8b was declined with a reply (a hook denial pausing the wake-up is intended).

Xum posted this on behalf of @ibetitsmike.

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

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

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this on behalf of @ibetitsmike.

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

Comment thread coderd/x/chatd/fork_wakeups.go Resolved
Comment thread coderd/x/chatd/fork_wakeups.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this on behalf of @ibetitsmike.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

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

@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum (AI agent) closed and reopened this on behalf of @ibetitsmike.

Closing and reopening only to make GitHub recompute the merge ref against current main (24fcb8445d) and run fork-ci on it. The head is unchanged.

@ibetitsmike ibetitsmike reopened this Nov 10, 2026
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum (AI agent) closed and reopened this again on behalf of @ibetitsmike.

The merge ref now points at current main (24fcb8445d), but the run started by the first reopen was queued against the old merge ref. This second reopen starts fork-ci on the recomputed merge. The head is unchanged.

@ibetitsmike ibetitsmike reopened this Nov 10, 2026
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum (AI agent) closed and reopened this on behalf of @ibetitsmike.

Closing and reopening only to make GitHub recompute the merge ref against current main (4c0cf8c83b) and run fork-ci on it. The head is unchanged.

@ibetitsmike ibetitsmike reopened this Nov 10, 2026
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum (AI agent) closed and reopened this again on behalf of @ibetitsmike.

The merge ref now points at current main (4c0cf8c83b), but the run started by the first reopen was queued against the old merge ref. This second reopen starts fork-ci on the recomputed merge. The head is unchanged.

@ibetitsmike ibetitsmike reopened this Nov 10, 2026
@ibetitsmike
ibetitsmike merged commit f924e36 into main Nov 10, 2026
32 of 46 checks passed
@ibetitsmike
ibetitsmike deleted the mike/chat-wakeups-2-worker branch November 10, 2026 08:53
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum (AI agent) merged this on behalf of @ibetitsmike.

Merge evidence for head 6e91a8154306b3346ddc7290e761737c065c7dc5:

  • fork-ci pull_request run 42783051413 passed; its build job checked out Merge 6e91a81543 into 4c0cf8c83b (current main). All checks green. Run 42756239770 had also passed on this head (7 jobs).
  • Codex on this exact head: no major issues (comment 6759190198). The 3 round 7 findings were fixed in 6e91a81 with tests that fail when each fix is reverted: wake-up resumes count against the family live subagent limit, the next run is computed from admission time, and a failed run is recorded in the claim-finish transaction. Declined: the round 6 hook-denial race (needs a hostile hook), and from round 8, expiry reached during admission (expiry bounds which occurrences are scheduled, not how long admission takes) and the schedule after a failed send (nothing was delivered, so the missed-slot rule applies). 0 unresolved review threads. Merge state CLEAN.
  • UAT round 2 PASS at 5e110fc (PR description): idle delivery over 8 slots, an 11 minute busy turn with one delivery after it, two replicas delivering 8 rows once each, a row paused while due, and no delivery without the experiment. The later commits are covered by the Postgres tests listed in the description. fork-chat-wakeups stays off on the daily deployment.

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