Skip to content

feat: add the create route for chat automations - #32096

Open
ThomasK33 wants to merge 33 commits into
tk/automations-api-readfrom
tk/automations-api-create
Open

ThomasK33 wants to merge 33 commits into
tk/automations-api-readfrom
tk/automations-api-create

Conversation

@ThomasK33

@ThomasK33 ThomasK33 commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Users, and later the agent tool, cannot create a chat automation yet (RFC: Automations for Coder Agents). This PR adds the create route behind the chat-automations experiment, the one create function that the route and the tool share, and the two limits that an operator sets. It is the first code that writes a webhook secret and the only route that returns one. Part of CODAGT-1135.

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

What changed

  • POST /api/experimental/organizations/{organization}/chats/automations answers 201 with the automation and, for a bearer or github webhook, the secret that Coder generated, once, with Cache-Control: no-store. For standard_webhooks the caller supplies the whsec_ secret and no response returns it. Get and list have no secret field, so no later read returns one.
  • chatautomation.Creator.Create authorizes the actor, validates every field, reads the target chat, model configuration and project as the actor, takes a per-owner advisory lock, counts the owner's automations, generates the secret and inserts as the actor. Every CHECK and NOT NULL condition of chat_automations is validated in Go first, because a failed constraint writes the whole row, secret included, to the PostgreSQL log. No error text quotes the prompt or a secret, and every 5xx has an empty detail, because the request logger logs 5xx bodies.
  • --chat-max-automations-per-owner (default 50, 1 to MaxInt32) caps one owner across organizations. The count runs as a subject that can only read that owner's automations (AsChatAutomationCounter) and never reaches the insert. Automations of a deleted organization still count: its last member can still create there. --chat-min-automation-schedule-interval (default 5m, 1m to 24h) is checked against Schedule.MinInterval().
  • InsertChatAutomation refuses a deleted or system owner and holds a shared lock on the owner row, so a soft delete that starts during an insert waits and then removes the new row.
  • ChatAutomation is audited for create, with prompt and webhook_secret masked.
  • TestServerDBCrypt (the test of feat: encrypt the webhook secret of a chat automation with dbcrypt #32076) now seeds chat automations for live users only. The insert refuses a deleted owner, and the soft delete trigger already removes a deleted user's automations.

Suggested reading order: coderd/x/chatd/chatautomation/create.go and validate.go, coderd/chatautomations.go, the query and dbauthz changes, then the tests. The rest is generated code and docs.

Scope

  • One PR because the create function, its validation, the cap and the secret path are one boundary: the route without the validation would write a row that can leak a secret into a log, and the validation without the route has no caller. It is about 800 product lines. Generated files and tests are the rest.
  • The shared create function has no creating-chat parameter yet. C2, the agent tool write actions, adds it with its first caller. The API request has no such field.
  • Not in this PR: update, rotate, run now, the webhook endpoint, the scheduler, the agent tool and any frontend.
  • With the experiment off, which is the default, the route answers 404 and nothing else changes.
  • This PR adds no role and no escalation path. The owner is always the actor, and dbauthz refuses an insert whose owner is not the actor, so a custom role with organization-level create still cannot create for another user.

Tradeoffs

  • The two limits are flags, so a change needs a restart, and replicas started with different values enforce different limits until all restart. Lowering the cap deletes nothing.
  • The response reads the owner and the chat titles after the commit. A failure of that read still returns 201 with the secret, because a 500 would lose the only copy that the caller can get.
  • A client that retries after a lost response creates a second automation. The first secret stays unseen in a row that the owner can delete, and it counts against the cap.
  • An existing_chat target must be a chat that the automation's owner owns. The RFC checks update on the target chat (line 459) and says that no handler or tool checks ownership in code (line 422), but update is not enough: an organization admin who holds agents-access has update on every chat of the organization, and main forbids that admin to send a message to a member's chat because chat processing forwards the owner's credentials to external services (checkSendChatMessage in coderd/exp_chats.go). A run into a member's chat would do the same. Creator.Create refuses it with a 400 on target_chat_id, so the agent tool and the update route inherit the rule. It narrows what RBAC grants.
  • A chat, model configuration or project that a concurrent delete removes between the check and the insert gives 409, and a deleted owner gives 403 "This user cannot own chat automations." A trigger of the table refuses a chat or project that is gone at the insert, and a foreign key check fails when a delete that was still open at the trigger commits. Create maps both to the same 409. A model configuration deleted after the insert's own read of it is only soft deleted, so the create answers 201 and the row names a soft-deleted model configuration, which the run path must refuse.
  • --chat-min-automation-schedule-interval uses Schedule.MinInterval() (feat(coderd/schedule/cron): add Standard schedule constructor #32063), which compares the times of day of a schedule and nothing else. It can refuse a schedule whose runs near midnight fall on different days, and it does not see clock changes. The flag text says so.

Live check

On a local enterprise build with its own PostgreSQL and encryption keys set, with the experiment on for one user: one automation of each kind and scheme was created, and each generated secret appeared once, in its own create response, and 0 times in the get and list bodies, the audit log, the raw webhook_secret column (stored as ciphertext), the server log and the PostgreSQL log. A user with the experiment off got 404, and the cap, the validation errors and an oversized body answered 409, 400 and 413. An owner at the cap also got 409 for a create in a second organization and in a soft-deleted one. An organization admin with agents-access who targeted a member's chat got 400 and no row.


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

Add --chat-max-automations-per-owner (default 50, 1 to MaxInt32) and
--chat-min-automation-schedule-interval (default 5m, 1m to 24h). Both
follow the other chat limits: a serpent option in ChatConfig and a range
check in DeploymentValues.Validate. The create route reads them through
chatautomation.Limits.
…a narrow subject

InsertChatAutomation inserts only for an owner that is neither deleted nor
a system user, and locks the owner row so that a concurrent soft delete of
the owner waits and then removes the new row. AsChatAutomationCounter can
read the automations of one owner across organizations and nothing else.
dbauthz.IsWrapped lets the shared create function assert its store.
Add the create request and response, the client method, the limits that
the create function enforces and its typed errors.
Add Creator.Create and the validation of a create request. Create
authorizes the actor, validates every field without a database read,
reads the chat, model configuration and project as the actor, then takes
the owner lock, checks the per-owner limit as a narrow subject, generates
the webhook secret for bearer and github, and inserts as the actor.
POST /api/experimental/organizations/{organization}/chats/automations
creates an automation behind the chat-automations experiment. The response
carries the generated webhook secret of a bearer or github webhook, once,
with Cache-Control: no-store. The audit log gets a create action.
An owner that is deleted, a system user or unknown gets no row, and a soft
delete of the owner that starts while an insert is open waits for it.
Lists the create request and response types and the create action of chat
automations.
…creating chat

A time zone that cron.Standard rejects is reported on schedule_time_zone, not
on schedule_cron. The creating chat of the agent tool is read before the
insert, so that a missing one is not reported as a missing owner.
Pin the HTTP mapping of the create route (201 with no-store, 400, 403, 404,
409, 413 and the empty-detail 500), that a secret appears only in the create
response of a bearer or github webhook, that a failed read after the commit
keeps the secret, the audit entry (mock and enterprise auditor) and that a
dbcrypt key encrypts the stored secret of every scheme.
Real PostgreSQL and a real authorizer. TestCreate compares every stored column,
TestCreateRejected checks each field and reference rule and that a rejected
request never inserts, and the other tests cover the per-owner limit (with a
deterministic race on the owner lock), the owner guard, authorization before any
store call, and the creating chat of the agent tool.
Drop an unreachable branch from the create handler, say what a missing row
from the insert can mean, give project_id its own refusal reason, inline the
interval range, fix the flag text (no route changes a schedule yet), and cut
test rows that repeat another row's branch.
Read the audit log as an auditor, return the header and status from the
raw create helper, and mark the soft delete that must run outside the
transaction.
InsertChatAutomation refuses a deleted owner, as the user soft delete trigger
removes the automations of a deleted user.
@linear-code

linear-code Bot commented Nov 12, 2026

Copy link
Copy Markdown

CODAGT-1135

@ThomasK33

Copy link
Copy Markdown
Member Author

@codex review

@ThomasK33

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 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-13T16:54:29.577258Z c872b46 Manual request
🔒 Security Review ✅ Completed 2026-11-13T16:55:53.043483Z c872b46 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: 5a5dec8401

ℹ️ 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/chatautomation/create.go Resolved
Comment thread coderd/chatautomations.go Resolved
@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

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

Reviewed commit: 5a5dec8401

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

@codex review

@ThomasK33

Copy link
Copy Markdown
Member Author

@codex security review

@ThomasK33
ThomasK33 changed the base branch from tk/automations-base-api-create to tk/automations-api-read November 13, 2026 08:42
@ThomasK33
ThomasK33 temporarily deployed to docs-preview November 13, 2026 08:44 — with GitHub Actions Inactive
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 77faba62a1

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

@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-32096.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 Security Review

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

Reviewed commit: 77faba62a1

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 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.
- Delete TestCreateOwner: the route race OwnerDeleted and TestInsertChatAutomationOwnerGuard own the owner guard.
- Delete TestCreateTargetOwnership: the route row TargetChatOfAnotherUser owns the rule and now asserts its detail.
- Delete TestCreateRejected rows Cron L loop, ZoneFactory, ScheduleOverStrict and TwoViolations: sibling rows, cron tests and the route row EmptyObject cover them.
- Delete TestCreate/ScheduleAcrossDaylightSaving: cron TestSchedule_MinInterval_Limits owns MinInterval.
- Delete TestCreate/RFCExample and its constant: the explicit enabled true moves into the ScheduleEnabled row.
- Delete TestCreateReferenceDeleted/ChatBeforeRecheck: the route race TargetChatDeleted covers the same window.
- Delete TestCreateScheduleNeedsNow: the guard has no caller that passes a zero time.
- Delete TestCreateAuthorization/MemberWithoutAgentsAccess: the route row NoAgentsAccess now sends an invalid body.
- Merge recorder, recordingStore and insertHook into one hookStore, and drop the call-name assertions.
- Move TestCreateCap/DeletedOrganizationStillCounts into the database count test.
- Delete the read loop and the GitHubNewChat row of TestCreateChatAutomation: db2sdk.ChatAutomation never reads the secret.
- Delete the route rows SecretForBearer, InvalidJSON and the five role rows: the Creator row and the role matrix cover them.
- Delete enterprise TestChatAutomationCreateEncrypted: dbcrypt TestChatAutomations/KeysSet covers encryption, also in a transaction.
- Delete enterprise TestChatAutomationCreateAuditEntry: Test_chatAutomationSecretsMasked covers the masking.
…irst uses it

- Remove CreateParams.CreatedByChatID, its checkReferences block, its foreign key in the insert error mapping and its comments from create.go: the API never sets it and no other caller exists.
- Keep an explicit zero CreatedByChatID in insertParams, because the exhaustruct linter needs every field of InsertChatAutomationParams set.
- Delete TestCreateCreatingChat, the CreatingChat row of TestCreateReferenceDeletedWhileInserting and its CreateParams argument, and the createdBy wiring in TestCreate.
- F1: TestCreateChatAutomationRejected counts the rows of the caller before and after each rejected request, so a create that stores a row and still answers 400 turns TargetChatOfAnotherUser red.
- M1: TestCreateReferenceDeleted asserts again that the answer is not ErrOwnerUnavailable, so a trigger or foreign key error that also carries ErrOwnerUnavailable turns both rows red.

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

Copy link
Copy Markdown
Member Author

Review-cap waiver, recorded before the push: the operator approved this push on 2026-11-13. It removes tests that repeat other tests or cannot fail, keeps the guards a verifier named, moves the CreatedByChatID path of the create function to the agent tool PR that first sets it, and merges the new head of #32079. It starts Codex review round 6, which the stack rule kept for the sync head. A new security flaw, data loss or broken core behavior found on that head stops this PR.


Generated with xum • Model: anthropic:claude-opus-5-5 • Thinking: high

…/automations-api-create

Conflicts, both in test files that A2a cut and this branch extends:
- coderd/chatautomations_test.go: drop TestChatAutomationsDeleteQueuedMessage, keep the create tests.
- coderd/database/chatautomations_test.go: drop TestChatAutomationColumnsOnChats and the imports it needed, keep the owner guard tests and the dbtime import.

A2a dropped the third parameter of newFixture, so the create tests call newFixture(t, db).

---
_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

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: c872b465a1

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

@ThomasK33
ThomasK33 temporarily deployed to docs-preview November 13, 2026 16:55 — 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: c872b465a1

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
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

This branch was previously deployed

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