Repository navigation
feat(coderd/x/chatd): add the schedule_wakeup tool and the /loop command - #93
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
|
@codex review
|
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 187991a213
ℹ️ 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".
…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
|
@codex review
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 46356a04fa
ℹ️ 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 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
|
@codex review
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f79d2cc786
ℹ️ 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".
…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.
|
@codex review
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bcc8ca6535
ℹ️ 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. Swish! 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 ( |
Closing and reopening only to make GitHub recompute the merge ref against current main ( |
The merge ref now points at current main ( |
…t-wakeups-3-loop # Conflicts: # coderd/x/chatd/ARCHITECTURE.md
|
@codex review
|
|
Codex Review: Didn't find any major issues. Bravo. 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 ( |
Merge evidence for head
|
Part of #11. Stacked on #91 (base
mike/chat-wakeups-2-worker); it retargets tomainafter #91 merges.Adds the two ways a chat creates wake-ups, both behind
fork-chat-wakeups. PR2's worker delivers them.schedule_wakeuptoolappendForkToolsadds the tool to a turn when the experiment is on. The model sets a prompt and exactly one of:delay_seconds: run once.interval_seconds: at least 60.cron: 5 fields, with an optional IANAtimezonethat defaults to UTC.max_runsandexpires_in_secondsare optional. The tool returns the wake-up ID and its first run time in the wake-up's time zone. Cron is parsed when the wake-up is created, so an invalid spec, or one that never occurs such as0 0 31 2 *, is refused instead of being delivered once and then paused, which closes the known gap in #91. Exactly 5 fields keeps out@everyandCRON_TZ. The first run time comes from the worker's ownnextWakeupRun, so there is no second cron parser./loop [interval] <prompt>SendMessagegets two hook lines:forkLoopCommandstrips/loop [interval]from a single-text-part message. A usage error stops the send.tx.SendMessage,forkLoop.schedulecreates the interval wake-up, so the first message and the loop commit or roll back together.CreateChatparses/loopin the first message of a root chat the same way. The wake-up is created through a newForkAfterInserthook onchatstate.CreateChatInput, which runs in the transaction that inserts the chat. A subagent's first message is never parsed, since its parent's model wrote it. A fallback title comes from the loop prompt, not the command.A
user_prompt_submithook still gets the message as submitted,/loopincluded, so it can deny or audit a loop. When the hook replaces the prompt, the loop runs the replacement.The first turn starts at once with only the prompt. The interval is
<n>s|m|h|d, defaults to 10 minutes and must be between 1 minute and 30 days. With the experiment off,/loopis stored verbatim.Shared rules
Both paths go through
createChatWakeup. It enforces these rules:/loop.schedule_wakeupwake-up is attributed to the chat owner, a/loopwake-up to the user who sent it.The per-day run caps come in PR5.
Known
A
/loopusage error or a full chat surfaces fromPOST /chats/{chat}/messagesas a 500, "Failed to create chat message.", with the reason indetail. Returning a 400 would need an edit tocoderd/exp_chats.go, which is out of scope here.A loop runs until the chat is archived: listing and canceling wake-ups come with the API in PR6.
coderd/x/chatd/ARCHITECTURE.mdgets one TODO line underPOST /api/v2/chats/{chat}/messagesfor the owner.Validation
go build ./...,go vet ./coderd/x/chatd/andgo test -run 'Wakeup|Loop|UserPromptSubmit|CreateChat|TestForkQuerier'overcoderd/x/chatd,coderd/x/chatd/chatstate,coderd/database/forkdbandcoderd/database/dbauthzagainst Postgres./loopparser, a replayedschedule_wakeupcall (also past its first run), and/loopthroughSendMessageandCreateChat, including the sender attribution, the hook payload, a hook override and the fallback title. TheSendMessagetests check that the loop is scheduled, that the experiment gate holds, and that an archived chat rolls back both the message and the wake-up./loopyaccepted, wrong delay time, a never-occurring cron accepted, no lookup by ID, the hook getting the stripped prompt, the owner as the loop creator,/loopparsed in a subagent's first message, no wake-up fromCreateChat, the hook override ignored and the title kept from the command./loop 5mstarted a turn at once with only the prompt, and its row ran every 300 s: three turns in the first 10.5 minutes.schedule_wakeupwithdelay_seconds: 60, and the prompt arrived 60 s later./loop 5m xwas stored verbatim, no row was created and the tool was not offered./loop 10s xand a bare/loopwere refused with the reason indetail(the 500 under Known), and nothing was stored.CreateChatand hook-override fixes are covered by the Postgres tests above.Size
About 440 lines of code; upstream files get 13 lines in
chat_mutations.go, 18 inchatd.go, 9 inchatstate/transitions.go, 3 infork_tools.goand one TODO line. About 370 lines of tests.