Skip to content

feat: add chat wake-up tables and queries - #82

Merged
ibetitsmike merged 6 commits into
mainfrom
mike/chat-wakeups-1-schema
Nov 10, 2026
Merged

ibetitsmike merged 6 commits into
mainfrom
mike/chat-wakeups-1-schema

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner

Xum opened this PR on behalf of @ibetitsmike.

Part of #11.

Adds the storage for chat wake-ups: the fork-chat-wakeups experiment, a chatd graph node with two tables, and the forkdb queries a worker and the wake-up tools need. Nothing calls them yet. The worker, schedule_wakeup and /loop, wait_for_events, caps, the API and the UI come in the next PRs of the stack.

Why upstream removed automations (scope item 0)

Upstream removed its chat automations in coder#30412 (merged 2026-10-11). I read the PR body, both commits, every PR comment, the 16 reverted stack PRs it lists, and searched the upstream issues. None of them says why. Reason not stated. The PR body only covers what it removes and the upgrade consequences. Two of upstream's design notes shaped this design:

  • Its schedule loop (feat(coderd): run chat automation schedules coder/coder#30160) noted that two replicas could both reach the prompt hook for the same occurrence, and proposed a lease as a follow-up. Here, claims take a lease with FOR UPDATE SKIP LOCKED, and run records are unique per occurrence.
  • Its credentials and RBAC resource needed destructive down migrations when the feature went. This design adds no API key scope, RBAC resource or upstream column.

Schema (node 01a1c28c-…_chat_wakeups, in chatd)

  • chat_wakeups: one row per wake-up of a chat, with the chat's organization_id copied on insert.
    • Kind delay, interval, cron or events, with the prompt each run sends.
    • Status active, paused, completed or canceled, with a reason.
    • next_run_at, plus lease_expires_at for the worker that claimed the row.
    • Expiry, max_runs and run_count.
    • CHECKs tie each kind to its fields.
  • chat_wakeup_runs: one row per delivery attempt.
    • Unique on (wakeup_id, run_key), where the run key is the scheduled time or an event key.
    • The wakeup_id foreign key is ON DELETE SET NULL, so deleting a wake-up does not reset the daily caps that count its runs.

Queries and authorization

  • ClaimDueChatWakeups claims due active rows with a lease under FOR UPDATE SKIP LOCKED, so replicas never claim the same row. FinishChatWakeupClaim succeeds only for the current lease holder.
  • InsertChatWakeup and InsertChatWakeupRun take the organization (and, for a run, the chat) from the parent row, so a caller cannot file a row under another chat or organization. InsertChatWakeupRun returns no row when that occurrence already ran.
  • Create, get, list, status change, delete and cancel-by-chat, plus daily counts of delivered runs per chat and per organization.
  • dbauthz authorizes each chat-scoped query through the owning chat, like UpsertChatTodos. Three worker-only queries span chats (claim, run status, organization count). They authorize on site-wide rbac.ResourceChat, which the chatd subject already has and members do not. No new RBAC resource, grant or scope.

Validation

  • Ran in Colima: go build ./..., go vet, chatdmig check, and the tests of chatdmig, forkdb, dbauthz, dbmetrics and codersdk. All passed. Rerunning forkdb/generate.sh gives no drift.
  • Each new test was checked by breaking the behavior it covers. Examples: removing SKIP LOCKED, the lease filter, the lease match on finish, ON CONFLICT, or the delivered filter, or switching the runs FK to CASCADE. Each break failed its test.
  • UAT ran in Colima: a fork server against a fresh Postgres 17, started with and without the flag.
    • The node applies at startup, and a restart is a no-op (identical ledger).
    • The experiments API lists fork-chat-wakeups only when the flag is set.
    • Psql checks of the constraints passed, and so did two concurrent claim sessions.
    • No remote Coder Agents chat ran, because the feature has no route or UI yet.

Size

About 700 hand-written lines. 150 of them are mechanical (dbmetrics pass-throughs and the dbauthz test table), and about 300 are tests. Generated, not counted: chat_wakeups.sql.go, models.go, querier.go, forkdbmock, apidoc, typesGenerated.ts.

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.
@ibetitsmike ibetitsmike changed the title feat(coderd/database): add chat wake-up tables and queries feat: add chat wake-up tables and queries Nov 9, 2026
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 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-10T01:10:40.187168Z a5068f2 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: bc3a7f3f7f

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

…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.
@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: d59e2abaee

ℹ️ 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/ARCHITECTURE.md Resolved
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.
@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. 🎉

Reviewed commit: f92aa5b90e

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

…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
@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. Hooray!

Reviewed commit: a5068f2af6

ℹ️ 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 (190ad7b164) 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 (190ad7b164), 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 e944a2a into main Nov 10, 2026
20 of 27 checks passed
@ibetitsmike
ibetitsmike deleted the mike/chat-wakeups-1-schema branch November 10, 2026 03:46
@ibetitsmike

Copy link
Copy Markdown
Owner Author

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

Merge evidence for head a5068f2af6f347f57ac4d527fed0cd5910b62f07:

  • fork-ci pull_request run 42747560000 passed; its build job checked out Merge a5068f2af6 into 190ad7b164 (current main). All checks green. Run 42733408059 had also passed on this head (agent-sdk, build, chat-tests, forkdb, graph, schema, site).
  • Codex on this exact head: no major issues (comment 6755984566). 0 unresolved review threads. Merge state CLEAN.
  • UAT on this head: a fresh database applies 11 migration nodes, chat_wakeups.organization_id is uuid NOT NULL with a foreign key and an index, and a restart is a no-op.
  • The only change to coderd/x/chatd/ARCHITECTURE.md is an owner TODO line.

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