Skip to content

feat: add get, list and delete routes for chat automations - #32079

Open
ThomasK33 wants to merge 46 commits into
tk/automations-dbcryptfrom
tk/automations-api-read
Open

ThomasK33 wants to merge 46 commits into
tk/automations-dbcryptfrom
tk/automations-api-read

Conversation

@ThomasK33

@ThomasK33 ThomasK33 commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Chat automations send a saved prompt to a chat from a webhook or a schedule (RFC: Automations for Coder Agents). This PR adds the routes that read and delete them, behind the chat-automations experiment, so that create, the scheduler and the webhook endpoint can follow in small PRs. Part of CODAGT-1135.

Stacked on #32076. Review only this diff. It merges after #32076.

Size of the diff: 706 lines of hand-written product code, 697 lines of tests, and 1,287 lines of generated code and docs.

What changed

Three routes under /api/experimental/organizations/{organization}/chats/automations: list (with ?owner=), get and delete. They answer 404 when the experiment is off for the caller. Access follows the RFC role table through RBAC and dbauthz: members read and delete their own automations, and organization administrators and deployment owners read and delete every automation in the organization. The response type codersdk.ChatAutomation has no secret field, and an owner or a chat title appears only when the caller can read that user or chat. chatautomation.Delete re-reads the row as the caller and deletes it in one transaction, and the delete is audited with prompt and webhook_secret masked.

Suggested reading order: coderd/chatautomations.go, coderd/x/chatd/chatautomation/delete.go, coderd/database/db2sdk/chatautomation.go, codersdk/chatautomations.go, then the tests. The rest is generated code and docs.

Scope

  • Not in this PR: create, update, secret rotation, the run path, the webhook endpoint, the scheduler and the agent tool.
  • With the experiment off, which is the default, the routes answer 404 and nothing else changes. The experiment that chore!: remove the chat automations experiment #30412 removed was never in a tagged release, so a leftover --experiments=chat-automations flag exists only on dev or dogfood deployments, and those turn these routes on.
  • Two replicas on different versions during an upgrade: no case exists. A replica without this PR has none of the routes and answers 404, as the gate does. A replica ignores a stored rule for an experiment that it does not know.

Tradeoffs

  • The delete refusal for queued automation messages and its lock land with feat(coderd): clean up the chat queue of a deleted automation #32140 and B2c, the first code that queues such messages. A caller that can read but not delete still gets 403. Until feat(coderd): clean up the chat queue of a deleted automation #32140, two deletes of one automation that overlap can both answer 204 and write two delete audit entries, like main's other deletes without a lock.
  • The list has no pagination, as in the RFC. An organization administrator's list holds every automation of the organization (members times the per-owner cap that the create PR adds), and each row carries its prompt, up to MaxPromptBytes.
  • The response has three fields beyond the RFC example: new_chat_model_config_id, project_id and reasoning_effort, named as in the create request. The RFC says PATCH "takes the same fields as create", so an editor must be able to read them. paused_reasons is always [] until the run path exists, and there is no field for the count of skipped and refused runs: that field arrives with its writer.
  • The audit action map for ChatAutomation lists only delete. The generated audit log page lists every action in the map, and nothing emits create or write yet. Later PRs add the actions they emit.
  • The RFC says "Custom roles can grant any chat_automation action." The custom role API lets an actor grant only the permissions that the actor holds (customRoleEscalationCheck), and no built-in role holds create. This PR adds no create route and keeps the role table: a custom role with read and delete deletes, one with read only gets 403, and one with delete and without read gets 404 and deletes nothing, because the delete re-reads the row with read.

Live check

On a local enterprise build with its own PostgreSQL and the experiment on for one of two users, the three routes behaved as described, and a canary secret appeared 0 times in every response, the audit log, the server log and the PostgreSQL log.


Generated with xum • Model: anthropic:claude-sonnet-5-5 • Thinking: xhigh

…chedules

