Part of CODAGT-1210 (1 of 2). Part of CODAGT-1135. > Stack: PR 5 of "Automations for Coder Agents", on top of #30117. The ticket is split in two by operation: this PR delivers webhook events to `existing_chat` targets; the stacked follow-up delivers them to `new_chat` targets and closes CODAGT-1210. Until then a `new_chat` automation's events endpoint answers `501`. ## What this adds - `POST /api/experimental/chat-automations/{automation}/events`. External systems send the automation's webhook secret as `Authorization: Bearer <secret>` and a JSON body of at most 256 KiB. The route has no API key middleware and sits outside the `/api/experimental` group, behind its own rate limiter keyed by caller and a fixed endpoint key (IP address for unauthenticated callers), so varying the automation id does not reset the budget. - Secret check: SHA-256 of the bearer value, compared in constant time. Unknown ids, other kinds, and wrong secrets all get the same `401`. The `chat-automations` experiment is checked for the automation owner only after the secret matches (`404` when off). The body is then read: over the limit `413`, not JSON `400`. - `chatd.PublishAutomationWebhook` sends as the owner (`dbauthz.As` with the owner's subject) through `SendMessage` with the `queue` busy behavior set explicitly, so automations never interrupt or steer. `user_prompt_submit` hooks run first: a denial is `403`, an unreachable hook service `502`. - One admission check runs before the send (so refused events never reach hooks) and again in the `AdmitInTx` callback, which locks the automation together with the chat's queued automations in one ascending-order call. Under the lock it rechecks: enabled, secret version (a request verified before a rotation but committed after it gets `401`), single-use state, target chat (root chat of the owner, same org, not archived), owner status, and the owner's `chat:update` permission. It then applies **When busy**: `skip` refuses a busy chat (`409`), `queue` refuses input beyond the automations' share, half the chat queue cap rounded down and at least 1 (`429`); busy and the share count only queued rows that promotion would still deliver. It marks a single-use webhook consumed and returns the provenance. - The message has two parts: the saved prompt, and a labeled part with the body as untrusted event data inside `<automation_event_data>` tags. `json.HTMLEscape` escapes `<`, `>`, and `&` in the body, which keeps the JSON value and makes the closing tag impossible to forge. - `chatd.Config.Authorizer` (wired from `options.Authorizer`) for the explicit owner checks; publishing fails closed without one. - New query `ConsumeChatAutomationWebhookByID` with a dbauthz wrapper. Status codes follow the RFC's failure outcomes table; the chat-without-a-model case returns the same `400` as a person's message. ## Notes for reviewers - Single-use consumption runs from the chatd publish code as the owner, because the chatd role only reads `chat_automation`. No lower-layer RBAC change. - ARCHITECTURE.md prose was drafted by an agent at the user's request; the user will review it. - Size: about 1,460 hand-written lines after review fixes (about 770 of them tests), over the ~1,000 budget because of the review hardening. Generated: about 360 lines (`coderd/apidoc`, `dbmock`, `dbmetrics`, `querier.go`, `queries.sql.go`, `docs/reference/api/schemas.md`, `typesGenerated.ts`). - Dogfood UAT ran through Coder Agents on a real dev deployment with curl deliveries. Round 1 found error-mapping defects, fixed here; rounds 2 and 3 passed, the last on the final code (the head after it changes only ARCHITECTURE.md). - Deferred: CODAGT-1233 (stale automation rows filling the physical queue cap; lower-layer `SendMessage` path). --- _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
150 lines
3.3 KiB
SQL
150 lines
3.3 KiB
SQL
-- name: InsertChatAutomation :one
|
|
INSERT INTO chat_automations (
|
|
id,
|
|
organization_id,
|
|
owner_id,
|
|
name,
|
|
created_by_chat_id,
|
|
kind,
|
|
enabled,
|
|
target_mode,
|
|
target_chat_id,
|
|
new_chat_model_config_id,
|
|
reasoning_effort,
|
|
when_busy,
|
|
webhook_use,
|
|
webhook_secret_hash,
|
|
webhook_secret_version,
|
|
prompt,
|
|
schedule_cron,
|
|
schedule_time_zone,
|
|
schedule_next_run_at,
|
|
created_at,
|
|
updated_at
|
|
) VALUES (
|
|
@id,
|
|
@organization_id,
|
|
@owner_id,
|
|
@name,
|
|
@created_by_chat_id,
|
|
@kind,
|
|
@enabled,
|
|
@target_mode,
|
|
@target_chat_id,
|
|
@new_chat_model_config_id,
|
|
@reasoning_effort,
|
|
@when_busy,
|
|
@webhook_use,
|
|
-- A nil []byte is sent as an empty bytea, not NULL. Store NULL so
|
|
-- automations without a secret satisfy chat_automations_kind_shape.
|
|
NULLIF(@webhook_secret_hash::bytea, ''::bytea),
|
|
@webhook_secret_version,
|
|
@prompt,
|
|
@schedule_cron,
|
|
@schedule_time_zone,
|
|
@schedule_next_run_at,
|
|
@created_at,
|
|
@updated_at
|
|
)
|
|
RETURNING
|
|
*;
|
|
|
|
-- name: GetChatAutomationByID :one
|
|
SELECT
|
|
*
|
|
FROM
|
|
chat_automations
|
|
WHERE
|
|
id = @id::uuid;
|
|
|
|
-- name: DeleteChatAutomationByID :exec
|
|
DELETE FROM
|
|
chat_automations
|
|
WHERE
|
|
id = @id::uuid;
|
|
|
|
-- name: GetChatAutomationsByIDsForUpdate :many
|
|
-- Locks the given automations in ascending id order so concurrent
|
|
-- lockers always acquire automation row locks in the same order.
|
|
-- Missing ids are not returned.
|
|
SELECT
|
|
*
|
|
FROM
|
|
chat_automations
|
|
WHERE
|
|
id = ANY(@ids::uuid[])
|
|
ORDER BY
|
|
id
|
|
FOR UPDATE;
|
|
|
|
-- name: GetChatAutomationsByOrganizationID :many
|
|
SELECT
|
|
*
|
|
FROM
|
|
chat_automations
|
|
WHERE
|
|
organization_id = @organization_id::uuid
|
|
ORDER BY
|
|
created_at DESC,
|
|
id DESC;
|
|
|
|
-- name: CountChatAutomationsByOwnerID :one
|
|
-- Counts the automations owner_id owns across all organizations.
|
|
SELECT
|
|
COUNT(*)
|
|
FROM
|
|
chat_automations
|
|
WHERE
|
|
owner_id = @owner_id::uuid;
|
|
|
|
-- name: UpdateChatAutomationByID :one
|
|
UPDATE
|
|
chat_automations
|
|
SET
|
|
name = @name,
|
|
prompt = @prompt,
|
|
target_chat_id = @target_chat_id,
|
|
new_chat_model_config_id = @new_chat_model_config_id,
|
|
reasoning_effort = @reasoning_effort,
|
|
when_busy = @when_busy,
|
|
schedule_cron = @schedule_cron,
|
|
schedule_time_zone = @schedule_time_zone,
|
|
schedule_revision = @schedule_revision,
|
|
schedule_next_run_at = @schedule_next_run_at,
|
|
enabled = @enabled,
|
|
queue_generation = @queue_generation,
|
|
updated_at = @updated_at
|
|
WHERE
|
|
id = @id::uuid
|
|
RETURNING
|
|
*;
|
|
|
|
-- name: UpdateChatAutomationWebhookSecretByID :one
|
|
-- Replaces the webhook secret hash and increments the secret version. The
|
|
-- single-use marker webhook_consumed_at is intentionally kept.
|
|
UPDATE
|
|
chat_automations
|
|
SET
|
|
webhook_secret_hash = @webhook_secret_hash,
|
|
webhook_secret_version = webhook_secret_version + 1,
|
|
updated_at = @updated_at
|
|
WHERE
|
|
id = @id::uuid
|
|
RETURNING
|
|
*;
|
|
|
|
-- name: ConsumeChatAutomationWebhookByID :execrows
|
|
-- Marks an unconsumed single-use webhook as consumed. It affects no row
|
|
-- when the automation is not a single-use webhook or was already
|
|
-- consumed, so callers can refuse the delivery.
|
|
UPDATE
|
|
chat_automations
|
|
SET
|
|
webhook_consumed_at = @now::timestamptz,
|
|
updated_at = @now::timestamptz
|
|
WHERE
|
|
id = @id::uuid
|
|
AND kind = 'webhook'
|
|
AND webhook_use = 'single'
|
|
AND webhook_consumed_at IS NULL;
|