Skip to content

feat(coderd/x/chatd): wake a chat when its child agents finish - #106

Merged
ibetitsmike merged 43 commits into
mainfrom
mike/chat-wakeups-4-events
Nov 10, 2026
Merged

ibetitsmike merged 43 commits into
mainfrom
mike/chat-wakeups-4-events

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

Xum opened this PR on behalf of @ibetitsmike.

Part of #11. Stacked on #93 (base mike/chat-wakeups-3-loop). It retargets when #93 merges.

Lets a coordinator chat wait for its child agents without blocking a turn, behind fork-chat-wakeups. This is the PRD 6.4 acceptance case: a coordinator starts three children, ends its turn, and gets one wake message per child as each one finishes.

wait_for_events tool

appendForkTools adds the tool next to schedule_wakeup. The model passes child_chat_ids (direct children only) or nothing, which watches every child that is still working. A watch set over 50 children is refused. It can also set a prompt and an expires_in_seconds (at least 60, 24 hours by default). The tool creates an events wake-up with source wait_for_events, one child_done event per child and next_run_at set to now. The wake-up takes the tool-call ID, and the tool looks it up first, so a replayed call returns the same wake-up, described from its stored events. It returns the wake-up ID, its expiry and the watched children.

Delivery

Events wake-ups run in the PR2 worker, after the busy-chat check, so a message never queues behind, steers or interrupts a turn.

  • On each claim the worker sends one message for the first watched child that is done and has no run yet. The message names the child and its status, and the run key is child_done:<child id>, so each child wakes the chat once and messages are never batched.
  • A child is done when it is archived, or settled in a status other than running, interrupting or paused, and has no active events wake-up of its own. A nested coordinator that waits for its own children is therefore not done yet.
  • The wake-up completes once every watched child has a run or no longer exists. Until then it polls again every 15 seconds.
  • A failed send records its run under a key of its own, so the child stays undelivered and the next poll retries it.
  • Event types other than child_done are ignored. An events value that is not a list pauses the wake-up with invalid_events.

sendWakeupPrompt now takes a wakeupDelivery (text, run key, finish), and its send error handling moves into finishWakeupSend, so prompt wake-ups behave as in PR2. A new forkdb query, ListChatWakeupRunKeys, gets dbauthz (read through authorizeChatWakeup) and dbmetrics wrappers.

Known

Validation

  • Ran in Colima on the current head: forkdb generate.sh with no drift, go build ./coderd/... ./enterprise/..., and go test -run 'WaitForEvents|Wakeup|ScheduleWakeup|Loop|TestAppendForkTools|TestForkQuerier' over coderd/x/chatd, coderd/database/forkdb and coderd/database/dbauthz against Postgres.
  • Tests cover the tool's watch set and refusals, one message per child keyed by child, a busy coordinator, archived and missing children, unknown and invalid events, a failed send that is retried, a replayed call, a watch set over the limit, the events poll in nextWakeupRun, the dbauthz action and the run-key query scope. Each of 17 behavior breaks makes a test fail.
  • UAT at b20e3c2 in Colima, with a fork server on Postgres 17 and oMLX GLM-5.3-Flash as the chat model:
    • A coordinator spawned 3 children, called wait_for_events with their IDs and ended its turn. It got 3 wake messages, one per child, each after the previous turn ended. It called wait_agent once per wake and reported each child's answer, and the wake-up completed with done.
    • A child finished while the parent was running: no message for 40 s, then one on the next tick after the parent went idle.
    • An archived child was delivered at once, a paused child was not, and a missing chat ID did not block completion.
    • An unknown event type and a non-child chat ID were ignored, and an invalid events value paused the wake-up.
    • Without the experiment, the tool is not offered.

Size

About 330 lines of code, all in fork files, and about 360 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.
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.
@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: 269dd17dec

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

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

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

Copy link
Copy Markdown

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

Reviewed commit: 8fa85047bd

ℹ️ 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 marked this pull request as ready for review November 10, 2026 14:15
@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 (69f04bdfdf) 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 (69f04bdfdf), 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

@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: 8fa85047bd

ℹ️ 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
@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. Delightful!

Reviewed commit: ffdb8baeb7

ℹ️ 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 976f8fc into main Nov 10, 2026
9 checks passed
@ibetitsmike
ibetitsmike deleted the mike/chat-wakeups-4-events branch November 10, 2026 15:20
@ibetitsmike

Copy link
Copy Markdown
Owner Author

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

Merge evidence for head ffdb8baeb7282301b2e967a86752e9f6b8a63f81:

  • fork-ci pull_request run 42838465076 passed; its build job checked out Merge ffdb8baeb7 into e44a0a2f97 (current main). All checks green.
  • Codex on this exact head: no major issues (comment 6770622952). 0 unresolved review threads. The last P2 (an expired but still active events wake-up blocked a new wait_for_events) was fixed in 9a3e417, with a regression test in ffdb8ba that fails without the fix.
  • UAT at b20e3c2 (fork server on Postgres 17, oMLX GLM-5.3-Flash) passed: a coordinator waiting on 3 children got one wake per child and completed with done; a child finishing while the parent was running woke it only after it went idle; archived, paused and missing children and invalid events behaved as specified; without the experiment the tool is not offered. Later commits are review fixes with tests and main merges.
  • 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