Skip to content

feat(coderd): create a chat for each webhook event of a new_chat automation - #30134

Merged
ThomasK33 merged 17 commits into
tk/codagt-1210-automations-webhooksfrom
tk/codagt-1210b-automations-new-chat
Oct 2, 2026
Merged

ThomasK33 merged 17 commits into
tk/codagt-1210-automations-webhooksfrom
tk/codagt-1210b-automations-new-chat

Conversation

@ThomasK33

@ThomasK33 ThomasK33 commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Closes CODAGT-1210. Part of CODAGT-1135.

Stack: PR 6 of "Automations for Coder Agents", on top of #30127 (CODAGT-1210 part 1, webhook events for existing_chat). This PR delivers webhook events to new_chat automations and replaces part 1's 501 for them.

What this adds

  • A webhook event for a new_chat automation creates a new chat as the automation owner through chatd CreateChat with an AdmitInTx callback, and the endpoint answers 202 with the new chat_id.
  • The chat uses the automation's model config and reasoning effort and client type api, and selects no MCP servers (the organization's Force On servers still apply). Its title is set explicitly to the automation name plus the acceptance time in UTC (<name> 2006-01-02 15:04 UTC), so automatic title generation does not run and event data never becomes the title.
  • Before the create, the same input checks as part 1 run (enabled, secret version, single-use unused), plus: the owner may create a chat in the organization (403), and the owner can read the automation's model config, which must be enabled and in the same organization (409). A user_prompt_submit denial creates no chat.
  • Under the automation lock, the callback rechecks all of that, sets chats.automation_id on the new chat (new query UpdateChatAutomationIDByID, dbauthz: update on the chat), consumes a single-use webhook, and returns the provenance. A refusal rolls back the whole creation.
  • The events endpoint records an audit entry for each created chat (action create, user = the automation owner, with automation_id and input_id in the additional fields), as chat creation through the chat API does.
  • Part 1's lock and consumption code is shared by both target modes.

Notes for reviewers

  • ARCHITECTURE.md prose was drafted by an agent at the user's request; the user will review it.
  • Size: about 600 hand-written lines (about 380 of them tests). Generated: about 65 lines (coderd/apidoc, dbmock, dbmetrics, querier.go, queries.sql.go).
  • Dogfood UAT ran through Coder Agents on a real dev deployment: curl deliveries created chats with the explicit UTC title, client type api, two message parts, and automation_id set; single-use concurrency created exactly one chat; a disabled model config gave 409 with no chat.

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

A webhook event for a new_chat automation now creates a chat as the
automation owner instead of answering 501.

- Before the create, the owner must be able to read the automation's
  enabled model config in the same organization (409 otherwise) and
  create a chat in the organization (403 otherwise), so a refusal
  happens before any lifecycle hook runs.
- The chat gets the automation name plus the acceptance time in UTC as
  its title, client type api, the automation's model config and
  reasoning effort, and no selected MCP servers. No title is generated.
- The AdmitInTx callback of CreateChat locks the automation and repeats
  every check, consumes a single-use webhook, and sets
  chats.automation_id through the new UpdateChatAutomationIDByID query.
  A refusal rolls back the whole creation.
- TestPublishAutomationWebhook/NewChatRefusedUnderLock changes the model
  config or the owner's roles while the user_prompt_submit hook runs, so
  only the checks repeated in the admission callback can refuse the
  input. Removing that recheck made both cases create a chat.
- TestChatAutomationEvents/ModelDisabled also posts to a new_chat
  automation and expects 409, not a retryable 500.
- The owner role removal is one fixture helper instead of two copies.
…oks' into HEAD

# Conflicts:
#	coderd/x/chatd/automations_publish.go
#	coderd/x/chatd/automations_publish_test.go
…oks' into tk/codagt-1210b-automations-new-chat
@ThomasK33

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-02T14:40:24.922034Z 7f5d2aa 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

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 3f3392df59

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

…oks' into tk/codagt-1210b-automations-new-chat
@ThomasK33

Copy link
Copy Markdown
Member Author

@codex review

@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: 6fe8d5de6c

ℹ️ 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/automations_publish.go Resolved
Chat creation through the chat API records an audit entry; a chat that
a webhook event creates did not. The events endpoint now records one
after the send, as created by the automation owner, with the automation
and input ids in the additional fields.

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

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 78e128c96c

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

…gt-1210b-automations-new-chat

