Files
coder/coderd/x/chatd/limits.go
T
Thomas Kosiewski 9316d67440 feat: add chat automations management API behind chat-automations (#30112)
Adds the management API for chat automations under
`/api/experimental/organizations/{organization}/chat-automations`,
behind the new user-scoped `chat-automations` experiment. This is the
first of two PRs for RFC step 3; nothing publishes automation messages
yet.

- **Experiment:** `chat-automations` is in `ExperimentsKnown`,
`ExperimentsUserScoped`, and the ruleguard list in `scripts/rules.go`.
Every route asks `chatd.AutomationsEnabled(ctx, api.ExperimentEvaluator,
callerID)`, which fails closed on a missing subject or evaluator; the
routes stay mounted and return 404 when the experiment is off for the
caller, so a runtime rule for one user applies on the next request
without a restart. There is no `/api/v2` alias.
- **Routes:** list, create (201), get, `PATCH`, delete (204), with
Swagger annotations and audit entries. List returns the automations in
the organization the caller can read (members see their own,
organization admins see all).
- **Service** (`coderd/x/chatd/automations.go`): create validates kind
and target shape, a non-empty prompt, a five-field cron and IANA time
zone, model access, and that an `existing_chat` target is a root chat of
the owner in the same organization. It defaults `when_busy` to `skip`
for schedules and `queue` for webhooks and computes the first schedule
cursor. `PATCH` runs the same per-field validation on each changed
field, rejects `kind` and `target_mode`, bumps `schedule_revision` on
prompt, cron, or time zone changes, and recomputes the cursor on cron or
time zone changes.
- **Owner-only:** only the owner can `PATCH` an automation, even with
organization-admin or site-owner roles (403); admins can list, read, and
delete.
- **Secrets:** a webhook's secret (`coder_automation_` prefix) is
returned once in the create response. Only its SHA-256 is stored, and it
appears in no list, get, audit, or log output.
- **Cap:** new option `--chat-max-automations-per-owner` (default 50)
caps automations per owner across all organizations; creating one more
returns 409. Creates serialize on a per-owner advisory lock.
- **Cron:** `cron.Standard(spec, timeZone)` accepts any standard
five-field expression (the existing `Weekly` rejects day-of-month and
month restrictions). Responses include the next five run times of an
enabled schedule.

PR 3b (stacked on this one) adds disable and re-enable with the queue
cutoff, makes delete run disable first, and adds secret rotation and the
schedule preview route. Until then, the queue promotion guard from
#30096 discards queued rows whose automation was deleted.

Dogfood UAT ran on a real dev instance and passed:
create/list/get/patch/delete via curl, owner-only 403s for an
organization admin and the site owner, the cap (including concurrent
creates and cross-organization counting), a runtime rule turning the
experiment off for one user without a restart, and `/api/v2` 404s.

Part of CODAGT-1209 (PR 3b closes it)
Part of CODAGT-1135
Refs CODAGT-1217

Stack: PR 3 of the Automations for Coder Agents stack, on top of #30096
(which is on top of #30085).

> This PR was written by an AI agent (xum) and reviewed by the agent's
own test audit and UAT. A human has not reviewed it yet.

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

---------

Signed-off-by: Thomas Kosiewski <tk@coder.com>
2026-10-02 15:54:49 +00:00

58 lines
2.5 KiB
Go

package chatd
import (
"cmp"
"github.com/coder/coder/v2/codersdk"
)
// Limits are the deployment chat limits.
type Limits struct {
// MaxStepsPerTurn is the maximum number of steps in a chat turn.
MaxStepsPerTurn int
// MaxGenerationRetries is the maximum number of consecutive retries
// after a model generation fails.
MaxGenerationRetries int
// MaxQueuedMessagesPerChat is the maximum number of messages that can
// be queued in a chat.
MaxQueuedMessagesPerChat int
// MaxAttachmentsPerChat is the maximum number of files linked to a
// chat.
MaxAttachmentsPerChat int
// MaxPromptBytes is the maximum size in bytes of the deployment system
// prompt, the plan mode instructions, and each user's custom prompt.
MaxPromptBytes int
// MaxConcurrentRecordingUploads is the maximum number of virtual
// desktop recordings that the server stores at the same time.
MaxConcurrentRecordingUploads int
// MaxAutomationsPerOwner is the maximum number of chat automations one
// user can own across all organizations.
MaxAutomationsPerOwner int
}
// LimitsFromConfig converts the deployment chat configuration to Limits.
func LimitsFromConfig(cfg codersdk.ChatConfig) Limits {
return Limits{
MaxStepsPerTurn: int(cfg.MaxStepsPerTurn.Value()),
MaxGenerationRetries: int(cfg.MaxGenerationRetries.Value()),
MaxQueuedMessagesPerChat: int(cfg.MaxQueuedMessagesPerChat.Value()),
MaxAttachmentsPerChat: int(cfg.MaxAttachmentsPerChat.Value()),
MaxPromptBytes: int(cfg.MaxPromptBytes.Value()),
MaxConcurrentRecordingUploads: int(cfg.MaxConcurrentRecordingUploads.Value()),
MaxAutomationsPerOwner: int(cfg.MaxAutomationsPerOwner.Value()),
}
}
// withDefaults sets each zero field to its codersdk default.
func (l Limits) withDefaults() Limits {
return Limits{
MaxStepsPerTurn: cmp.Or(l.MaxStepsPerTurn, codersdk.DefaultChatMaxStepsPerTurn),
MaxGenerationRetries: cmp.Or(l.MaxGenerationRetries, codersdk.DefaultChatMaxGenerationRetries),
MaxQueuedMessagesPerChat: cmp.Or(l.MaxQueuedMessagesPerChat, codersdk.DefaultChatMaxQueuedMessagesPerChat),
MaxAttachmentsPerChat: cmp.Or(l.MaxAttachmentsPerChat, codersdk.DefaultChatMaxAttachmentsPerChat),
MaxPromptBytes: cmp.Or(l.MaxPromptBytes, codersdk.DefaultChatMaxPromptBytes),
MaxConcurrentRecordingUploads: cmp.Or(l.MaxConcurrentRecordingUploads, codersdk.DefaultChatMaxConcurrentRecordingUploads),
MaxAutomationsPerOwner: cmp.Or(l.MaxAutomationsPerOwner, codersdk.DefaultChatMaxAutomationsPerOwner),
}
}