Automations store a five-field cron expression and an IANA time zone.
Standard parses both and gives a repeated local time one run and a
nonexistent local time one run after the clock change, which the
robfig/cron Next used by Weekly and Daily does not do. MinInterval returns
the shortest gap between runs for the API check, because Min only samples
one week from 1970-01-01.
… MinInterval

Next searches again when robfig/cron gives up after five years, so a leap day schedule keeps running across 2100. The zone period walk starts 26 hours before the reading, so historic offsets above 14 hours still find the first instance of a repeated time. MinInterval scans 28 years, which holds every calendar a year can have, so schedules that set both day fields get their true shortest gap. It stops once it sees two days in a row, and searches from midnight, so common schedules take microseconds.
Go reports wrong zone period bounds after the last transition in a zone's data, so the walk over zone periods never ended on December 31 of a leap year. A reading is now resolved from the offsets of the zone around it, with fixed bounds on every loop, and Next returns the zero time instead of spinning when a step does not move forward.

MinInterval drops the calendar scan and returns the shortest distance between two times of day around the clock. Standard rejects the zone data files localtime, posixrules and Factory.
…a through 2100

The differential test runs over every clock change of five zones and December 31 in years up to 2100, with the zone data of the host and the data that Go embeds. Every Next call in the termination test has a deadline. The model of cron comes from robfig/cron, not from the code under test.
…limits

No two runs are closer than MinInterval while the UTC offset of the zone does not change. An offset change can shorten a gap that spans it, and a pair across midnight counts without days in a row. Tests pin both.
A zone name with an empty, dot or dot-dot segment is rejected, because time.LoadLocation cleans the path and ./localtime reached the host file. Tests start Next inside repeated periods of up to a whole day, so the bound on the readings that Next walks is pinned, and a test from a zone with a local mean time offset above 15 hours pins the range of offsets that Next reads. The comment on the search for later runs now states 16 years, which is what robfig's year limit gives.
…the zone once

A zone name is rejected when its first segment is posix or right, because right counts leap seconds and gives wrong runs on hosts that have it. The files that are not zones are rejected in any directory. The error for an unknown zone holds the name once. A test starts Next in Juneau, where the walk visits 1,441 readings, one more than the repeated day of Sitka.
Mark database.ChatAutomation as an Auditable, with the target name and ID,
the organization requirement, and the audit table. webhook_secret and
prompt are ActionSecret, so an entry shows that they changed and never
their value.
…n needs

GetChatsByIDs reads the chats whose titles a response shows.
GetChatIDsWithQueuedMessagesByAutomationID and
DeleteChatQueuedMessagesByAutomationID let a delete lock the chats that hold
queued messages of the automation before it removes them.
The routes sit under /api/experimental/organizations/{organization}/chats/automations
behind the chat-automations experiment, which is evaluated for the caller and
answers 404 when off. The response type has no webhook secret field. Delete
locks the automation, then each chat that holds a queued message of it in chat
ID order like a chat transition, then removes the queued messages and the row.
ARCHITECTURE.md prose was drafted by an agent at the user's request; the user
will review it.
The request logger logs the response body of a 5xx, so the handlers write the
message only and log the error.
Cut test rows, phases and helpers that no product mutant separates,
merge the two lock-order subtests into one test, and replace the
expected-value builder of the response test with targeted assertions.
Add TestChatAutomationsInternalError: a 500 from list, get or delete
carries a message and no detail, and the response body never holds the
cause.
This PR audits only the delete. A later PR adds the action that it audits,
so that the audit log page never lists an action that nothing emits.
A role that can read a chat automation and cannot delete it got 404. The
chat project routes answer 403 in that case. Also correct the comments that
the review found false or unclear, and the ARCHITECTURE.md bullet.
…ssage names the automation

Delete no longer removes queued messages. Nothing writes automation_id on a
queued row before the run path exists, and how a delete leaves a chat's queue
is a chat state decision that belongs with that path. Delete takes the
automation lock, re-reads the automation and refuses with ErrQueuedMessages
when a queued message names it. The handler answers a generic 500.
@ThomasK33

