Skip to content

feat(coderd/x/chatd): add the schedule_wakeup tool and the /loop command - #93

Merged
ibetitsmike merged 29 commits into
mainfrom
mike/chat-wakeups-3-loop
Nov 10, 2026
Merged

ibetitsmike merged 29 commits into
mainfrom
mike/chat-wakeups-3-loop

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 #91 (base mike/chat-wakeups-2-worker); it retargets to main after #91 merges.

Adds the two ways a chat creates wake-ups, both behind fork-chat-wakeups. PR2's worker delivers them.

schedule_wakeup tool

appendForkTools adds 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 IANA timezone that defaults to UTC.

max_runs and expires_in_seconds are 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 as 0 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 @every and CRON_TZ. The first run time comes from the worker's own nextWakeupRun, so there is no second cron parser.

/loop [interval] <prompt>

SendMessage gets two hook lines:

  • Before the transaction, forkLoopCommand strips /loop [interval] from a single-text-part message. A usage error stops the send.
  • Inside the transaction, after tx.SendMessage, forkLoop.schedule creates the interval wake-up, so the first message and the loop commit or roll back together.

CreateChat parses /loop in the first message of a root chat the same way. The wake-up is created through a new ForkAfterInsert hook on chatstate.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_submit hook still gets the message as submitted, /loop included, 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, /loop is stored verbatim.

Shared rules

Both paths go through createChatWakeup. It enforces these rules:

  • At most 10 active or paused wake-ups per chat.
  • Prompts of at most 4000 bytes, which must not start with /loop.
  • A schedule_wakeup wake-up is attributed to the chat owner, a /loop wake-up to the user who sent it.
  • A tool call replayed after a worker crash returns the same wake-up: it takes the ID of the tool-call occurrence, and the lookup by that ID comes before any validation.

The per-day run caps come in PR5.

Known

A /loop usage error or a full chat surfaces from POST /chats/{chat}/messages as a 500, "Failed to create chat message.", with the reason in detail. Returning a 400 would need an edit to coderd/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.md gets one TODO line under POST /api/v2/chats/{chat}/messages for the owner.

Validation

  • Ran in Colima on the current head (with feat(coderd/x/chatd): deliver due chat wake-ups to idle chats #91's head merged): go build ./..., go vet ./coderd/x/chatd/ and go test -run 'Wakeup|Loop|UserPromptSubmit|CreateChat|TestForkQuerier' over coderd/x/chatd, coderd/x/chatd/chatstate, coderd/database/forkdb and coderd/database/dbauthz against Postgres.
  • Tests cover the tool's argument rules, time zones, the limit and delay math, the /loop parser, a replayed schedule_wakeup call (also past its first run), and /loop through SendMessage and CreateChat, including the sender attribution, the hook payload, a hook override and the fallback title. The SendMessage tests 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.
  • Each of 17 behavior breaks makes a test fail. The breaks are: no schedule, prefix kept, scheduling outside the transaction, gate removed, limit off by one, time zone ignored, two kinds accepted, /loopy accepted, wrong delay time, a never-occurring cron accepted, no lookup by ID, the hook getting the stripped prompt, the owner as the loop creator, /loop parsed in a subagent's first message, no wake-up from CreateChat, the hook override ignored and the title kept from the command.
  • UAT at 187991a in Colima, with a fork server on Postgres 17 and oMLX GLM-5.3-Flash as the chat model:
    • /loop 5m started a turn at once with only the prompt, and its row ran every 300 s: three turns in the first 10.5 minutes.
    • Server down across a due slot: the missed slot was delivered once at restart, then the loop was back on schedule.
    • The model called schedule_wakeup with delay_seconds: 60, and the prompt arrived 60 s later.
    • Without the experiment, /loop 5m x was stored verbatim, no row was created and the tool was not offered.
    • /loop 10s x and a bare /loop were refused with the reason in detail (the 500 under Known), and nothing was stored.
    • The later CreateChat and 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 in chatd.go, 9 in chatstate/transitions.go, 3 in fork_tools.go and one TODO line. About 370 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
@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-10T10:33:46.813969Z 54b8b81 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: 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".

Comment thread coderd/x/chatd/fork_schedule_wakeup.go Resolved
Comment thread coderd/x/chatd/ARCHITECTURE.md Resolved
Comment thread coderd/x/chatd/fork_schedule_wakeup.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.
…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.
@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: 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".

Comment thread coderd/x/chatd/fork_schedule_wakeup.go Resolved
Comment thread coderd/x/chatd/fork_schedule_wakeup.go Resolved
Comment thread coderd/x/chatd/fork_schedule_wakeup.go Resolved
Comment thread coderd/x/chatd/chat_mutations.go Resolved
Comment thread coderd/x/chatd/fork_loop_command.go Resolved
Comment thread coderd/x/chatd/chat_mutations.go Resolved
Comment thread coderd/x/chatd/fork_schedule_wakeup.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.
…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.
@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: 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".

Comment thread coderd/x/chatd/chat_mutations.go Resolved
Comment thread coderd/x/chatd/chat_mutations.go Resolved
Comment thread coderd/x/chatd/chat_mutations.go Resolved
…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.
@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: 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".

Comment thread coderd/x/chatd/chatd.go Resolved
@ibetitsmike
ibetitsmike marked this pull request as ready for review November 10, 2026 08:55
@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. Swish!

Reviewed commit: d40b7004af

ℹ️ 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 (2d635ea9a6) 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 (2d635ea9a6), 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 (716102744d) 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 (716102744d), 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
…t-wakeups-3-loop

# Conflicts:
#	coderd/x/chatd/ARCHITECTURE.md
@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. Bravo.

Reviewed commit: 54b8b8195f

ℹ️ 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 (643ba9ab2c) 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 (643ba9ab2c), 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 32bbba5 into main Nov 10, 2026
20 of 27 checks passed
@ibetitsmike
ibetitsmike deleted the mike/chat-wakeups-3-loop branch November 10, 2026 11:09
@ibetitsmike

Copy link
Copy Markdown
Owner Author

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

Merge evidence for head 54b8b8195fe2daf76b89460d6215e7439de9ea4e:

  • fork-ci pull_request run 42802452387 passed; its build job checked out Merge 54b8b8195f into 643ba9ab2c (current main). All checks green. Run 42799566872 had also passed on this head.
  • Codex on this exact head: no major issues (comment 6765094562). 0 unresolved review threads. 54b8b8195f merges main 273daf03dd and keeps every ARCHITECTURE.md TODO line from both sides.
  • UAT PASS at 187991a213 in Colima with a fork server on Postgres 17 and GLM-5.3-Flash (PR body, Validation): /loop 5m ran every 300 s, a slot missed while the server was down was delivered once at restart, a model schedule_wakeup with delay_seconds: 60 delivered its prompt 60 s later, and with the experiment off /loop was stored verbatim with no row and no tool. Accepted as covering this head: the later commits are Codex fixes (CreateChat, hook override) and keep-both-sides merges of main (last: 273daf03dd), covered by the Postgres tests in chat-tests.
  • Known: invalid /loop input still answers 500 with the reason in detail; the follow-up API PR returns 400.

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