Adds the `manage_automations` chat tool with its read and stop actions
(`list`, `get`, `disable`, `delete`), gated by an interim per-chat
switch. Tool sets have not landed, so this ships
`chats.manage_automations_enabled` as the RFC describes for that case;
tool sets later replace it and migrate its values.
Part of CODAGT-1212 (slice 12a of 3). Part of CODAGT-1135.
## What changes
- **Switch.** Migration `000611` adds `chats.manage_automations_enabled`
(default off). It is set through `POST /api/v2/chats` and `PATCH
/api/v2/chats/{chat}`. After creation only the chat owner may send the
field (403 otherwise). Turning it on returns 400 when the
`chat-automations` experiment is off for the chat owner (POST checks the
resolved `owner_id`, not the caller) or when the chat is a sub-agent
chat. Turning it off is always accepted. The audit table tracks it.
- **Offering.** The tool is offered only in root chats, outside plan
mode and explore mode, with the switch on, and with the experiment on
for the chat owner (decided once per turn, like `mcp-tool-search`). It
is not on the plan-mode or explore allowlists, and sub-agent chats are
created with the switch off.
- **Per-call recheck.** Every call reloads the chat and checks all rules
again, with a fresh experiment evaluation, before doing anything.
- **Scoping.** Owner and organization come from the chat, never from
arguments. Reads run as the owner through a new query that filters on
organization and owner. Other members' automations are "not found", even
when the chat owner is an organization admin.
- **Automation-reached turns.** Detected at call time from the full
history (compaction replays user rows without `automation_id`, so the
prompt window is not enough). In such turns only automations that target
the chat or created it (`chats.automation_id`) are visible.
- **Visibility of prompts and deletes.** `list` leaves out prompts (tool
results stay in the chat, which may be shared); `get` returns the prompt
of the automation it names. In a turn an automation reached, `delete`
only removes the triggering automation; the others can still be
disabled.
- **Audit.** `disable` and `delete` go through the management service
and write a background audit entry with `chat_id`, plus the triggering
`automation_id` and `input_id` in reached turns. Results use the API
shape and never contain a secret or secret hash.
`create`, `update`, `enable`, and `run_now` come in the next PR and
return "not available in this version" here.
Follow-up: CODAGT-1270 (bound the history read that reached-turn
detection does on each call).
## Size
About 1,380 hand-written lines: about 460 production, 680 tests, and 240
SQL that has to be there (the `chats_expanded` view rebuilt in both
migrations and the 17 inline column lists). Generated: +649/-280 (sqlc,
dbmock, dbmetrics, dump.sql, apidoc, API and audit docs,
`typesGenerated.ts`).
## Validation
- Unit and DB tests for every rule above (switch 403/400 cases, offering
rules, per-call recheck, owner/org scoping with an org-admin owner,
reached-turn detection including compaction replay, audit fields, secret
scan), plus a separate test audit. Full `make pre-push` passed before
push.
- Remote dogfood UAT passed on this slice: switch API cases, tool absent
with the switch off, in plan mode, in sub-agents and with the experiment
off for the owner (checked in the server's per-step tool lists),
list/get/disable/delete, and a secret scan of all tool output. Audit
entries are verified in Go tests only.
> [!NOTE]
> ARCHITECTURE.md prose was drafted by an agent at the user's request;
the user will review it.
## Stack
Stacked on the Automations for Coder Agents stack (30085 ... 1214b).
Next: slice 12b (write actions), then 12c (composer toggle).
---
_Generated with [`xum`](https://github.com/coder/xum) • Model:
`anthropic:claude-opus-5-5` • Thinking: `high`_
210 lines
5.4 KiB
SQL
210 lines
5.4 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: GetChatAutomationsByOrganizationIDAndOwnerID :many
|
|
SELECT
|
|
*
|
|
FROM
|
|
chat_automations
|
|
WHERE
|
|
organization_id = @organization_id::uuid
|
|
AND owner_id = @owner_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;
|
|
|
|
-- name: GetDueChatAutomationSchedules :many
|
|
-- Returns enabled schedule automations whose cursor is at or before now,
|
|
-- oldest cursor first, starting after the (after_next_run_at, after_id)
|
|
-- keyset so callers can page through every due row. Automations of
|
|
-- inactive owners and existing_chat automations whose target chat is gone
|
|
-- or archived are left out. It takes no locks: publishing rechecks each
|
|
-- row under the chat and automation locks.
|
|
SELECT
|
|
chat_automations.*
|
|
FROM
|
|
chat_automations
|
|
JOIN users ON users.id = chat_automations.owner_id
|
|
LEFT JOIN chats ON chats.id = chat_automations.target_chat_id
|
|
WHERE
|
|
chat_automations.kind = 'schedule'
|
|
AND chat_automations.enabled
|
|
AND chat_automations.schedule_next_run_at <= @now::timestamptz
|
|
AND (chat_automations.schedule_next_run_at, chat_automations.id) > (@after_next_run_at::timestamptz, @after_id::uuid)
|
|
AND users.status = 'active'
|
|
AND NOT users.deleted
|
|
AND (
|
|
chat_automations.target_mode = 'new_chat'
|
|
OR (chats.id IS NOT NULL AND NOT chats.archived)
|
|
)
|
|
ORDER BY
|
|
chat_automations.schedule_next_run_at,
|
|
chat_automations.id
|
|
LIMIT
|
|
@limit_count::int;
|
|
|
|
-- name: AdvanceChatAutomationScheduleCursor :execrows
|
|
-- Moves the schedule cursor of an enabled schedule automation from the
|
|
-- observed occurrence to next_run_at. It affects no row when the schedule
|
|
-- revision or the cursor changed since they were observed, so exactly one
|
|
-- caller moves the cursor past each occurrence. A NULL next_run_at means
|
|
-- no occurrence is pending.
|
|
UPDATE
|
|
chat_automations
|
|
SET
|
|
schedule_next_run_at = sqlc.narg('next_run_at')::timestamptz,
|
|
updated_at = @updated_at::timestamptz
|
|
WHERE
|
|
id = @id::uuid
|
|
AND kind = 'schedule'
|
|
AND enabled
|
|
AND schedule_revision = @schedule_revision::bigint
|
|
AND schedule_next_run_at = @observed_next_run_at::timestamptz;
|