Copy link
Copy Markdown
Member Author

@codex review

@ThomasK33

Copy link
Copy Markdown
Member Author

@codex security review

@ThomasK33
ThomasK33 changed the base branch from tk/automations-base-api-read to tk/automations-dbcrypt November 13, 2026 08:18
@ThomasK33
ThomasK33 temporarily deployed to docs-preview November 13, 2026 08:19 — with GitHub Actions Inactive
@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Docs preview

Links open this pull request's preview at https://pr-32079.docs.cdr-preview.dev, which updates with each push.

Check off each page once it's been reviewed. If a page changes in a later push, its checkbox clears automatically so it gets a fresh look. Pages not yet wired into the docs navigation aren't listed here.

Preview sites for each built commit (2)

Every built commit keeps its own preview, newest first. Previews aren't indexed by search engines and are deleted when this pull request is merged or closed.

@chatgpt-codex-connector

Copy link
Copy Markdown

💡 Codex Review

setHasPendingParameterInput(true);

P2 Badge Mark parameter updates pending before validation

When a parameter changes, this pending flag is not set until handleChange has awaited form.setFieldValue; if Formik/Yup validation yields after rendering the edited value, the submit button remains enabled and a quick submission can create a workspace with a value whose dynamic-parameter evaluation has not even been queued. Set the pending state synchronously before awaiting the form update, then debounce and send the request.



P2 Badge Add a story for the parameter-evaluation state

When a dynamic-parameter response is pending, the view now disables the submit button and replaces its normal state with a spinner, but every story inherits isEvaluatingParameters: false; therefore Pixel never captures this newly reachable visual state. Add a story that sets this prop to true so regressions in the pending-state rendering are covered.

AGENTS.md reference: site/AGENTS.md:L74-L75

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

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

key, err := client.GetProvisionerKey(ctx, res.Key)

P2 Badge Security: Keep the provisioner key out of the request URL

When a lower-privileged actor can read coderd or proxy logs, this metadata lookup exposes the newly created provisioner credential. GetProvisionerKey interpolates res.Key into /api/v2/provisionerkeys/{key}, and coderd records r.URL.Path verbatim, so every successful CLI creation persists the reusable one-time secret outside stdout. The key can then authenticate a provisioner daemon for its organization and tags. Return metadata from create, or fetch it without placing the secret in the URL.

SECURITY.md reference: SECURITY.md:L13-L15

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

View security finding report

Only the user who started this review can view the report in Codex.

@ThomasK33

Copy link
Copy Markdown
Member Author

@codex review

@ThomasK33

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 1b9dbeca65

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

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: 1b9dbeca65

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@ThomasK33

Copy link
Copy Markdown
Member Author

Two Codex findings on this head (comments 6847923172 and 6847932144) name site/src/pages/CreateWorkspacePage/ and enterprise/cli/provisionerkeys.go. Neither file is in this PR. Codex ran before the base branch was retargeted to tk/automations-dbcrypt, so it saw the commits of main that this branch merged. After the retarget, Codex reviewed the same head again and found no issue (receipts 6848153678 and 6848185049).

ThomasK33 added a commit that referenced this pull request Nov 13, 2026
…tk/automations-tool-read