No conflicts. make gen produced no diff.

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

Copy link
Copy Markdown
Member Author

sync with main d9c13dc: no content conflicts; make gen produced no diff


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

@ThomasK33
ThomasK33 marked this pull request as ready for review October 1, 2026 09:38
…gt-1210b-automations-new-chat

No conflicts. make gen produced no diff.

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

Copy link
Copy Markdown
Member Author

sync with main 35abc22: no content conflicts; make gen produced no diff


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

@ibetitsmike ibetitsmike left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Xum (an AI coding agent) prepared and posted this review on behalf of Mike. Each finding came from automated review passes and was re-verified against the code; please push back on anything that looks wrong.

The new_chat delivery path matches CODAGT-1210 part 2 and the RFC: creation goes through CreateChat with AdmitInTx, every pre-create check is repeated under the automation lock, a refusal rolls back the whole chat, and the earlier Codex audit finding is fixed. One small audit robustness gap remains.

Verdict: no blocking issues

Severity: P0 0, P1 0, P2 0, P3 1 inline (plus 1 folded nit)

Nits:

  • coderd/exp_chat_automations.go:495: json.Marshal of a map[string]string cannot fail, so the fields = nil branch is dead and would silently drop automation_id/input_id if it ever ran. Drop the branch or log the error like the GetChatByID failure above it.
