Skip to content

feat(coderd): cancel wake-ups on archive and reject invalid /loop commands - #133

Merged
ibetitsmike merged 99 commits into
mainfrom
mike/chat-wakeups-8-loop-archive
Nov 10, 2026
Merged

ibetitsmike merged 99 commits into
mainfrom
mike/chat-wakeups-8-loop-archive

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

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

Two small follow-ups to the wake-up worker and /loop.

  • Archive cancels wake-ups. When fork-chat-wakeups is on, archiving a chat cancels the active and paused wake-ups of every chat in its family (reason archived), in the same transaction as the archive. Unarchiving never resumes them. Both archive paths (the API and auto-archive) already go through forkSetFamilyArchived, so no upstream file changes.
  • Invalid /loop returns 400. chatd returns *chatd.InvalidLoopCommandError, and coderd answers create-chat and send-message with 400 Invalid /loop command. and the reason as the detail (for example interval must be between 1m0s and 720h0m0s, got 30 seconds). Errors raised later inside the send transaction, such as the per-chat wake-up cap, still return 500.

coderd/x/chatd/ARCHITECTURE.md gets one TODO(ibetitsmike) line next to the family-archive lock order note.

Tests: TestWakeupsCanceledOnArchive (wake-ups only, with nested subagents, experiment off) and TestForkChatLoopErrors (create and send, invalid and valid). Breaking the cancel call or either 400 hook fails them.

UAT: passed on a local dev instance at 1df817c (this PR plus #134). Archiving a chat cancelled its wake-up and its child chat's wake-up, with status canceled and reason archived. Both stayed cancelled after unarchive, and resume returned 409. An archived chat with a 1-minute loop never woke over 12 worker periods, while an unarchived control chat with the same loop woke 3 times.

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
…r-events

# Conflicts:
#	coderd/x/chatd/fork_wait_for_events.go
#	coderd/x/chatd/fork_wakeups.go
…nto mike/chat-wakeups-8-loop-archive

# Conflicts:
#	coderd/exp_chats.go
#	coderd/x/chatd/fork_family_lock.go
Base automatically changed from mike/chat-wakeups-7-pr-events to main November 10, 2026 21:35
@ibetitsmike
ibetitsmike marked this pull request as ready for review November 10, 2026 21:37
@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 (df1ef73ac6) 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 (df1ef73ac6), 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 15cfeed into main Nov 10, 2026
21 of 28 checks passed
@ibetitsmike
ibetitsmike deleted the mike/chat-wakeups-8-loop-archive branch November 10, 2026 22:16
@ibetitsmike

Copy link
Copy Markdown
Owner Author

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

Merge evidence for head 40e0499b2e57f2912a26b915196b1273417862ca:

  • fork-ci pull_request run 42909818344 passed; its build job checked out Merge 40e0499b2e into df1ef73ac6 (current main). Run 42906799862 had also passed on this head against 18aec9ebaf. All checks green.
  • Codex: the review round on this exact head finished with no new findings (review state comment 6765283488 names 40e0499b2e, no unresolved findings). 0 of 1 review threads unresolved; the one thread asked for ARCHITECTURE.md text, which stays a TODO for the owner.
  • UAT at 1df817c298 (this PR plus feat: manage a chat's wake-ups through the API #134) with a real model, fork-chat-wakeups on: archiving a parent canceled its own and its child's wake-ups (canceled, reason archived, no next run), and they stayed canceled after unarchive; a /loop 1m chat archived and unarchived before its first run never woke over 12 worker periods while an unarchived control woke 3 times; /loop and /loop 5m returned 400 "Invalid /loop command." with the usage detail, and an interval over 720h returned 400. This head's own diff is identical to the UAT'd one file by file; it only adds the merge of feat(coderd): wake a chat on pull request activity #131 and main.
  • 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