Repository navigation
feat(coderd/x/chatd): wake a chat when its child agents finish - #106
Conversation
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.
…oder into mike/chat-wakeups-2-worker
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.
… mike/chat-wakeups-3-loop
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.
… mike/chat-wakeups-3-loop
…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.
… mike/chat-wakeups-3-loop
…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.
…ike/chat-wakeups-4-events
…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.
|
@codex review
|
There was a problem hiding this comment.
💡 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".
|
@codex review
|
There was a problem hiding this comment.
💡 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".
|
@codex review
|
There was a problem hiding this comment.
💡 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".
…ait_for_events watches
|
@codex review
|
|
Codex Review: Didn't find any major issues. Nice work! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
Closing and reopening only to make GitHub recompute the merge ref against current main ( |
The merge ref now points at current main ( |
There was a problem hiding this comment.
💡 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".
|
@codex review
|
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
Merge evidence for head
|
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_eventstoolappendForkToolsadds the tool next toschedule_wakeup. The model passeschild_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 apromptand anexpires_in_seconds(at least 60, 24 hours by default). The tool creates aneventswake-up with sourcewait_for_events, onechild_doneevent per child andnext_run_atset 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.
child_done:<child id>, so each child wakes the chat once and messages are never batched.running,interruptingorpaused, and has no activeeventswake-up of its own. A nested coordinator that waits for its own children is therefore not done yet.child_doneare ignored. Aneventsvalue that is not a list pauses the wake-up withinvalid_events.sendWakeupPromptnow takes awakeupDelivery(text, run key, finish), and its send error handling moves intofinishWakeupSend, so prompt wake-ups behave as in PR2. A new forkdb query,ListChatWakeupRunKeys, gets dbauthz (read throughauthorizeChatWakeup) and dbmetrics wrappers.Known
childAwaitsAnswerreturns false until feat(coderd): let descendant chats ask the user blocking questions behind fork-nested-subagents #86 is on main. Then it callspendingDescendantQuestion.Validation
generate.shwith no drift,go build ./coderd/... ./enterprise/..., andgo test -run 'WaitForEvents|Wakeup|ScheduleWakeup|Loop|TestAppendForkTools|TestForkQuerier'overcoderd/x/chatd,coderd/database/forkdbandcoderd/database/dbauthzagainst Postgres.nextWakeupRun, the dbauthz action and the run-key query scope. Each of 17 behavior breaks makes a test fail.wait_for_eventswith their IDs and ended its turn. It got 3 wake messages, one per child, each after the previous turn ended. It calledwait_agentonce per wake and reported each child's answer, and the wake-up completed withdone.running: no message for 40 s, then one on the next tick after the parent went idle.eventsvalue paused the wake-up.Size
About 330 lines of code, all in fork files, and about 360 lines of tests.