Adds the storage and authorization pieces for Automations for Coder Agents. No route, SDK request type, or chatd code uses them yet, so nothing is exposed. This is the bottom PR of the Automations stack (RFC step 1). Later PRs add the chatd acceptance seam, management routes, webhooks, and schedules on top of these names and constraints. **Schema** (migrations 000609, 000610) - `chat_automations` table with enums `chat_automation_kind`, `chat_automation_target_mode`, `chat_automation_webhook_use`, and `chat_automation_when_busy`. It includes `created_by_chat_id`, the webhook secret hash and version, the single-use `webhook_consumed_at` marker, the schedule cursor and revision, and `queue_generation`. Named CHECKs enforce the target and kind shapes from the RFC. A composite foreign key keeps `new_chat_model_config_id` in the automation's organization, the same pattern the model override tables use. A trigger (`chat_automations_chat_organization`) keeps `target_chat_id` and `created_by_chat_id` in that organization too. A composite foreign key cannot do that while keeping `ON DELETE SET NULL` on PostgreSQL 13. The trigger validates a reference only on insert or when it changes, so chat deletion can still clear both references. Indexes cover the owner and both chat references for cascades and `SET NULL`. - Provenance columns with partial indexes and no foreign key to `chat_automations`: `automation_id`, `input_id`, `queue_generation` on `chat_queued_messages` (all set or all null), `automation_id` and `input_id` on `chat_messages`, and `chats.automation_id`. `chats_expanded` is recreated to include `automation_id`. - `chat_automation` values for `api_key_scope` and `resource_type`. The down migration is a no-op because enum values cannot be dropped. **Authorization** - New organization-scoped RBAC resource `chat_automation` (create, read, update, delete). The `agents-access` role grants it for the member's own automations. It is excluded from the member role's user-level permissions, so a member without `agents-access` gets nothing. Site owners and organization admins get it through their existing all-permissions lists. The owner-only update, enable, and run rule from the RFC belongs to the management service in a later PR. - The generated scopes stay out of `externalLowLevel`, so API keys gain no user-requestable scopes. - `dbauthz` wrappers for the three queries this PR adds: `InsertChatAutomation`, `GetChatAutomationByID`, and `DeleteChatAutomationByID`. `InsertChatAutomation` also requires update on the target chat, read on the creating chat, and read on the new-chat model config (its user and group ACL). There is also a `dbgen.ChatAutomation` helper. **Audit** - `enterprise/audit/table.go` entries for `ChatAutomation`, with `webhook_secret_hash` and `prompt` marked `ActionSecret`. The prompt is treated as chat content, like chat titles. The schedule cursor, revision, and queue generation are ignored as internal bookkeeping. `Chat.automation_id` is tracked. **Required checks from the ticket** - Migrations apply and roll back: `TestMigrate` and `TestMigrateUpWithFixtures` (new fixture for 000609). Dogfood UAT also applied the migrations on a dev instance, rolled them back, re-applied them, and compared schema dumps. After rollback, the only differences from main were the enum values that cannot be dropped and develop.sh's own `_develop` schema. After re-applying, the only difference from this branch's `dump.sql` was the `_develop` schema. - Secret hash marked secret: `Test_chatAutomationSecretFieldsRedacted` checks the table entries and that an audit diff contains neither the raw hash, its base64 form, nor the prompt text. Also covered: `TestRolePermissions` cases for own, other owner, other organization, and a member without `agents-access`. `TestChatAutomationShapeConstraints` covers the named CHECKs, the cross-organization foreign key and trigger, a cross-organization retarget, and deleting referenced chats. Under `site/`, only generated files changed (`typesGenerated.ts`, `rbacresourcesGenerated.ts`). Closes CODAGT-1207 Part of CODAGT-1135 --- _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_ --------- Signed-off-by: Thomas Kosiewski <tk@coder.com>
103 lines
2.1 KiB
SQL
103 lines
2.1 KiB
SQL
INSERT INTO chat_automations (
|
|
id,
|
|
organization_id,
|
|
owner_id,
|
|
name,
|
|
created_by_chat_id,
|
|
kind,
|
|
enabled,
|
|
target_mode,
|
|
target_chat_id,
|
|
reasoning_effort,
|
|
when_busy,
|
|
webhook_use,
|
|
webhook_secret_hash,
|
|
webhook_secret_version,
|
|
prompt
|
|
)
|
|
SELECT
|
|
'6a1f0c3e-2b7d-4c8e-9f10-3d5e7a9b1c21',
|
|
c.organization_id,
|
|
c.owner_id,
|
|
'fixture-webhook',
|
|
c.id,
|
|
'webhook',
|
|
true,
|
|
'existing_chat',
|
|
c.id,
|
|
'high',
|
|
'queue',
|
|
'multi',
|
|
'\xdeadbeef'::bytea,
|
|
1,
|
|
'Summarize the webhook payload.'
|
|
FROM (
|
|
SELECT id, organization_id, owner_id FROM chats ORDER BY created_at, id LIMIT 1
|
|
) AS c;
|
|
|
|
INSERT INTO chat_automations (
|
|
id,
|
|
organization_id,
|
|
owner_id,
|
|
name,
|
|
kind,
|
|
enabled,
|
|
target_mode,
|
|
new_chat_model_config_id,
|
|
prompt,
|
|
schedule_cron,
|
|
schedule_time_zone,
|
|
schedule_next_run_at
|
|
)
|
|
SELECT
|
|
'7b2e1d4f-3c8e-4d9f-8a21-4e6f8b0c2d32',
|
|
m.organization_id,
|
|
u.id,
|
|
'fixture-schedule',
|
|
'schedule',
|
|
true,
|
|
'new_chat',
|
|
m.id,
|
|
'Post the daily status report.',
|
|
'0 9 * * 1-5',
|
|
'UTC',
|
|
'2026-01-05 09:00:00+00'
|
|
FROM (
|
|
SELECT id, organization_id FROM chat_model_configs ORDER BY created_at, id LIMIT 1
|
|
) AS m
|
|
CROSS JOIN (
|
|
SELECT id FROM users ORDER BY created_at, id LIMIT 1
|
|
) AS u;
|
|
|
|
INSERT INTO chat_queued_messages (
|
|
chat_id,
|
|
content,
|
|
created_by,
|
|
automation_id,
|
|
input_id,
|
|
queue_generation
|
|
)
|
|
SELECT
|
|
a.target_chat_id,
|
|
'[{"type":"text","text":"Summarize the webhook payload."}]'::jsonb,
|
|
a.owner_id,
|
|
a.id,
|
|
'8c3f2e5a-4d9f-4e0a-9b32-5f7a9c1d3e43',
|
|
a.queue_generation
|
|
FROM chat_automations a
|
|
WHERE a.id = '6a1f0c3e-2b7d-4c8e-9f10-3d5e7a9b1c21';
|
|
|
|
UPDATE chat_messages
|
|
SET
|
|
automation_id = '6a1f0c3e-2b7d-4c8e-9f10-3d5e7a9b1c21',
|
|
input_id = '9d4a3f6b-5e0a-4f1b-8c43-6a8b0d2e4f54'
|
|
WHERE id = (
|
|
SELECT id FROM chat_messages ORDER BY id LIMIT 1
|
|
);
|
|
|
|
UPDATE chats
|
|
SET automation_id = '7b2e1d4f-3c8e-4d9f-8a21-4e6f8b0c2d32'
|
|
WHERE id = (
|
|
SELECT id FROM chats ORDER BY created_at, id LIMIT 1
|
|
);
|