Skip to content

feat(coderd): wake a chat on pull request activity - #131

Merged
ibetitsmike merged 81 commits into
mainfrom
mike/chat-wakeups-7-pr-events
Nov 10, 2026
Merged

ibetitsmike merged 81 commits into
mainfrom
mike/chat-wakeups-7-pr-events

Conversation

@ibetitsmike

Copy link
Copy Markdown
Owner

Part of #11 (W5 chat wake-ups), stacked on #115.

A chat can now wait for activity on a pull request. wait_for_events takes an optional pull_request_url and pull_request_events (any of review, comment, checks, closed). The wake-up worker polls the pull request through the chat owner's linked git account every 60 s and wakes the chat when requested activity from someone other than the owner shows up.

  • Who counts: the owner's own reviews and comments never wake the chat. The wake-up message names the author and URL of each event and never includes comment or review bodies. A summary lists at most 20 events, then - and N more.
  • Checks: the chat wakes only when checks settle on a head SHA, or the result changes on a new head. A new head with pending checks does not wake it.
  • Closed: closed wakes on close or merge. A pull request that closes when closed was not requested stays pending until the wake-up expires.
  • Refusals: an already closed or merged pull request, a URL that is not a pull request, a host with no configured git provider, and an owner with no linked account are refused when the tool is called.
  • Read failures: a rate limit retries at the provider's reset time with a 60 s floor, and any other error retries after 5 minutes. Neither pauses nor completes the wake-up.
  • Children: a call can watch child chats and a pull request together. Each tick delivers one event, and the pull request is not read in a tick that delivers a child.

Commits 1 to 4 are the GitHub activity reader from #121, cherry-picked because this stack's base predates it. They are already in main and will drop out when the stack is rebased onto main.

Hot files: coderd/coderd.go (1 line, sets ForkPullRequestActivity in the chatd config) and coderd/x/chatd/chatd.go (3 small hunks for the config field).

Tests: table tests at the worker boundary in fork_pull_request_events_internal_test.go and a coderd boundary test for provider and token resolution. Negative controls were run on each assertion (27 breaks, all caught).

UAT: passed on 3f6e2f05d0 against a local server with a linked GitHub account and a throwaway draft PR (#129, closed). The Codex bot comment woke a comment watch once, listing the bot and the comment URL with no body; the owner's own comments woke nothing over 3 polls; closing the PR woke a closed watch in 15 s; a merged PR, a non-PR URL and a missing repo were refused at the tool call; next_run_at advanced by 60 s per poll. A baseline checks_state of success turned out to be three completed check runs on the head, not a zero-check rollup (zero checks read as none).

Xum posted this on behalf of @ibetitsmike.

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.
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.
… mike/chat-wakeups-3-loop

# Conflicts:
#	coderd/x/chatd/chat_mutations.go
…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.
…wake-up

The wake-up ID derives from the tool call, so a call replayed after a worker
restart finds the wake-up it created instead of adding a second one.
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.
…mpossible cron

- schedule_wakeup takes the ID of the tool-call occurrence, since providers
  can reuse a call ID in a later step.
- A replayed call returns its wake-up before the first run is recomputed.
- A cron schedule that never occurs is refused.
- user_prompt_submit hooks see the /loop command as submitted.
- A /loop wake-up is attributed to the user who sent it.
…s to the loop

A /loop command as the first message of a new root chat now creates its
wake-up in the transaction that inserts the chat, through a ForkAfterInsert
hook on chatstate.CreateChatInput. A subagent first message is never parsed,
since its parent model wrote it. When a user_prompt_submit hook replaces the
prompt, the loop runs the replacement.
…orker

# Conflicts:
#	coderd/database/forkdb/chat_wakeups.sql.go
#	coderd/database/forkdb/querier.go
#	coderd/database/forkdb/queries/chat_wakeups.sql
…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.
…flake the per-child tick

Add TestWakeupEventsUnknownAndInvalidEvents: an event type the worker does not know sends nothing and does not hold the wake-up open, and events that are not a JSON array pause it with reason invalid_events.

TestWakeupEventsDeliversOncePerChild advanced its clock by exactly the poll interval, but the worker adds the time it spent since the claim to the next poll, so under load a tick came microseconds before the wake-up was due and skipped a delivery. Tick one second past the interval.
…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.
# Conflicts:
#	coderd/database/forkdb/chat_wakeups.sql.go
#	coderd/x/chatd/fork_tools.go
#	coderd/x/chatd/fork_wait_for_events.go
#	coderd/x/chatd/fork_wait_for_events_internal_test.go
#	coderd/x/chatd/fork_wakeups.go
# Conflicts:
#	coderd/database/dbauthz/fork_forkdb.go
#	coderd/database/dbmetrics/fork_forkdb.go
#	coderd/database/forkdb/forkdbmock/forkdbmock.go
#	coderd/database/forkdb/querier.go
Base automatically changed from mike/chat-wakeups-5-caps to main November 10, 2026 19:22
…r-events

# Conflicts:
#	coderd/x/chatd/fork_wait_for_events.go
#	coderd/x/chatd/fork_wakeups.go
@ibetitsmike
ibetitsmike marked this pull request as ready for review November 10, 2026 19:23

@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: d8b3d03d1a

ℹ️ 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_wait_for_events.go Resolved
Comment thread coderd/x/chatd/fork_wait_for_events.go Resolved
Comment thread coderd/x/chatd/fork_wait_for_events.go Resolved
@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: 9ccfcfc2b5

ℹ️ 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 (5654571bb0) 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 (5654571bb0), 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 18aec9e into main Nov 10, 2026
20 of 27 checks passed
@ibetitsmike
ibetitsmike deleted the mike/chat-wakeups-7-pr-events branch November 10, 2026 21:35
@ibetitsmike

Copy link
Copy Markdown
Owner Author

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

Merge evidence for head 7f8c3fd7b8784e875c112dcfac6445eaa8f51c71:

  • fork-ci pull_request run 42897905134 passed; its build job checked out Merge 7f8c3fd7b8 into 5654571bb0 (current main). Run 42889994798 had also passed on this head against bef9c59966. All checks green.
  • Codex: the last review was on 9ccfcfc2b5 (review 5848284248). This head only merges main after feat(coderd/x/chatd): stop the workspaces of workspace subagents when their family is archived #147; its own diff is identical to 9ccfcfc2b5's (same patch ID). 0 of 32 review threads unresolved. Declined with replies: the late head change after expiry, duplicate suppression during the final retry window, rolling upgrades across mixed replicas (the fork runs one coderd), and the Checks read permission on the default watch (the call fails at once with the 403, and the agent can retry without checks).
  • UAT round 2 on this exact head with a real model and a throwaway draft PR (UAT W5 PR7 round 2 throwaway (do not merge) #155, closed, branches deleted): the default watch (only pull_request_url) stored review, comment, checks and closed; a new head's CI woke the chat exactly once with checks: success, 0 of 2 failed; a Codex bot comment woke a comment watch once and Mike's own comment did not; closing the PR woke the closed watch; an unknown event value was refused with no wake-up row; a children-only watch woke the parent when the child finished. Round 1 at 3f6e2f05d0 covered comments, close, refusals and the 60-second poll cadence.
  • No migration.

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