Conflict in coderd/chatautomations.go: the sync of #32079 edited the doc of
loadChatAutomationRefs, which this PR moved to chatautomation.LoadRefs. The
deletion stays. The create handler of #32096 now calls chatautomation.LoadRefs,
and LoadRefs reads the chats through GetReadableChatsByIDs, the filtering read
that #32079 renamed so that main's all-or-nothing GetChatsByIDs stays.
- coderd: drop TestChatAutomationsDeleteQueuedMessage. chatautomation TestDelete/QueuedMessageOfTheAutomation owns the refusal and TestChatAutomationsInternalError/Delete owns the 500 without detail.
- coderd: drop the MemberNoAgents, Auditor and ScopedToken rows of TestChatAutomationsAuthorization and their fixtures. The dbauthz TestChatAutomationAuthorization matrix owns those role and scope outcomes.
- coderd: drop the UnreadableUsername row of TestChatAutomationsOwnerFilter. UnreadableUsernameWithReadableRows kills the same mutants. The Me row now owns an automation, so that reading me as a username fails it.
- coderd: drop the allow-list token half of TestChatAutomationsResponse/Unreadable. TestChatAutomationsReadOnlyRole owns the owner fallback, and Webhook now asserts OwnerID.
- coderd: assert the audit resource type and target in TestChatAutomationsOwnerLifecycle and drop TestChatAutomationResource, whose ResourceRequiresOrgID check restated a flag that nothing observes.
- chatautomation: drop TestDeleteForbidden (TestChatAutomationsReadOnlyRole owns the 403) and TestDeleteOtherOrganization (TestChatAutomationsAuthorization/WrongOrganizationPath owns the 404).
- codersdk: drop TestChatAutomationResponseTypesHaveNoSecret, a field name grep. TestChatAutomationsResponse/Webhook reads the response body for the secret.
- database: drop TestGetReadableChatsByIDs. TestChatAutomationsResponse/Webhook resolves the target and creator chats through the query.
- enterprise/audit: drop the created-direction block and the JSON loop of Test_chatAutomationSecretsMasked. The exact map of the rotated diff covers the masking.
…rst uses it

Nothing writes a chat_queued_messages row with automation_id yet, so the
refusal in chatautomation.Delete never fires. Move it, with its tests, to the
PR that adds the first writer.

- Move ErrQueuedMessages and the queued-message refusal of chatautomation.Delete.
- Move the advisory lock in Delete and the exported LockID.
- Move the query GetChatIDsWithQueuedMessagesByAutomationID: SQL, dbauthz method and MethodTestSuite row, generated code, dbmetrics wrapper.
- Move TestDeleteLock, the TestDelete rows QueuedMessageOfTheAutomation and OtherQueuedMessages, and the queue and queued test helpers.
- Keep the 403 for a caller that can read an automation and cannot delete it: map the dbauthz NotAuthorized error of DeleteChatAutomationByID to ErrDeleteForbidden. The removed probe query did this.

_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-sonnet-5-5` • Thinking: `xhigh`_
@ThomasK33

Copy link
Copy Markdown
Member Author

@codex review

@ThomasK33

Copy link
Copy Markdown
Member Author

@codex security review

@ThomasK33
ThomasK33 temporarily deployed to docs-preview November 13, 2026 16:12 — with GitHub Actions Inactive
@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: f12982c2f3

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@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: f12982c2f3

ℹ️ 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/chatautomations.go

// This row holds the decrypted webhook secret. The converter never
// copies it.
automation, err := api.Database.GetChatAutomationByID(ctx, id)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Read automations without decrypting webhook secrets

In a multi-replica deployment where an already-running keyless replica shares the database with a newly keyed replica, requesting an automation created by the keyed replica returns 500 even though this response never uses the secret. GetChatAutomationByID always decrypts webhook_secret; the supported failure mode is demonstrated in enterprise/dbcrypt/dbcrypt_internal_test.go:2480-2505, while the list query avoids it by omitting secret columns. Use an authorized secret-free single-row query here so ordinary reads remain available during encryption-key rollout.

Useful? React with 👍 / 👎.

@ThomasK33
ThomasK33 added this pull request to stack #32184 November 13, 2026 17:08
@nickvigilante
nickvigilante removed their request for review November 13, 2026 21:56
@ssncferreira
ssncferreira removed their request for review November 16, 2026 18:50

This branch was previously deployed

1 inactive deployment
docs-preview — f12982c2 Deployed Nov 13, 2026 by ThomasK33 via deploy #156
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