Review coverage
  • code-review Standards: 2 findings; 1 kept (folded nit), 1 dropped (ARCHITECTURE.md edits are the owner's work per AGENTS.md Chatd, and the PR body says the user will review that prose)
  • code-review Spec: 0 findings; no spec gaps or scope creep
  • Adjudicator: 1 additional P3 found while verifying the audit path
  • frontend-review: skipped, no files under site/
  • coder-frontend-review: skipped, no files under site/

Comment thread coderd/exp_chat_automations.go Outdated
Comment on lines +498 to +509
audit.BackgroundAudit(ctx, &audit.BackgroundAuditParams[database.Chat]{
Audit: *api.Auditor.Load(),
Log: api.Logger,
UserID: automation.OwnerID,
OrganizationID: automation.OrganizationID,
Action: database.AuditActionCreate,
New: chat,
Status: http.StatusAccepted,
IP: r.RemoteAddr,
UserAgent: r.UserAgent(),
AdditionalFields: fields,
})

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

P3: Automation chat audit entry can be lost on client disconnect and has no request ID

BackgroundAudit and the GetChatByID above it run on the request context, so a webhook sender that disconnects or times out after CreateChat commits cancels both calls: the chat exists but no create entry is written (only a log line). The entry also leaves RequestID as the zero UUID although httpmw.AttachRequestID runs on this route, so it cannot be tied to the server logs. Other request-scoped background audits (auditChatModelConfigTransitions in coderd/exp_chats.go, coderd/usersecrets.go) use context.WithoutCancel(ctx) and pass httpmw.RequestID(r), and audit.InitRequest exports on context.Background().

Fix: derive auditCtx := context.WithoutCancel(ctx) at the top of auditChatAutomationCreatedChat, use it for the lookup and the export, and set RequestID: httpmw.RequestID(r).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 3a1f381: auditChatAutomationCreatedChat uses context.WithoutCancel(ctx) for the chat lookup and the export, and sets RequestID: httpmw.RequestID(r). TestChatAutomationEvents/NewChatTarget now asserts the entry's request ID is set. The same commit drops the dead json.Marshal error branch.


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

…gt-1210b-automations-new-chat

No conflicts. make gen produced no diff.

---
_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
Run the created-chat lookup and audit export on a context detached from
the webhook request so a disconnecting sender cannot drop the entry, and
record the request ID. Drop the dead json.Marshal error branch.

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

Copy link
Copy Markdown
Member Author

sync with main d11809f: merged #30127; the created-chat audit now survives a client disconnect and carries the request ID, and the dead json.Marshal branch is gone (Mike's review)


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

@ibetitsmike ibetitsmike left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Xum re-reviewed this PR on behalf of @ibetitsmike using GPT-6.1 Sol (high) Exec subagents.

All three prior items are fixed in code; the existing audit thread is answered but still unresolved/outdated on GitHub, with no duplicate inline comment here. New findings: Standards 0, Spec 0, Frontend 0 (no own-layer site changes). Saved fresh-head webhook, publisher/creation/dependency and authorization tests passed, and the request-ID negative control failed as intended. Disconnect survival is source-verified only, broader local checks were not rerun, and Storybook remains failed; supplied inherited Pixel logs show report-server HTTP 500, not an own-layer source defect.

…gt-1210b-automations-new-chat

No conflicts. make gen produced no diff.

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

Copy link
Copy Markdown
Member Author

sync with main a328bee: merged #30127


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

@ibetitsmike ibetitsmike left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Xum validated this PR and approved it on behalf of @ibetitsmike.

Validated head af833abeef5a. The audit lifetime, request-ID and create-audit fixes remain intact after the restack. Fresh real new-chat HTTP/audit, publisher/creation/dependency and authorization tests passed. Current required CI passed. Disconnect survival remains source-verified rather than newly reproduced, and accepted tool/workspace follow-ups remain unchanged.

…gt-1210b-automations-new-chat

No conflicts. make gen produced no diff.

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

Copy link
Copy Markdown
Member Author

merged #30127


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

…gt-1210b-automations-new-chat

No conflicts. make gen produced no diff.

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

Copy link
Copy Markdown
Member Author

merged #30127


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

…gt-1210b-automations-new-chat

No conflicts. make gen produced no diff.

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

Copy link
Copy Markdown
Member Author

merged #30127 (main dc07908)


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

…tions-new-chat

Brings main ff158c0 into this layer. No conflicts.

---
_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
Main now starts automatic title generation for every chat whose title
source is fallback, the default. new_chat automation chats set an explicit
title but no source, so title generation would replace it with a title
derived from the first message, which includes the webhook event data.
Mark the title as user-set, as the chat API does for an explicit title.

---
_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
ThomasK33 added a commit that referenced this pull request Oct 2, 2026
…tions-schedules

Brings the new_chat automation title source fix from #30134. Resolved
automations_publish.go: this layer's automationNewChatTitle call keeps
the new TitleSource line.

---
_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
ThomasK33 added a commit that referenced this pull request Oct 2, 2026
…ations-run-now

Brings the new_chat automation title source fix from #30134.

---
_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
ThomasK33 added a commit that referenced this pull request Oct 2, 2026
…ions-chat-labels

Brings the new_chat automation title source fix from #30134.

---
_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
ThomasK33 added a commit that referenced this pull request Oct 2, 2026
…mations-list-page

Brings the new_chat automation title source fix from #30134.

---
_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
ThomasK33 added a commit that referenced this pull request Oct 2, 2026
…tions-editor

Brings the new_chat automation title source fix from #30134.

---
_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
ThomasK33 added a commit that referenced this pull request Oct 2, 2026
…ons-webhook-editor

Brings the new_chat automation title source fix from #30134.

---
_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
ThomasK33 added a commit that referenced this pull request Oct 2, 2026
…automations-tool

Brings the new_chat automation title source fix from #30134.

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

Copy link
Copy Markdown
Member Author

Main sync (sweep 11) found one behavior interaction in this layer, fixed in 7f5d2aa:

Main (000608_chat_title_source) now starts automatic title generation for every chat whose title_source is fallback, which is the default. new_chat automation chats set an explicit title (<name> <time>) but no source, so after the merge, title generation would have replaced that title with one derived from the first message, which contains the webhook event data. The chat is now created with TitleSource: user, the same source the chat API uses for an explicit title. TestPublishAutomationWebhook/NewChat asserts the source and fails without the fix.


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

@ThomasK33

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 7f5d2aa18e

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

@ibetitsmike ibetitsmike left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Xum validated this PR and approved it on behalf of @ibetitsmike.

Revalidated head 7f5d2aa18e6b against base b623c2606b78. Independent Standards and Spec renewal found no new blockers; the original accepted exclusions remain unchanged.

Re-reviewed the explicit user title source and its privacy boundary. HTTP, publisher, authorization, SDK and inherited title-generation checks passed. The maintained stored-row assertion passed, failed when only the production TitleSource assignment was removed through a scratch Go overlay, then passed restored.

Approval is scoped to this layer and the reviewed inherited interactions. No full-repository suite, fresh Pixel screenshots or remote UAT claimed.

@ThomasK33
ThomasK33 merged commit 1e1e884 into main Oct 2, 2026
29 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 2, 2026
@ThomasK33
ThomasK33 deleted the tk/codagt-1210b-automations-new-chat branch October 2, 2026 15:55
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants